【问题标题】:Combining 2 Pdfs side by side并排结合2个Pdfs
【发布时间】:2016-08-14 22:01:03
【问题描述】:

我有 2 个 Pdf,但页数并不总是相同。是否可以将这两个文件并排合并为 1。我的意思是两个 pdf 的第 1 页将在同一页上,第 2 页上,等等。如果其中一个 Pdf 不够长,我打算将合并的 Pdf 的那一侧留空。

我一直在研究 iTextSharp 等库,但没有任何运气。如果可能,首选的语言是 C#。输出甚至可能不需要是 Pdf,图像可能就足够了。谢谢你。

【问题讨论】:

  • 这应该可以使用任何通用 PDF 库。尤其是 iTextSharp 是可能的。

标签: c# pdf merge


【解决方案1】:

您可以从每个页面创建所谓的表单 XObject,然后将这些 XObject 并排绘制在一个页面上。

这可以在Docotic.Pdf library 的帮助下实现。这是一个示例,展示了如何将两个页面并排放置在其他文档的页面上。

Create XObject from page

该示例使用同一文档中的两个页面并对其进行缩放。您可能不需要缩放页面。无论如何,该示例应该为您提供一些信息。

免责声明:我为开发 Docotic.Pdf 库的公司工作。

【讨论】:

    【解决方案2】:

    下面的代码展示了如何使用XFINIUM.PDF库并排合并2个PDF文件:

    FileStream input1 = File.OpenRead("input1.pdf");
    PdfFile file1 = new PdfFile(input1);
    PdfPageContent[] fileContent1 = file1.ExtractPageContent(0, file1.PageCount - 1);
    file1 = null;
    input1.Close();
    
    FileStream input2 = File.OpenRead("input2.pdf");
    PdfFile file2 = new PdfFile(input2);
    PdfPageContent[] fileContent2 = file2.ExtractPageContent(0, file2.PageCount - 1);
    file2 = null;
    input2.Close();
    
    PdfFixedDocument document = new PdfFixedDocument();
    
    int maxPageCount = Math.Max(fileContent1.Length, fileContent2.Length);
    
    for (int i = 0; i < maxPageCount; i++)
    {
        PdfPage page = document.Pages.Add();
        // Make the destination page landscape so that 2 portrait pages fit side by side
        page.Rotation = 90;
    
        if (i < fileContent1.Length)
        {
            // Draw the first file in the first half of the page
            page.Graphics.DrawFormXObject(fileContent1[i], 0, 0, page.Width / 2, page.Height);
        }
        if (i < fileContent2.Length)
        {
            // Draw the second file in the second half (x coordinate is half page) of the page
            page.Graphics.DrawFormXObject(fileContent2[i], page.Width / 2, 0, page.Width / 2, page.Height);
        }
    }
    
    document.Save("SideBySide.pdf");
    

    免责声明:我为开发此库的公司工作。

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2013-08-21
      • 2011-05-17
      • 2014-10-16
      相关资源
      最近更新 更多