Can you provide a method to adjust the size of the pdf to A4 size, and also include the adaptation of AcroForms?
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
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?
It should be necessary to scale. Such as this pdf: dabaoz1.pdf It's too big to see it all.
You want to read an existing PDF and than recreate it with a "better" PageSize?
change into
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.
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();
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.
Is there any code example of specific implementation? thanks.
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.