itextsharp PdfCopy and landscape pages

房东的猫 提交于 2019-12-01 12:37:20

You need to use reader.GetPageRotation(i);

Solved code:

    public static int AddPagesFromStream(Document document, PdfCopy pdfCopy, Stream m, bool addFooter, int detailPages, string footer, int footerPageNumOffset, int numPages, string pageLangString, string printLangString)
    {
        CreateFont();
        try
        {
            m.Seek(0, SeekOrigin.Begin);
            var reader = new PdfReader(m);

            // get page count
            var pdfPages = reader.NumberOfPages;
            var i = 0;

            // add pages
            while (i < pdfPages)
            {
                i++;

                // import page with pdfcopy
                var page = pdfCopy.GetImportedPage(reader, i);

                // get page center
                float posX;
                float posY;
                var rotation = reader.GetPageRotation(i);
                if (rotation == 0 || rotation == 180)
                {
                    posX = page.Width / 2;
                    posY = 0;
                }
                else
                {
                    posX = page.Height / 2;
                    posY = 20f;
                }

                var ps = pdfCopy.CreatePageStamp(page);
                var cb = ps.GetOverContent();

                // add footer
                cb.SetColorFill(BaseColor.WHITE);
                var gs1 = new PdfGState {FillOpacity = 0.8f};
                cb.SetGState(gs1);
                cb.Rectangle(0, 0, document.PageSize.Width, 46f + posY);
                cb.Fill();

                // Text
                cb.SetColorFill(BaseColor.BLACK);
                cb.SetFontAndSize(baseFont, 7);
                cb.BeginText();

                // create text
                var pages = string.Format(pageLangString, i + footerPageNumOffset, numPages);
                cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, printLangString, posX, 40f + posY, 0f);
                cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, footer, posX, 28f + posY, 0f);
                cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, pages, posX, 20f + posY, 0f);
                cb.EndText();

                ps.AlterContents();

                // add page to new pdf
                pdfCopy.AddPage(page);
            }

            // close PdfReader
            reader.Close();
            // return number of pages
            return i;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            return 0;
        }
    }
Bruno Lowagie

In your code sample, page is of type PdfImportedPage. You're asking for its Bounding Box. PdfImportedPage is of type PdfTemplate, so you're asking for the Bounding Box of an XObject. I doubt that will work.

You should ask the reader object for the rotation of the page. I guess you've already discovered that yourself because you say "The given example works for PdfReader, but not for PdfCopy".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!