开发者

How do I scale a PDF page while maintaining orientation with iTextSharp?

How do I scale a pdf page, while maintaining rotation in itextsharp? I have the following, but I lose rotation:

    public static void ScaleToLetter(string inPDF, string outPDF)
    {
        PdfReader reader = new PdfReader(inPDF);
        Document doc = new Document(PageSize.LETTER);
        Document.Compress = true;
        PdfWriter writer = PdfWriter.GetInstance(doc, 
            new FileStream(outPDF, FileMode.Create));
        doc.Open();
        PdfContentB开发者_C百科yte cb = writer.DirectContent;
        int rotation;

        PdfImportedPage page;
        for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
        {
            page = writer.GetImportedPage(reader, pageNumber);
            rotation = reader.GetPageRotation(pageNumber);

            cb.AddTemplate(page, 
                PageSize.LETTER.Width / reader.GetPageSize(pageNumber).Width, 
                0, 0, 
                PageSize.LETTER.Height / reader.GetPageSize(pageNumber).Height, 
                0, 0);
            doc.NewPage();
        }
        doc.Close();
    }


I had a similar issue, and got it working by changing the height and width of each page, from LETTER to LETTER rotated. In other words, if the current page is thin and tall then it is vertical. But if it is wide and short, then it is horizontal and I should rotate the page.

Here's your code changed:

    public static void ScaleToLetter(string inPDF, string outPDF)
    {
        using (var reader           = new PdfReader(inPDF))
        using (var doc              = new Document(PageSize.LETTER))
        {
            Document.Compress       = true;
            using (
                var writer          = PdfWriter.GetInstance(doc,
                    new FileStream(outPDF, FileMode.Create)))
            {
                doc.Open();
                var cb              = writer.DirectContent;

                PdfImportedPage page;
                for (var pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
                {
                    page            = writer.GetImportedPage(reader, pageNumber);

                    if (page.Width <= page.Height)
                        doc.SetPageSize(PageSize.LETTER);
                    else
                        doc.SetPageSize(PageSize.LETTER.Rotate());
                    doc.NewPage();

                    cb.AddTemplate(page,
                        doc.PageSize.Width / reader.GetPageSize(pageNumber).Width,
                        0, 0,
                        doc.PageSize.Height / reader.GetPageSize(pageNumber).Height,
                        0, 0);
                }
                doc.Close();
            }
        }
    }

I also needed to constrain proportions, so I ended up with this:

 public static void ScaleToA4(string inPDF, string outPDF)
    {
        using (var reader        = new PdfReader(new MemoryStream(File.ReadAllBytes(inPDF))))
        using (var document      = new Document(PageSize.A4))
        using (var ms            = new MemoryStream())
        using (var writer        = PdfWriter.GetInstance(document, ms))
        {
            document.Open();
            var cb               = writer.DirectContent;

            for (var pageNumber  = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
            {
                var page         = writer.GetImportedPage(reader, pageNumber);
                document.SetPageSize(page.Width <= page.Height ?
                    PageSize.A4 : PageSize.A4.Rotate());
                document.NewPage();

                var widthFactor  = document.PageSize.Width / page.Width;
                var heightFactor = document.PageSize.Height / page.Height;
                var factor       = Math.Min(widthFactor, heightFactor);

                var offsetX      = (document.PageSize.Width - (page.Width * factor)) / 2;
                var offsetY      = (document.PageSize.Height - (page.Height * factor)) / 2;
                cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
            }
            document.Close();
            File.WriteAllBytes(outPDF, ms.GetBuffer());
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜