TL;DR: Study six efficient methods to merge PDFs in C# utilizing the Syncfusion PDF Library. Streamline your doc workflow, batch course of recordsdata, and mix a number of PDFs seamlessly.
In at this time’s digital age, PDFs have turn into the go-to format for sharing, archiving, and managing paperwork. Whether or not you’re dealing with enterprise reviews, authorized paperwork, or private recordsdata, merging a number of PDFs into one cohesive file is commonly important. Doing so simplifies group, enhances consistency, and makes it simpler to share paperwork professionally.
With the Syncfusion PDF Library, merging PDF recordsdata is simple and will be automated for batch processes, saving effort and time.
This weblog will cowl the next six environment friendly strategies to merge PDF recordsdata:
- Mix a number of PDF paperwork right into a single file.
- Merge chosen pages ranges from completely different PDF paperwork.
- Optimize sources throughout the PDF merging course of.
- Prolong the web page margins whereas merging PDF paperwork.
- Protect accessibility tags when merging PDF recordsdata.
- Add bookmarks to the merged PDF paperwork for simpler navigation.
Let’s get began!
Discover Now
Getting began with app creation
- First, create a .NET console app utilizing Visible Studio.
- Then, open Visible Studio and navigate to Instruments -> NuGet Package deal Supervisor -> Package deal Supervisor Console.
- Run the next command within the Package deal Supervisor Console to put in the Syncfusion.Pdf.Web.Core NuGet bundle.
Set up-Package deal Syncfusion.Pdf.Web.Core
Mix a number of PDF paperwork right into a single file
Combining a number of PDF paperwork right into a single file is a typical activity, particularly when consolidating reviews, kinds, or different paperwork into one unified PDF. This course of will be completed utilizing the Syncfusion PDF Library with just some strains of code.
On this instance, we’ll merge the next two PDF recordsdata, file1.pdf, and file2.pdf, positioned within the undertaking’s information folder.
file1.pdf
Steps to merge two PDF recordsdata
- Initialize a brand new occasion of the PdfDocument class. This object will maintain the merged PDF.
- Create two FileStream objects, every representing one of many enter PDF recordsdata. These streams will mean you can learn the contents of the PDF recordsdata.
- Name the PdfDocumentBase.Merge() technique, passing the PdfDocument object and the array of FileStream objects. This may mix the contents of the 2 enter PDF recordsdata into the PdfDocument object.
- Save the merged PdfDocument to a MemoryStream or FileStream object. On this step, the mixed PDF is written right into a file.
Check with the next code instance.
utilizing Syncfusion.Pdf; //Create a PDF doc. utilizing (PdfDocument finalDocument = new PdfDocument()) { //Get the stream from an current PDF doc. utilizing (FileStream firstStream = new FileStream("information/file1.pdf", FileMode.Open, FileAccess.Learn)) utilizing (FileStream secondStream = new FileStream("information/file2.pdf", FileMode.Open, FileAccess.Learn)) { //Create a stream array for merging. Stream[] streams = { firstStream, secondStream }; //Merge PDF paperwork. PdfDocumentBase.Merge(finalDocument, streams); //Save the doc. utilizing (FileStream outputStream = new FileStream("combine-multiple-pdf-files.pdf", FileMode.Create, FileAccess.ReadWrite)) { finalDocument.Save(outputStream); } } }
Whenever you execute the above code instance, you’ll acquire a single merged PDF doc because the output.
Merge the chosen web page ranges from completely different PDF paperwork
Merging chosen web page ranges from completely different PDF paperwork entails combining particular pages from a number of PDFs into one file. You may simply obtain this utilizing the Syncfusion PDF library by loading every PDF, choosing the specified web page ranges, and appending these pages to a brand new doc. This enables exact management over the pages that should be included within the remaining merged PDF.
On this instance, we’ll take pages 2 to five from file1.pdf and pages 1 to 2 from file2.pdf to merge these particular pages right into a single PDF file.
file1.pdf
Steps to merge chosen PDF pages vary from completely different PDF recordsdata
- First, initialize a brand new PdfDocument occasion to retailer the ultimate merged PDF file.
- Subsequent, create two FileStream objects to characterize the enter PDF recordsdata. These streams will mean you can entry and browse the contents of the PDFs.
- Load the primary enter PDF file (file1.pdf) utilizing a PdfLoadedDocument object, after which import the desired pages by calling the ImportPageRange technique.
- Repeat the method for the second PDF file (file2.pdf), importing the specified pages.
- Lastly, save the mixed PDF doc to a FileStream or MemoryStream. Check with the next code instance.
// Create a brand new PDF doc. utilizing PdfDocument finalDocument = new PdfDocument(); // Open and cargo the primary PDF file. utilizing FileStream firstStream = new FileStream("information/file1.pdf", FileMode.Open, FileAccess.Learn); PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstStream); // Import pages 2 to five from the primary PDF doc. finalDocument.ImportPageRange(loadedDocument1, 1, 5); // Open and cargo the second PDF file. utilizing FileStream secondStream = new FileStream("information/file2.pdf", FileMode.Open, FileAccess.Learn); PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondStream); // Import pages 1 to 2 from the second PDF doc. finalDocument.ImportPageRange(loadedDocument2, 0, 1); // Save the ultimate doc. utilizing FileStream outputStream = new FileStream("merge-selected-page-ranges.pdf", FileMode.Create, FileAccess.ReadWrite); finalDocument.Save(outputStream);
After working this system, you’re going to get a PDF file containing the merged web page ranges from each enter paperwork.
Learn Now
Optimize sources throughout the PDF merging course of
Optimizing sources throughout the PDF merging course of entails lowering file measurement and reminiscence utilization whereas sustaining the standard of the merged doc. With the Syncfusion PDF Library, you may streamline the merging course of by effectively managing sources similar to photos, fonts, and different belongings, guaranteeing that the ensuing PDF is compact and optimized for efficiency.
The OptimizeResources property in PdfMergeOptions is used to optimize PDF web page sources throughout the merging course of.
Steps to optimize sources throughout the PDF merging course of
- Begin by creating a brand new occasion of the PdfDocument class, which is able to maintain the merged and optimized PDF.
- Create FileStream objects for the enter PDF recordsdata to permit studying their contents throughout the merge course of.
- To optimize sources like photos and fonts throughout the merging course of, use the OptimizeResources property out there within the PdfMergeOptions class. This helps cut back file measurement and reminiscence utilization whereas preserving doc high quality.
- After merging, save the optimized PDF doc to a FileStream or MemoryStream. Check with the next code instance.
// Create a brand new PDF doc. utilizing (PdfDocument outputDocument = new PdfDocument()) { // Load the primary PDF doc. utilizing (FileStream firstPDFStream = new FileStream("information/file1.pdf", FileMode.Open, FileAccess.Learn)) { // Load the second PDF doc utilizing (FileStream secondPDFStream = new FileStream("information/file2.pdf", FileMode.Open, FileAccess.Learn)) { // Create a listing of streams to merge. Stream[] streams = { firstPDFStream, secondPDFStream }; // Create a merge choices object. PdfMergeOptions mergeOptions = new PdfMergeOptions(); // Allow the optimize sources choice. mergeOptions.OptimizeResources = true; // Merge the PDF paperwork with optimization. PdfDocumentBase.Merge(outputDocument, mergeOptions, streams); // Save the doc. utilizing (FileStream outputMemoryStream = new FileStream("optimize-resources-merge-pdf.pdf", FileMode.Create, FileAccess.ReadWrite)) { outputDocument.Save(outputMemoryStream); } } } }
After working the code, you’ll obtain a merged PDF file optimized for measurement, guaranteeing environment friendly useful resource utilization.
Be aware: The optimization course of is simplest when the PDF paperwork being merged comprise equivalent sources, similar to fonts, photos, and different embedded components. The optimization could not considerably cut back the file measurement if the sources differ between the PDFs.
Prolong the web page margins whereas merging PDF paperwork
Extending the web page margins whereas merging PDF paperwork lets you add further area across the content material of every web page within the remaining merged doc. Utilizing the Syncfusion PDF Library, you may alter the web page format throughout the merging course of, guaranteeing that the merged PDF has constant and expanded margins as wanted.
Steps to increase web page margins throughout PDF merging
- First, initialize a brand new PdfDocument object and set the web page margins to your required measurement. On this instance, we set the margins to 50 factors on all sides.
- Create FileStream objects for the enter PDF recordsdata to permit entry to their contents for merging.
- To increase the margins throughout the merging course of, permit the ExtendMargin choice within the PdfMergeOptions object. This ensures the ultimate merged doc applies the desired margin settings to all pages.
- Use the PdfDocumentBase.Merge technique to merge the enter PDF paperwork, making use of the margin settings from the PdfMergeOptions.
- Lastly, save the merged doc with prolonged margins to a FileStream or MemoryStream. Check with the next code instance.
// Create a brand new PDF doc with 50-point margins. utilizing PdfDocument outputDocument = new PdfDocument { PageSettings = { Margins = { All = 50 } } }; // Load the primary and second PDF paperwork. utilizing FileStream firstPDFStream = new FileStream("information/file1.pdf", FileMode.Open, FileAccess.Learn); utilizing FileStream secondPDFStream = new FileStream("information/file2.pdf", FileMode.Open, FileAccess.Learn); // Create a merge choices object and allow the margin extension. PdfMergeOptions mergeOptions = new PdfMergeOptions { ExtendMargin = true }; // Merge the PDF paperwork with prolonged margins. PdfDocumentBase.Merge(outputDocument, mergeOptions, new[] { firstPDFStream, secondPDFStream }); // Save the doc with prolonged margins. utilizing FileStream outputMemoryStream = new FileStream("extend-margins-while-merging-pdfs.pdf", FileMode.Create, FileAccess.ReadWrite); outputDocument.Save(outputMemoryStream);
After working this system, you’ll acquire a merged PDF with prolonged margins on every web page, offering constant further area across the content material.
Strive Now
Protect accessibility tags when merging PDF paperwork
Preserving accessibility tags when merging PDF recordsdata ensures that the merged doc retains vital metadata and construction, similar to headings, studying order, and various textual content for photos. These tags are essential for making PDFs accessible to customers with disabilities, enabling display screen readers and different assistive applied sciences to interpret the content material accurately.
Utilizing the Syncfusion PDF Library, you may merge PDF paperwork whereas sustaining these accessibility options, guaranteeing that the ensuing file stays absolutely accessible.
Steps to protect accessibility tags throughout PDF merging
- First, create a brand new PdfDocument occasion to carry the merged and accessible PDF doc.
- Arrange FileStream objects to load the PDF recordsdata you need to merge, guaranteeing these recordsdata comprise accessibility tags.
- Then, create a PdfMergeOptions object and allow the MergeAccessibilityTags property to make sure that accessibility tags (like studying order, headings, alt-text, and so on.) are maintained throughout the merge course of.
- Merge the loaded PDF paperwork utilizing the PdfDocumentBase.Merge technique whereas making use of the merge choices for accessibility.
- Lastly, save the merged PDF to a FileStream to make sure the doc is saved with all of the accessibility options intact. Check with the next code instance.
// Create a brand new PDF doc. utilizing (PdfDocument finalDoc = new PdfDocument()) { // Load the primary and second PDF paperwork. utilizing (FileStream stream1 = new FileStream("information/file1.pdf", FileMode.Open, FileAccess.Learn)) utilizing (FileStream stream2 = new FileStream("information/file2.pdf", FileMode.Open, FileAccess.Learn)) { // Create merge choices and allow accessibility tag merging. PdfMergeOptions mergeOptions = new PdfMergeOptions { MergeAccessibilityTags = true }; // Merge PDF paperwork with accessibility tags preserved. PdfDocumentBase.Merge(finalDoc, mergeOptions, new[] { stream1, stream2 }); // Save the merged doc with preserved accessibility tags. utilizing (FileStream outputFileStream = new FileStream("merge-tags.pdf", FileMode.Create, FileAccess.ReadWrite)) { finalDoc.Save(outputFileStream); } } }
After working this program, the ensuing merged PDF (merge-tags.pdf) will retain all accessibility options from the unique recordsdata, guaranteeing that the doc is absolutely accessible and will be navigated by assistive applied sciences similar to display screen readers.
Add bookmarks to the merged PDF paperwork for simpler navigation
Including bookmarks to the merged PDF paperwork enhances navigation by permitting customers to leap to particular sections or pages throughout the doc shortly. Bookmarks act as an interactive desk of contents, making it simpler to find key content material.
Utilizing the Syncfusion PDF Library, customers can create bookmarks primarily based on particular person file titles. This may assist shortly navigate to particular sections of the merged file.
Steps so as to add bookmarks to merged PDF paperwork
- First, create a brand new PdfDocument occasion to carry the merged PDF paperwork with bookmarks.
- Use FileStream objects to load the PDF recordsdata you need to merge. The loaded paperwork shall be merged, and bookmarks shall be added at the start of every doc.
- Merge the loaded PDF recordsdata utilizing the PdfDocumentBase.Merge technique to mix them into the ultimate doc.
- To enhance navigation, add bookmarks for every merged PDF doc. On this instance, bookmarks are added to characterize the beginning of every doc within the merged file.
- Lastly, save the merged doc with bookmarks to a FileStream, which ensures that the bookmarks will seem within the output PDF. Check with the next code instance.
// Create a brand new PDF doc. utilizing (PdfDocument finalDocument = new PdfDocument()) { // Load the primary and second PDF paperwork. utilizing (FileStream firstStream = new FileStream("information/file1.pdf", FileMode.Open, FileAccess.Learn)) utilizing (FileStream secondStream = new FileStream("information/file2.pdf", FileMode.Open, FileAccess.Learn)) { // Load the stream into PdfLoadedDocument class. utilizing PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstStream); utilizing PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondStream); // Merge the PDF paperwork. PdfDocumentBase.Merge(finalDocument, new[] { loadedDocument1, loadedDocument2 }); // Create a bookmark for the primary PDF file. PdfBookmark bookmark1 = finalDocument.Bookmarks.Add("Chapter 1 - Barcodes"); // Set the vacation spot web page for the primary bookmark. bookmark1.Vacation spot = new PdfDestination(finalDocument.Pages[0]); // Set the situation for the primary bookmark. bookmark1.Vacation spot.Location = new PointF(20, 20); // Create a bookmark for the second PDF file. PdfBookmark bookmark2 = finalDocument.Bookmarks.Add("Chapter 2 - HTTP Succinctly"); // Set the vacation spot web page for the second bookmark (after the pages of the primary PDF) bookmark2.Vacation spot = new PdfDestination(finalDocument.Pages[loadedDocument1.PageCount]); // Set the situation for the second bookmark. bookmark2.Vacation spot.Location = new PointF(20, 20); // Save the doc with bookmarks right into a stream. utilizing (FileStream outputFileStream = new FileStream("add-bookmarks.pdf", FileMode.Create, FileAccess.ReadWrite)) { finalDocument.Save(outputFileStream); } } }
Whenever you execute this code, the output shall be a merged PDF file named add-bookmarks.pdf. The doc will embrace bookmarks representing every appended PDF, enabling straightforward navigation via the content material utilizing a bookmark panel within the PDF viewer.
GitHub reference
For extra particulars, seek advice from the 6 methods to merge PDF recordsdata utilizing the .NET PDF Library and C# GitHub demo.
Strive It Free
Conclusion
Thanks for studying! On this weblog, we’ve explored easy methods to merge PDF recordsdata utilizing the Syncfusion .NET PDF Library in C#. We encourage you to dive deeper into the documentation, the place you’ll discover much more choices and options, full with code examples.
If in case you have any queries relating to these functionalities, kindly tell us by leaving a remark beneath. You too can attain out to us via our assist discussion board, assist portal, or suggestions portal. We’re all the time delighted that will help you!