Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lychengit opened this issue Mar 13, 2024 · 9 comments

Comments

@Lychengit
Copy link

Lychengit commented Mar 13, 2024

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

@mkl-public
Copy link
Contributor

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?

@Lychengit
Copy link
Author

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

@asturio
Copy link
Member

asturio commented Mar 14, 2024

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

@Lychengit
Copy link
Author

image
change into
image

@asturio
Copy link
Member

asturio commented Mar 16, 2024

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.

@Lychengit
Copy link
Author

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();

@mkl-public
Copy link
Contributor

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.

@Lychengit
Copy link
Author

Is there any code example of specific implementation? thanks.

@asturio
Copy link
Member

asturio commented Mar 27, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants