Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate multiple PDFs and write to one single PDF

Tags:

c#

pdf

itextsharp

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);
like image 659
JasonSec Avatar asked Apr 08 '16 14:04

JasonSec


1 Answers

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

EDIT: As @mkl has stated below the following is really only applicable for rotate entries of 0° or 90° if you have pages at 180° or 270° this code will need some modifications. Also any annotations in the original will be lost.

like image 147
JasonSec Avatar answered Oct 01 '22 11:10

JasonSec