【问题标题】:Combine content from multiple pages into one page using itext 7使用 itext 7 将多​​个页面的内容合并到一个页面中
【发布时间】:2018-02-02 19:14:24
【问题描述】:

我有一份 pdf 保险文件,现在每个条款都在单独的页面上。我想获取包含短子句的页面并将它们与其他页面合并到一个页面中。

我在这里传递代表 2 页的文件。我试图了解如何使用一些测试代码来做到这一点的机制。

我想将这两个页面读入一个新的目标页面并返回。

我不想更改原始内容的比例,但如果我不使用 ConcatMatrix 进行转换,那么它根本不会显示任何内容。

在这里我可以在一页中看到两个页面的内容,但是它们太小了。我只是在将参数放入 AddXObject 中。参数取 a,b,c,d 等,那么你怎么知道哪些是 x,y,height,width..?

public byte[] ManipulatePdf(byte[] file)
    {
        byte[] result;

        using (var ms = new MemoryStream())
        {
            PdfDocument pdf = new PdfDocument(new PdfWriter(ms));
            PdfDocument sourcePdf = new PdfDocument(new PdfReader(new MemoryStream(file)));                
            var pages = sourcePdf.GetNumberOfPages();                
            PageSize nUpPageSize = PageSize.A4;
            PdfPage targetPage = pdf.AddNewPage(nUpPageSize);
            PdfCanvas canvas = new PdfCanvas(targetPage);                

            //if we remove ConcatMatrix we get nothing
            //otherwise it appears too small
            for (int i = 1; i <= pages; i++)
            {
                PdfPage currentPage = sourcePdf.GetPage(i);
                Rectangle orig = targetPage.GetPageSize();
                var width = nUpPageSize.GetWidth() / orig.GetWidth() / 2f;
                var height = nUpPageSize.GetHeight() / orig.GetHeight() / 2f;
                AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(
                width, height);
                canvas.ConcatMatrix(transformationMatrix);

                var height1 = orig.GetHeight();
                var width2 = orig.GetWidth() / 2;
                var height2 = orig.GetHeight() / 2;

                PdfFormXObject pageCopy = currentPage.CopyAsFormXObject(pdf);
                if (i == 1)
                    canvas.AddXObject(pageCopy, 0, height1);                    
                else
                    canvas.AddXObject(pageCopy,0, 1200);

            }                
            // close the documents
            pdf.Close();
            sourcePdf.Close();
            canvas.Release();
            result = ms.GetBuffer();
        }


        return result;
    }

【问题讨论】:

  • AddXObject 调用在此处将它们各自的pageCopy 添加到页面上边框之外。

标签: c# .net itext


【解决方案1】:

这是我的解决方案,我不想缩小它,所以我使用 0.99 来保持原始大小,如果我使用 1 则它根本不会出现。

第二页的 y 位置使用负值。如果我使用 +400 则它从页面上消失,0 与第一页内容重叠,但负值继续向下移动。 :)

 //if we remove ConcatMatrix we get nothing
            //otherwise it appears to small
            //setting transformation to 1 shows nothing
            for (int i = 1; i <= pages; i++)
            {
                PdfPage currentPage = sourcePdf.GetPage(i);
                Rectangle orig = targetPage.GetPageSize();
                var width = 0.99f;//= nUpPageSize.GetWidth() / orig.GetWidth() / 2f;
                var height = 0.99f;//nUpPageSize.GetHeight() / orig.GetHeight() / 2f;
                AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(
                width, height);
                canvas.ConcatMatrix(transformationMatrix);

                var height1 = orig.GetHeight();
                var width2 = orig.GetWidth() / 2;
                var height2 = orig.GetHeight() / 2;

                PdfFormXObject pageCopy = currentPage.CopyAsFormXObject(pdf);
                if (i == 1)                                         
                    canvas.AddXObject(pageCopy, 0, 0); 
                else                      
                    canvas.AddXObject(pageCopy, 0, -400);

            }                

【讨论】:

  • “第二页的y位置是用负值”——嗯……显然,毕竟你想要第二页的内容在第一个,所以在您将第一页添加到 0 之后显然是负面的。
  • “如果我使用 1,那么它根本不会出现。” - 这让我非常惊讶。你能分享一个发生这种情况的示例源 PDF 吗?
猜你喜欢
  • 2019-10-30
  • 2022-12-03
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多