I have the following simplified code for pulling existing 8x10 PDFs from multiple locations, rotating them if need be (almost all need to be), then writing them to a single 11x17 PDF page by page...
while (Page < StackOne.Length) { Files++; using (var strm = new FileStream(RenderPath + "Test_" + Page + ".pdf", FileMode.Create, FileAccess.Write, FileShare.Read)) { using (var MasterReport = new iTextSharp.text.Document(iTextSharp.text.PageSize._11X17)) { using (var writer = PdfWriter.GetInstance(MasterReport, strm)) { MasterReport.Open(); MasterReport.NewPage(); var cb = writer.DirectContent; for (; Page <= NumPages * Files && Page < StackOne.Length; Page++) { var ProductionEntry = StackOne[Page - 1]; var filepath = NetPath + ProductionEntry.UniqueProductId + ".pdf"; if (File.Exists(filepath)) { var reader = new PdfReader(filepath); var pagesize = reader.GetPageSize(1); if (pagesize.Height > pagesize.Width) { var ExistingPage = reader.GetPageN(1); var rotation = ExistingPage.GetAsNumber(PdfName.ROTATE); int desiredrot = 90; if (rotation != null) { desiredrot += rotation.IntValue; desiredrot %= 360; } ExistingPage.Put(PdfName.ROTATE, new PdfNumber(desiredrot)); } cb.AddTemplate(writer.GetImportedPage(reader, 1), 50, 50); } MasterReport.NewPage(); } } } } }
However the page rendered doesn't have the pages rotated as they should be, I've verified the height > width branch is indeed being taken but the pages returned are still 8x10 instead of 10x8 written on each 11x17 page.
I searched around for a question this specific but couldn't find one that wasn't just writing to another file or the entire page instead of to a specific location on an 11x17 sheet.
EDIT: So with a little experimenting and looking at other examples I'm able to rotate the 8x10 page and write it to my 11x17 but unfortunately I can't seem to find a way to place it exactly where I want it here's the relevant code snippet:
var reader = new PdfReader(filepath); var tm = new AffineTransform(1.0F, 0, 0, 1.0F, x, y); if (reader.GetPageSize(1).Height > reader.GetPageSize(1).Width) tm.SetTransform(0, -1f, 1f, 0, 0, reader.GetPageSize(1).Height); writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), tm);
2 Answers
Answers 1
Okay so after tons of searching and sifting through code it appears the answer was quite simple (as usual), as shown above my initial problem was putting a rotate tag on the page didn't actually rotate the page as I expected. After learning about the option of specifying a matrix for the pdftemplate it was pretty simple here's the working code:
var reader = new PdfReader(filepath); var tm = new AffineTransform(); if (reader.GetPageSize(1).Height > reader.GetPageSize(1).Width) tm.SetTransform(0, -1f, 1f, 0, 0, reader.GetPageSize(1).Height); tm.Concatenate(new AffineTransform(1f, 0, 0, 1f, y, x)); writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), tm);
P.S. http://partners.adobe.com/public/developer/en/pdf/PDFReference.pdf P: 162 (143 in physical form) for those who aren't fresh out of algebra
Answers 2
You may simple add affine transform into AddTemplate procedure.
Like this:
PdfContentByte cb = stamper.getOverContent(pageNumber); int rotation = reader.getPageRotation(pageNumber); PdfImportedPage page = stamper.getImportedPage(reader, pageNumber); if (rotation == 270) cb.addTemplate(page, 0, 1f, -1f, 0, reader.getPageSizeWithRotation(pageNumber).getWidth(), 0); else if (rotation == 90) { cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(pageNumber).getHeight()); } else { cb.addTemplate(page, 0, 0); }
Its java, but I think it is not a problem.
0 comments:
Post a Comment