I am currently in the process of writing an app that "formats" PDF's to our
requirements, using iText 2.1.7.
We basically take a portrait PDF, and scale down the pages, so we can fit 2
pages of the original PDF, on one landscape page of the new PDF. We also
leave some space at the bottom of the page which is used for post
processing.
This process works 90% of the time as it should.
However, we received a PDF that has been cropped/trimmed by the content
department, and when we view this PDF in Acrobat, it looks as excpected.
However, when we process it, the new PDF includes the entire original
MediaBox, and the crop lines.
Here is the code we use, and how a problem output looks.
Code:
File tempFile = new File(tempFilename);PdfReader reader = new
PdfReader(originalPdfFile);Document doc = new Document(new
RectangleReadOnly(842f, 595f), 0, 0, 0, 0);PdfWriter writer =
PdfWriter.getInstance(doc, new FileOutputStream(tempFile));
doc.open();for (int i = 1; i < reader.getNumberOfPages(); i = i + 2) {
doc.newPage();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, i); // page #1
float documentWidth = doc.getPageSize().getWidth() / 2;
float documentHeight = doc.getPageSize().getHeight() - 65f;
float pageWidth = page.getWidth();
float pageHeight = page.getHeight();
float widthScale = documentWidth / pageWidth;
float heightScale = documentHeight / pageHeight;
float scale = Math.min(widthScale, heightScale);
float offsetX = (documentWidth - (pageWidth * scale)) / 2;
float offsetY = 65f; //100f
cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);
PdfImportedPage page2 = writer.getImportedPage(reader, i+1); // page #2
pageWidth = page.getWidth();
pageHeight = page.getHeight();
widthScale = documentWidth / pageWidth;
heightScale = documentHeight / pageHeight;
scale = Math.min(widthScale, heightScale);
offsetX = ((documentWidth - (pageWidth * scale)) / 2) + documentWidth;
offsetY = 65f; //100f
cb.addTemplate(page2, scale, 0, 0, scale, offsetX, offsetY);//430f
}
doc.close();
original pdf in acrobat:
http://i.stack.imgur.com/cilSZ.jpg
modified in acrobat, showing unwanted pretrim content:
http://i.stack.imgur.com/ebrEr.jpg
Thanks
Shaine
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK
Develop, test and display web and hybrid apps with a single code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php