0

I could not replace PDF page in PDF document when there is high margin. How to resize PDF page using pdfbox2.0 ?

If pdf page content (in input pdf document) is 6" x 8" - then i want to make page size as 5" x 7" and save the pdf document

  • 1
    Very confusing question: do you want to replace or resize? PageDrawer is for rendering. – Tilman Hausherr May 18 '16 at 09:44
  • if pdf page content (in input pdf document) is 6" x 8" - then i want to make page size as 5" x 7" and save the pdf document – chandra sekhar May 19 '16 at 13:11
  • Please include that comment in your question, that would make it more useful for others (you can edit). – Tilman Hausherr May 19 '16 at 18:56
  • I get pdf document with left and right page margins with 1 inch as input to my application - in this case it works fine. But if I get page margins as 2 inch, then currently we are preparing images as per margins and saving the document ( in this case, we will not able to do "Text search" ). Now I want to make pdf page with 2 inch margins as text searchable without any content loss. Hope this is understandable. Thanks for help Tilman. – chandra sekhar May 20 '16 at 07:26
  • I'm more confused than ever. Did answer help or not? If not, what happened? Or is the problem that you need more code, i.e. also load and save a document? Or is it a new question, i.e. why does text search not work for some files? – Tilman Hausherr May 20 '16 at 08:27
  • when ever pdf page contain high margin, I have to resize the content (without loss of content, font style etc). currently, what i am doing is : convert the page into Image and draw that image in the page as per new dimensions/margins (in this case, text search is not possible in that page) – chandra sekhar May 20 '16 at 08:54

1 Answers1

0

Assuming you have a PDPage object:

PDRectangle mediaBox = page.getMediaBox();
if (mediaBox.getWidth() == 6 * 72 && mediaBox.getHeight() == 8 * 72)
     mediaBox = new PDRectangle(5 * 72, 7 * 72);

and then save your document.

If you're using 1.8, then use findMediaBox() instead of getMediaBox().

It might be more useful to set the cropBox instead, the methods are similar. I can't really tell because I don't have your files and don't know what real problem you're trying to solve. It might also be useful to adjust all 4 elements of the PDRectangle (see javadoc) instead of just the width and height.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • PDFRenderer pdfRenderer = new PDFRenderer(document); BufferedImage highResImage = pdfRenderer.renderImageWithDPI(pageNo, OPTIMAL_DPI, ImageType.RGB); PDImageXObject ximage = LosslessFactory.createFromImage(document, highResImage); Matrix transform = new Matrix(width, 0, 0, height, xPosition, yPosition); PDPage newPage = PDFFileUtil.replacePage(document, pageNo); PDPageContentStream contentStream = new PDPageContentStream(document, newPage, PDPageContentStream.AppendMode.OVERWRITE, false); contentStream.drawImage(ximage, transform); – chandra sekhar May 20 '16 at 08:54
  • above code i am using, to draw image as per new margin – chandra sekhar May 20 '16 at 08:55
  • Well yes, if you replace the content stream with an image, you lose the text (if there ever was some). You will also lose quality because you replace vector graphics with pixel graphics. With my solution from this answer you won't lose any content. If it doesn't work, please post a link to the PDF. – Tilman Hausherr May 20 '16 at 10:23
  • PDRectangle oldmb = page.getMediaBox(); float mbwidth = oldmb.getWidth() * reductionFactor; float mbheight = oldmb.getHeight() * reductionFactor; PDRectangle newmb = new PDRectangle(mbwidth, mbheight); – chandra sekhar May 20 '16 at 12:08
  • like above, i have created cropBox and set. PDPage newPage = PDFFileUtil.replacePage(document, pageNo); newPage.setCropBox(newcb); newPage.setMediaBox(newmb); document.save( documentBean.getPreparedFile() ); now, in generated pdf document i can see Blank pages (no content is visible) – chandra sekhar May 20 '16 at 12:10
  • I don't know what PdfFileUtil is doing. You don't need it anyway. Just get your PDPage object (in 2.0 with PDDocument.getPage(), in 1.8 with PDDocument.getDocumentCatalog().getAllPages().get()) and set the mediabox and save. There is no need to replace the page. – Tilman Hausherr May 20 '16 at 12:29
  • i did it same, but content is getting cut off (top and right side) and also, i can see font size in the pdf page is big, so that content is lost. – chandra sekhar May 20 '16 at 12:59
  • I need the PDF to research more. – Tilman Hausherr May 20 '16 at 13:07
  • Of course it is getting cut off on the top and right. That is what you asked for: cut off the margins. – Tilman Hausherr May 20 '16 at 13:41
  • I want the page content should be resized/shrinked to new crop box. (by any means of automatically decreasing font size or decreasing line space etc). Means original page content should be fitted into new cropbox size . I think this is same problem, that I am having : http://stackoverflow.com/questions/21375150/how-to-resize-existing-pdf-page-size/21378162#21378162 – chandra sekhar May 20 '16 at 14:17
  • Your question is constantly changing / evolving. If you want to shrink the content stream stuff (a few hours ago you wanted to cut off margins), then prepend a scale CTM command to your content stream. Prepend is easy in PDFBox 2.0, see the PDPageContentStream construtors javadoc. In the content stream, call transform(Matrix.getScaleInstance(0.9f,0.9f)); – Tilman Hausherr May 20 '16 at 14:26
  • Btw the other answer you point to does the same with iText as I do with PDFBox. – Tilman Hausherr May 20 '16 at 15:04
  • PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false); contentStream.transform(Matrix.getScaleInstance(0.6f, 0.6f)); document.save( documentBean.getPreparedFile() ); I have tried with above code, there is no effect on resulting page. Is there anything has to be done after creating scaleInstance on CTM – chandra sekhar May 23 '16 at 08:36
  • I said prepend, not append. – Tilman Hausherr May 23 '16 at 08:37
  • and don't forget to close your content stream. – Tilman Hausherr May 23 '16 at 08:37
  • Thats really awesome, thanks for help Tilman. Content in pdf page is getting resized as per my reduction factor. Resized content is left down justified. is there any method to make it centre justified? – chandra sekhar May 23 '16 at 11:12
  • use getTranslateInstance to create a translating matrix, this will move. You'll have to do your calculations yourself based on the page size as PDF has no
    command.
    – Tilman Hausherr May 23 '16 at 11:16