Centering an pdfimportedpage in iTextSharp

假装没事ソ 提交于 2019-12-05 05:12:33

问题


I am appending PDFs together using the function below via iTextSharp. Its working fine. The only problem is that PDFs that are larger than the set size of the document (A4), ends up being scaled and placed at the bottom left corner of the document. I would like to centre it. Can anyone point me in the right direction to achieving this? Cheers.

    private void appendPDF(appendDoc doc)
    {
        PdfContentByte pdfContentByte = pdfWriter.DirectContent;
        PdfReader pdfReader = null;

        if (doc.MemoryStream != null && doc.MemoryStream.CanRead)
        {
            pdfReader = new PdfReader(doc.MemoryStream);
        }
        else if (File.Exists(doc.FullFilePath))
        {
            pdfReader = new PdfReader(doc.FullFilePath);
        }

        if (pdfReader != null)
        {
            for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
            {                    
                PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, pageIndex);

                float importedPageXYRatio = importedPage.Width / importedPage.Height;

                if (XYRatio > 1f)
                {
                   iTextDocument.SetPageSize(PageSize.A4.Rotate());
                }
                else
                {
                   iTextDocument.SetPageSize(PageSize.A4);
                }

                iTextDocument.NewPage();
                pdfContentByte.AddTemplate(importedPage, 0, 0);

            }
        }
    }

Edit:

This was the solution I ended up using.

private void appendPDF(appendDoc doc)
    {
        PdfContentByte pdfContentByte = pdfWriter.DirectContent;
        PdfReader pdfReader = null;

        if (doc.MemoryStream != null && doc.MemoryStream.CanRead)
        {
            pdfReader = new PdfReader(doc.MemoryStream);
        }
        else if (File.Exists(doc.FullFilePath))
        {
            pdfReader = new PdfReader(doc.FullFilePath);
        }

        if (pdfReader != null)
        {
            for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
            {                    
                PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, pageIndex);

                float importedPageXYRatio = importedPage.Width / importedPage.Height;

                if (XYRatio > 1f)
                {
                   iTextDocument.SetPageSize(PageSize.A4.Rotate());
                }
                else
                {
                   iTextDocument.SetPageSize(PageSize.A4);
                }                  

                iTextDocument.NewPage();

                var truePageWidth = iTextDocument.PageSize.Width - iTextDocument.LeftMargin - iTextDocument.RightMargin;
                var truePageHeight = iTextDocument.PageSize.Height - iTextDocument.TopMargin - iTextDocument.BottomMargin;

                var x = (truePageWidth - importedPage.Width) / 2 + iTextDocument.RightMargin;
                var y = (truePageHeight - importedPage.Height) / 2 + iTextDocument.BottomMargin;

                pdfContentByte.AddTemplate(importedPage, x, y);                    
            }
        }
    }

回答1:


Can you set the x-coordinate when you call AddTemplate?

Float offset = 0;
if(importedPage.width < iTextDocument.PageSize.Width) {
   offset = (iTextDocument.PageSize.Width - importedPage.width)/2;
}
pdfContentByte.AddTemplate(importedPage, offset, 0);

Or does it do the scaling in AddTemplate so you don't know the final width?



来源:https://stackoverflow.com/questions/1905433/centering-an-pdfimportedpage-in-itextsharp

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