在iTextSharp中居中pdfimported页面 [英] Centering an pdfimportedpage in iTextSharp

查看:479
本文介绍了在iTextSharp中居中pdfimported页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过iTextSharp使用以下功能将PDF附加在一起。它的工作正常。唯一的问题是,大于文档(A4)的设置大小的PDF最终会缩放并放置在文档的左下角。我想把它放在中心位置。有人能指出我正确的方向来实现这一目标吗?干杯。

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

            }
        }
    }

编辑:

这是我最终使用的解决方案。

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


推荐答案

你可以在调用AddTemplate时设置x坐标吗?

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

或者它是否在AddTemplate中进行缩放,因此您不知道最终宽度?

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

这篇关于在iTextSharp中居中pdfimported页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆