简体   繁体   English

并排合并2个Pdfs

[英]Combining 2 Pdfs side by side

I have 2 Pdfs, not always the same number of pages. 我有2个Pdf,但页数并不总是相同。 Would it be possible to combine the 2 files side by side into 1. By that I mean page 1 from both pdfs would go on the same page, page 2, and on. 是否有可能将2个文件并排合并为1个文件。也就是说,两个pdf文件的第1页将位于同一页,第2页及以后。 If one of the Pdfs aren't long enough, I plan to leave that side of the combined Pdf blank. 如果其中一个Pdf不够长,我计划将合并的Pdf的那一侧留空。

I've been looking into libraries such as iTextSharp but haven't had any luck. 我一直在研究诸如iTextSharp之类的库,但没有运气。 The preferred language to do this in if possible is C#. 如果可能的话,首选的语言是C#。 The output might not even need to be a Pdf, an image may suffice. 输出甚至可能不需要是Pdf,图像就足够了。 Thank you. 谢谢。

The code below shows how to combine 2 PDF files side by side using XFINIUM.PDF library: 下面的代码显示了如何使用XFINIUM.PDF库并排合并两个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");

Disclaimer: I work for the company that develops this library. 免责声明:我为开发此库的公司工作。

You can create so-called Form XObject from each page and then draw these XObjects on a page side by side. 您可以从每个页面创建所谓的Form XObject,然后在页面上并排绘制这些XObject。

This is possible with help of Docotic.Pdf library . 这可以在Docotic.Pdf库的帮助下进行 Here is a sample that shows how to put two pages side by side on a page in other document. 这是一个示例,该示例演示如何将两个页面并排放置在其他文档的页面上。

Create XObject from page 从页面创建XObject

The sample uses two pages from the same document and also scales them. 该示例使用来自同一文档的两个页面,并对其进行缩放。 You probably don't need to scale pages. 您可能不需要缩放页面。 Anyway, the sample should give you some info for a start. 无论如何,该示例应为您提供一些入门信息。

Disclaimer: I work for the company that develops Docotic.Pdf library. 免责声明:我为开发Docotic.Pdf库的公司工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM