peacefullearner
peacefullearner

Reputation: 81

Combining 2 Pdfs side by side

I have 2 Pdfs, not always the same number of pages. 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. If one of the Pdfs aren’t long enough, I plan to leave that side of the combined Pdf blank.

I’ve been looking into libraries such as iTextSharp but haven’t had any luck. The preferred language to do this in if possible is C#. The output might not even need to be a Pdf, an image may suffice. Thank you.

Upvotes: 1

Views: 2068

Answers (2)

Bobrovsky
Bobrovsky

Reputation: 14236

You can create so-called Form XObject from each page and then draw these XObjects on a page side by side.

This is possible with help of Docotic.Pdf library. Here is a sample that shows how to put two pages side by side on a page in other document.

Create XObject from page

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.

Upvotes: 0

Mihai Iancu
Mihai Iancu

Reputation: 1828

The code below shows how to combine 2 PDF files side by side using XFINIUM.PDF library:

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.

Upvotes: 1

Related Questions