Syncfusion Feedback


Trusted by the world’s leading companies

Overview

The Syncfusion .NET PDF library makes it easy to merge or combine multiple PDF files into a single PDF document in C#. The process is simple and efficient, and the merged PDF files retain their original formatting, layout, annotations, bookmarks, form fields, and appearance.

The merge PDF feature works seamlessly on these platforms: WinForms, WPF, Blazor, .NET MAUI, .NET Core, WinUI, Xamarin, and UWP.

.NET merge PDF


How to merge PDF files in C#

  • Install Syncfusion.Pdf.Net.Core NuGet package in your project.
  • Create a PdfDocument object.
  • Create two FileStream objects to represent the two input PDF files.
  • Call the PdfDocumentBase.Merge() method to merge the two input PDF files into the PdfDocument object.
  • Save the PdfDocument object to the MemoryStream object.

Here is an example of how to merge PDF files in C# using the Syncfusion .NET PDF library. You can merge multiple PDF files into a single PDF document with just a few lines of code.

  1. //Create a PDF document
  2. using (PdfDocument finalDocument = new PdfDocument())
  3. {
  4. //Get the stream from an existing PDF document
  5. using (FileStream firstStream = new FileStream("File1.pdf", FileMode.Open, FileAccess.Read))
  6. using (FileStream secondStream = new FileStream("File2.pdf", FileMode.Open, FileAccess.Read))
  7. {
  8. //Create a PDF stream for merging
  9. Stream[] streams = { firstStream, secondStream };
  10. //Merge PDF documents
  11. PdfDocumentBase.Merge(finalDocument, streams);
  12. //Save the document into a stream
  13. using (MemoryStream outputStream = new MemoryStream())
  14. {
  15. finalDocument.Save(outputStream);
  16. }
  17. }
  18. }

PDF - Merge options

Merge specific range of pages from different PDF documents

Merging specific ranges of pages from different PDF documents is a quick and easy way to combine different parts of documents into a single PDF document.

  1. //Create a new PDF document
  2. using (PdfDocument finalDocument = new PdfDocument())
  3. {
  4. //Open the first PDF file
  5. using (FileStream firstStream = new FileStream("File1.pdf", FileMode.Open, FileAccess.Read))
  6. {
  7. //Load the first PDF document
  8. PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstStream);
  9. //Import a range of pages from the first PDF document into the final document
  10. //The range starts at page 2 and ends at the last page
  11. finalDocument.ImportPageRange(loadedDocument1, 1, loadedDocument1.Pages.Count - 1);
  12. //Open the second PDF file
  13. using (FileStream secondStream = new FileStream("File2.pdf", FileMode.Open, FileAccess.Read))
  14. {
  15. //Load the second PDF document
  16. PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondStream);
  17. //Import a range of pages from the second PDF document into the final document.
  18. //The range starts at page 2 and ends at page 5
  19. finalDocument.ImportPageRange(loadedDocument2, 2, 5);
  20. //Save the final document into a memory stream
  21. using (MemoryStream outputStream = new MemoryStream())
  22. {
  23. finalDocument.Save(outputStream);
  24. }
  25. }
  26. }
  27. }

Extend margin

Extend the margin of PDF pages while merging PDF documents.

  1. //Create a new PDF document
  2. using (PdfDocument outputDocument = new PdfDocument())
  3. {
  4. //Create a new instance for the document margin
  5. PdfMargins documentMargins = new PdfMargins();
  6. //Set the margin to 50 points on all sides
  7. documentMargins.All = 50;
  8. //Set the document margins
  9. outputDocument.PageSettings.Margins = documentMargins;
  10. //Load the first PDF document
  11. using (FileStream firstPDFStream = new FileStream("File1.pdf", FileMode.Open, FileAccess.Read))
  12. {
  13. //Load the second PDF document
  14. using (FileStream secondPDFStream = new FileStream("File2.pdf", FileMode.Open, FileAccess.Read))
  15. {
  16. //Create a list of streams to merge
  17. Stream[] streams = { firstPDFStream, secondPDFStream };
  18. //Create a merge options object
  19. PdfMergeOptions mergeOptions = new PdfMergeOptions();
  20. //Enable the extend margin option
  21. mergeOptions.ExtendMargin = true;
  22. //Merge the PDF documents
  23. PdfDocumentBase.Merge(outputDocument, mergeOptions, streams);
  24. //Save the document to a memory stream
  25. using (MemoryStream outputMemoryStream = new MemoryStream())
  26. {
  27. outputDocument.Save(outputMemoryStream);
  28. }
  29. }
  30. }
  31. }

Optimize PDF resources

Optimize PDF resources when merging multiple PDF documents into a single PDF document.

  1. //Create a new PDF document
  2. using (PdfDocument outputDocument = new PdfDocument())
  3. {
  4. //Load the first PDF document
  5. using (FileStream firstPDFStream = new FileStream("File1.pdf", FileMode.Open, FileAccess.Read))
  6. {
  7. //Load the second PDF document
  8. using (FileStream secondPDFStream = new FileStream("File2.pdf", FileMode.Open, FileAccess.Read))
  9. {
  10. //Create a list of streams to merge
  11. Stream[] streams = { firstPDFStream, secondPDFStream };
  12. //Create a merge options object
  13. PdfMergeOptions mergeOptions = new PdfMergeOptions();
  14. //Enable the optimize resources option
  15. mergeOptions.OptimizeResources = true;
  16. //Merge the PDF documents
  17. PdfDocumentBase.Merge(outputDocument, mergeOptions, streams);
  18. //Save the document to a memory stream
  19. using (MemoryStream outputMemoryStream = new MemoryStream())
  20. {
  21. outputDocument.Save(outputMemoryStream);
  22. }
  23. }
  24. }
  25. }

Add bookmarks to merged PDF documents

When merging multiple PDF files, users can create bookmarks based on the titles of individual files. This will help quickly navigate to specific sections of the merged file.

  1. //Create a PDF document
  2. using (PdfDocument finalDocument = new PdfDocument())
  3. {
  4. //Load the first PDF document
  5. using (FileStream firstStream = new FileStream("File1.pdf", FileMode.Open, FileAccess.Read))
  6. //Load the second PDF document
  7. using (FileStream secondStream = new FileStream("File2.pdf", FileMode.Open, FileAccess.Read))
  8. {
  9. //Create a list of streams to merge
  10. Stream[] streams = { firstStream, secondStream };
  11. //Merge PDF documents
  12. PdfDocumentBase.Merge(finalDocument, streams);
  13. //Create a bookmark for the first PDF file
  14. PdfBookmark bookmark1 = finalDocument.Bookmarks.Add("Chapter 1 - Barcodes");
  15. //Set the destination page for the first bookmark
  16. bookmark1.Destination = new PdfDestination(finalDocument.Pages[0]);
  17. //Set the destination for the first bookmark
  18. bookmark1.Destination.Location = new PointF(20, 20);
  19. //Create a bookmark for the second PDF file
  20. PdfBookmark bookmark2 = finalDocument.Bookmarks.Add("Chapter 2 - HTTP Succinctly");
  21. //Set the destination page for the second bookmark
  22. bookmark2.Destination = new PdfDestination(finalDocument.Pages[3]);
  23. //Set the destination for the second bookmark
  24. bookmark2.Destination.Location = new PointF(20, 20);
  25. // Save the document into a stream
  26. using (MemoryStream outputStream = new MemoryStream())
  27. {
  28. finalDocument.Save(outputStream);
  29. }
  30. }
  31. }




Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion® is proud to hold the following industry awards.

Scroll up icon
Chat with us