并排组合2个Pdfs

时间:2016-04-21 15:24:34

标签: c# pdf merge

我有2个Pdfs,并不总是相同的页数。是否可以将2个文件并排组合成1.我的意思是来自两个pdf的第1页将在同一页面上,第2页,然后打开。如果其中一个Pdfs不够长,我打算留下合并的Pdf空白的那一面。

我一直在寻找像iTextSharp这样的图书馆,但没有任何运气。如果可能,首选语言是C#。输出甚至可能不需要是Pdf,图像可能就足够了。谢谢。

2 个答案:

答案 0 :(得分:1)

下面的代码显示了如何使用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");

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

答案 1 :(得分:0)

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

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

Create XObject from page

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

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

相关问题