itext merge pdf

May 8, 2025 9 min read

iText is a powerful and versatile open-source library that allows developers to create, manipulate, and process PDF documents programmatically. It offers extensive capabilities for PDF manipulation, including merging, splitting, and editing. This article will delve into the specifics of using iText for merging PDF files, covering the essential concepts, code examples, and troubleshooting tips. Before we get started, consider exploring how BreezePDF can provide a more straightforward alternative for PDF merging, especially for simpler tasks that don't require extensive coding.

Merge PDFs Effortlessly with BreezePDF

Create fillable PDFs for free, merging them securely in your browser. No sign up is required!

Merge PDFs Now →

PDF merging is a critical function in various document management workflows, enabling users to combine multiple PDF documents into a single, cohesive file. This process is essential for compiling reports, creating comprehensive presentations, and assembling document packages. However, merging PDFs using code libraries like iText can often be complex and time-consuming. BreezePDF provides a user-friendly and efficient solution for PDF merging, offering a simpler alternative for users seeking a no-code approach. If you need to add a signature, please check out how to add a signature to a fillable PDF with BreezePDF.

BreezePDF offers a streamlined, intuitive approach to merging PDFs, making it accessible to users of all technical skill levels. Without the need for coding or complex configurations, BreezePDF allows you to quickly and easily combine multiple PDF files into a single, well-organized document. This can save significant time and effort compared to the more technical iText library, especially when dealing with basic PDF merging tasks.

II. Understanding PDF Merging with iText

PDF merging is the process of combining two or more PDF documents into a single PDF file. This is often needed for various reasons, such as consolidating multiple reports into one comprehensive document. Another common use case involves compiling various presentation slides or assembling different components of a document package. PDF merging simplifies document management and improves organization by creating a unified, accessible resource.

There are two main approaches to PDF merging using iText. The modern approach (iText 7 and later) utilizes the PdfMerger class, which provides a streamlined and efficient way to combine PDF files. Alternatively, a more manual approach involves copying pages from source documents and inserting them into a destination document. While the manual approach offers more granular control, it can be more complex and time-consuming than using the PdfMerger class.

III. iText Versions and Dependency Setup

When working with iText, it's essential to choose the appropriate version for your project. iText 7 and iText 8 are the current supported versions, offering enhanced features and improved performance. It's crucial to avoid using iText 5, as it is outdated and no longer maintained. While iText 7 and iText 8 both serve the same core function, iText 8 might offer some performance enhancements. However, this performance enhancement may be negligible depending on your use case.

To use iText in your project, you need to add the iText dependencies to your project's build configuration. For Maven projects, this involves adding the iText dependency to your pom.xml file. For .NET projects, you can add the iTextSharp NuGet package. Proper dependency setup ensures that your project has access to the necessary iText libraries and functionalities. Adding these dependencies is crucial to successfully implement PDF merging.

IV. Core Concepts of iText PDF Merging

Several key classes are involved in the iText PDF merging process. The PdfDocument class represents a PDF document, while PdfWriter is used to write content to a PDF document. PdfReader is used to read content from an existing PDF document. The PdfMerger class is specifically designed for merging PDF documents. The PdfPageFormCopier, PdfCanvas, and PdfOutline classes are also essential for advanced merging techniques, enabling more fine-grained control over the merging process.

The basic PDF merging process involves several steps. First, open the destination PDF for writing using PdfWriter. Attach the opened document to a PdfMerger object. For each source document, open the source document using PdfReader, and then use the merge() method of the PdfMerger object to merge it into the destination document. This process repeats for each source document, creating a single merged PDF file.

V. Code Examples for iText PDF Merging

The following sections provide code examples for iText PDF merging in C#/.NET and Java, illustrating the practical implementation of the concepts discussed earlier. These examples cover simple PDF merging, merging using PdfMerger, merging as byte arrays, merging with specific page ranges, controlling source document closure, and merging documents with a table of contents. These examples will provide a strong understanding of how to use iText for merging PDF files.

A. Simple PDF merging (C#/.NET)

(Code example would be inserted here)

B. Simple PDF merging (Java)

  1. Prepare input PDF file list as a list of input streams.
  2. Prepare output stream for the merged PDF file.
  3. Create document and PdfReader objects.
  4. Create PdfIterator object using inputPdfList.
  5. Create a reader list for the input PDF files.
  6. Create a writer for the outputStream.
  7. Open the document.
  8. Get PdfContentByte instance from the writer object.
  9. Iterate and process the reader list.
  10. Create page and add content.
  11. Close the document and outputStream.

C. Merging PDFs using PdfMerger (iText 7+)

(Code example would be inserted here)

D. Merging PDFs as byte arrays (C#)

(Code example would be inserted here)

E. Merging with specific page ranges (iText 7+)

PdfMerger Merge(PdfDocument from, int fromPage, int toPage)

PdfMerger Merge(PdfDocument from, IList<int> pages)

F. Controlling source document closure

pdfMerger.SetCloseSourceDocuments(true);

G. Merging documents and create a Table of Contents

(Code example would be inserted here)

VI. Handling Different Page Sizes

When merging PDFs with different page sizes, iText's default behavior is to retain the original page sizes of each document. This means that the merged PDF may contain pages with varying dimensions, which can sometimes lead to visual inconsistencies. To address this, you may need to resize pages during the merging process to ensure uniformity and a more polished final document.

In C#/.NET, resizing pages during merging can be achieved using PdfCanvas and PdfFormXObject. This involves creating a new page with the desired dimensions, scaling the content of the original page to fit the new page, and then adding the scaled content to the new page. This ensures that all pages in the merged PDF have the same size, regardless of their original dimensions. For example, you can resize every page to A5 format. BreezePDF does not require this code and handles page sizes seamlessly.

(Code example for resizing to A5 would be inserted here)

VII. Advanced Techniques and Considerations

Advanced PDF merging techniques involve adding page numbers during the merge process, ensuring PDF/A compliance, and handling PDFs that lack headers or footers. Adding page numbers can improve document navigation and organization. PDF/A compliance ensures that the merged PDF is suitable for long-term archiving. Properly handling PDFs without headers or footers can maintain a consistent appearance throughout the merged document.

Each of these advanced techniques requires careful consideration and implementation to ensure the final merged PDF meets the desired requirements. Adding page numbers typically involves creating a custom header or footer with dynamic page number insertion. Ensuring PDF/A compliance involves adhering to specific PDF/A standards during the merging process. Proper header and footer handling may require custom code to add or modify headers and footers across all merged documents.

VIII. Troubleshooting Common Issues

Several common issues can arise during PDF merging with iText. PDF documents may get cut off during the merge process, often due to differing page sizes or incorrect scaling. Whitespace issues can occur after merging, leading to inconsistent spacing between elements. General PDF exceptions can also occur, indicating underlying problems with the PDF documents being merged. Detecting and adjusting page sizes appropriately can be the start to troubleshooting merge errors.

To address these issues, you may need to detect and adjust page sizes, correct whitespace problems, and handle PDF exceptions gracefully. Detecting and adjusting page sizes often involves analyzing the dimensions of each PDF document and scaling the content as needed. Correcting whitespace problems may require adjusting margins and padding. Handling PDF exceptions involves implementing error handling mechanisms to catch and resolve potential issues during the merging process. You can always avoid these issues by using BreezePDF.

IX. BreezePDF: A Simplified Approach to PDF Merging

BreezePDF offers a simplified approach to PDF merging, serving as an excellent alternative to iText for straightforward merging tasks. With its user-friendly interface, BreezePDF simplifies the PDF merging process, making it accessible to users who prefer a no-code solution. Unlike iText, which requires coding and technical expertise, BreezePDF provides an intuitive, drag-and-drop interface that allows you to quickly and easily combine PDF files.

BreezePDF's ease of use and intuitive interface makes it an ideal choice for users who want to merge PDF files without the complexity of coding. You can simply upload the PDF files you want to merge, arrange them in the desired order, and then click a button to create a single, merged PDF document. This streamlined process saves time and effort compared to the more technical iText library, making BreezePDF a convenient option for everyday PDF merging tasks.

For users who prioritize simplicity and ease of use over advanced customization options, BreezePDF offers significant advantages over iText. By eliminating the need for coding, BreezePDF empowers users of all technical skill levels to efficiently merge PDF files and streamline their document management workflows. This makes BreezePDF a valuable tool for anyone seeking a hassle-free PDF merging solution. You can even quickly create a fillable PDF with BreezePDF.

X. Conclusion

This article explored various iText PDF merging techniques, providing code examples and troubleshooting tips. While iText offers extensive capabilities for PDF manipulation, it can be complex and time-consuming for users without coding experience. For simple merging tasks, BreezePDF presents a user-friendly alternative that simplifies the PDF merging process, eliminating the need for coding and technical expertise.

When choosing the right PDF merging solution for your needs, consider the complexity of your merging tasks, your technical skills, and your time constraints. If you require advanced customization options and have coding expertise, iText may be the preferred choice. However, if you prioritize simplicity, ease of use, and a no-code approach, BreezePDF offers a more convenient and efficient solution for merging PDF files.

Ultimately, the best PDF merging solution depends on your specific requirements and preferences. By understanding the capabilities of both iText and BreezePDF, you can make an informed decision and choose the tool that best aligns with your needs and goals, making PDF merging a breeze.