OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

Can you provide a method to adjust the size of the pdf to A4 size, and also include the adaptation of AcroForms?

Open Lychengit opened this issue 1 year ago • 9 comments

Because the page width of some PDFs is very large, I want to adjust the PDF to an A4 size that is easier to read. I hope there is a way to do this easily.

Here is a pdf sample with AcroForms: 6.pdf

Lychengit avatar Mar 13 '24 04:03 Lychengit

Can one assume that the content of the pages already fits on A4 and one merely has to increase or decrease the margins? Then it suffices to change media and crop boxes. Or is it necessary to scale or even reflow the content?

mkl-public avatar Mar 13 '24 13:03 mkl-public

It should be necessary to scale. Such as this pdf: dabaoz1.pdf It's too big to see it all.

Lychengit avatar Mar 14 '24 00:03 Lychengit

You want to read an existing PDF and than recreate it with a "better" PageSize?

asturio avatar Mar 14 '24 06:03 asturio

image change into image

Lychengit avatar Mar 14 '24 06:03 Lychengit

I think, if you want to scale and reflow the content, you can use OpenPDF to do this. But this functionality is quite high level to implement directly in the library. I don't even know how one could do this. The text must be extracted, and den inserted in the new document. And images too. A very complex task.

asturio avatar Mar 16 '24 17:03 asturio

I tried itext,It can move the content of the page, but AcroForms are lost,How to transfer AcroForms as is,Like the 6.pdf this file, how complete scale:

PdfReader pdfReader = new PdfReader(source); Document doc = new Document(); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(target)); doc.open(); PdfContentByte cb = writer.getDirectContent(); for(int i = 1; i <= pdfReader.getNumberOfPages(); i++){ PdfImportedPage page = writer.getImportedPage(pdfReader, i); float width = page.getWidth(); float height = page.getHeight(); doc.setPageSize(new com.itextpdf.text.Rectangle(PageSize.A4.getHeight(), PageSize.A4.getWidth())); doc.newPage(); float widthScale = getWidthScale(height); float heightScale = getHeightScale(width); cb.addTemplate(page, widthScale, 0, 0, heightScale,0,0); } doc.close();

Lychengit avatar Mar 17 '24 01:03 Lychengit

Copying the page with getImportedPage actually only copies the static page content, annotations are lost. Thus, I would propose instead to change the media box and crop box of the page and prepend a scaling transformation to the page content stream.

Scaling (obviously) changes the coordinates of the content. Thus, all other objects that also contain coordinates on the page, need to be adjusted. This includes the annotations but also objects like link destinations.

mkl-public avatar Mar 18 '24 17:03 mkl-public

Is there any code example of specific implementation? thanks.

Lychengit avatar Mar 19 '24 00:03 Lychengit

I fear there is no such an example. This is a very specific problem, and scaling thinks around is not trivial. Such an example "could" be in pdf-toolbox, but maybe it won't be a "simple" one.

asturio avatar Mar 27 '24 22:03 asturio