如何在使用iTextSharp保持方向的同时缩放PDF页面?

问题描述 投票:6回答:1

如何缩放pdf页面,同时在itextsharp中保持旋转?我有以下,但我失去轮换:

    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();
        PdfContentByte 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();
    }
itextsharp
1个回答
15
投票

我有一个类似的问题,并通过改变每页的高度和宽度,从LETTER到LETTER旋转来实现它。换句话说,如果当前页面很薄而且很高,那么它是垂直的。但如果它是宽而短,那么它是水平的,我应该旋转页面。

这是您的代码更改:

    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();
            }
        }
    }

我还需要限制比例,所以我最终得到了这个:

 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());
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.