PDF merge – fast and with low overhead

XFINIUM.PDF 9.5 brings support for fast and low overhead PDF merge operations.

PDF merge was already available in XFINIUM.PDF through the PdfFixedDocument.AppendFile method.
Two or more PDF files can be merged like this:

PdfFixedDocument document = new PdfFixedDocument();
document.AppendFile("file1.pdf");
document.AppendFile("file2.pdf");
document.Save("merged.pdf");

This works fine in all situations but it can add an overhead with large PDF files because each file is parsed into the XFINIUM.PDF high level object model. The advantage is that additional operations can be performed on the merged file: bookmarks can be added for each file, pages can be watermarked, etc.

XFINIUM.PDF 9.5 brings a new method for PDF merge operations through the PdfMerger class. This utility class is specialized only in PDF merge operations, it loads and merges PDF files at a very low level without creating a full high level XFINIUM.PDF object model. The drawback is that PDF files can only be merged and that’s all, no other operations are possible.

The simplest way to merge 2 or more PDF files is to use the static method MergeFiles:

PdfMerger.MergeFiles("merged.pdf", "file1.pdf", "file2.pdf");

The same method also accepts streams:

PdfMerger.MergeFiles(mergedStream, inputStream1, inputStream2);

With the above methods the PDF files must not be encrypted or encrypted with no password.
If the files to be merged require a password to be opened, then they can be merged like this:

PdfMerger merger = new PdfMerger();
merger.AppendFile("file1.pdf", "password");
merger.AppendFile("file2.pdf", "password");
merger.Save("merged.pdf");

The merged file can also be encrypted by providing a security handler object as parameter to the Save method.

PdfMerger merger = new PdfMerger();
merger.AppendFile("file1.pdf", "password");
merger.AppendFile("file2.pdf", "password");

PdfAesSecurityHandler aes = new PdfAesSecurityHandler();
aes.KeySize = 256;
aes.UserPassword = "pdfmerger";
merger.Save("merged.pdf", aes); 

All methods that accepts paths as parameters have overloads that accept streams as parameters.

An evaluation version of XFINIUM.PDF library can be downloaded and tried without time limitation.

2 thoughts on “PDF merge – fast and with low overhead”

  1. Is it possible to merge two PDFs having just one line of text into a single PDF with one page?
    e.g.
    PDF 1 – Page 1 – Text 1
    PDF 2 – Page 1 – Text 2

    Merged PDF – Page 1 –
    Text 1
    Text 2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: