How to Merge Two or More PDFs in .NET

Have you ever come to the point where you want to merge two PDFs? Perhaps you need to attach a supporting document at the end of a form. Or maybe you need to batch append resumes with cover letters. Whatever your use case, it should be simple and automatable. So in this blog post, we’re going to show just how simple it can be to merge two or more PDFs, and then we’ll show you some extra little tricks to customize a merged document further.

.NET

.NET appears in many situations and environments, from the server to the desktop, and even on mobile. Having one solution in all these environments makes it easy for developers to switch between projects and jobs. Looking to the future with .NET 5 and the unification of the frameworks, switching jobs should be even easier.

Merging Example

Let’s say we’ve got two documents, each consisting of one page each. We want to merge these two pages together to create a document with one page.

Copy
1
2
3
4
5
6
7
// Get two documents and merge them together. 0 denotes the page index of where to insert the document.
var documentEditor = new DocumentEditor();
documentEditor.ImportDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider("Assets/dog.pdf"));
documentEditor.ImportDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider("Assets/cat.pdf"));

// Write out to a new file.
documentEditor.SaveDocument(new FileDataProvider("dogCatPair.pdf"));

With just four lines of code, it’s possible to merge the two documents together and write out to the target file.

The code above shows the use of the powerful PSPDFKit Document Editor. The importDocument method indicates where to place the document, with 0 denoting index 0 and DocumentEditor.IndexPosition.BeforeIndex instructing the editor to place the pages before the given index (this is necessary for the first document, as there are no other indices to reference yet). The last parameter instructs the editor where to obtain the document from. In the code example above, we take a simple file path, but it’s also possible to pass a custom data provider that extends IDataProvider to provide data from any source you require, such as memory, network data, or even a cryptographic solution.

It’s possible to stack as many of these operations together as needed. One factor to be aware of is the size of the PDFs in memory. Opening and appending PDFs can be a memory-intensive process, so if you are importing large documents, keep this in mind.

Extra Operations

Now that we have our two documents merged, let’s have a quick overview of what else we can do with the Document Editor.

Let’s say we want to make flip cards now. We have a page on one side, and if we flip it on the horizontal edge, we have another page, but in the correct readable orientation. With the previous example, if we did this, the second page would be upside-down.

We can fix this.

Copy
1
2
3
4
5
documentEditor.ImportDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider("Assets/dog.pdf"));
documentEditor.ImportDocument(0, DocumentEditor.IndexPosition.BeforeIndex, new FileDataProvider("Assets/cat.pdf"));

// Now flip the second page.
documentEditor.RotatePages(new List<int>{1}, Rotation.Degrees180);

We take the second page and flip it 180 degrees. Now, if we’re looking at the first page and flip it along the horizontal edge, the second page will appear in the correct orientation! We’ve made flip cards.

RotatePages offers the opportunity to rotate multiple pages at one time in increments of 90 degrees. And to make things simple, the Rotation enum offers positive and negative values.

From here, it’s easy to explore additional possibilities — including but not limited to AddPage, RemovePage, and SetPageLabel — in order to create the final PDF you need.

Conclusion

Now you should know how to achieve document merging with the PSPDFKit .NET Library, and you should also have learned how extra operations can be applied to the newly created document. Feel free to explore the guides and API documentation for more examples and other important features of the SDK.

If you’d like to try out the PSPDFKit .NET Library for yourself, head over to the trial page and download the SDK today.

PSPDFKit Library for Java/.NET

Download the free 60-day trial and integrate it with your web app today.

The data you submit is treated confidentially and will never be disclosed to third parties.
We may occasionally send you product news and important announcements.