Articles in this section
Category / Section

How can we add page labels while importing pages from one PDF to another using Syncfusion Essential PDF in a C#?

3 mins read

The Syncfusion Essential® PDF is a feature-rich and high performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can add page labels while importing pages from one PDF to another using C#.

Steps to add page labels while importing pages from one PDF to another programmatically:

  1. Create a new console application project.Screenshot (1337).png
  2. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
    Screenshot (2157).png
  3. Include the following necessary namespaces in your Program.cs file.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
  1. Add the following code to your Program.cs to import pages from one PDF to another and add page labels:
    C#
// Open the first PDF document (Barcode.pdf).
FileStream inputFileStream = new FileStream(@"../../../Barcode.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream);
// Create a new PDF document.
PdfDocument document = new PdfDocument();
// Add a section to the new document.
PdfSection section = document.Sections.Add();
// Set page label style and starting number for the section.
section.PageLabel = new PdfPageLabel();
section.PageLabel.StartNumber = 1;
section.PageLabel.NumberStyle = PdfNumberStyle.UpperRoman;
// Set page margins to zero.
section.PageSettings.Margins.All = 0;
// Iterate through pages of the loaded document.
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
   // Add a new page to the section.
   PdfPage page = section.Pages.Add();
   // Create page as template from the loaded document.
   PdfTemplate template = loadedDocument.Pages[i].CreateTemplate();
   // Draw the template on the new page.
   page.Graphics.DrawPdfTemplate(template, PointF.Empty, template.Size);
}
// Open the second PDF document (Invoice.pdf).
loadedDocument = new PdfLoadedDocument(new FileStream(@"../../../Invoice.pdf", FileMode.Open));
// Add another section to the new document.
PdfSection section1 = document.Sections.Add();
// Set page label style and starting number for the second section.
section1.PageLabel = new PdfPageLabel();
section1.PageLabel.StartNumber = 1;
section1.PageLabel.NumberStyle = PdfNumberStyle.Numeric;
// Set page margins to zero.
section1.PageSettings.Margins.All = 0;
// Iterate through pages of the second loaded document.
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
   // Add a new page to the second section.
   PdfPage page = section1.Pages.Add();
   // Create page as template from the second loaded document.
   PdfTemplate template = loadedDocument.Pages[i].CreateTemplate();
   // Draw the template on the new page.
   page.Graphics.DrawPdfTemplate(template, PointF.Empty, template.Size);
}

// Close the loaded document.
loadedDocument.Close(true);
// Create a memory stream to save the new document.
MemoryStream stream = new MemoryStream();
// Save the new document to the memory stream.
document.Save(stream);
// Close the new document.
document.Close(true);
// Write the new document from the memory stream to a file.
File.WriteAllBytes(@"Output.pdf", stream.ToArray());

A complete working sample can be downloaded from page_labels_while_importing_pages_from_one_PDF_to_another.zip

By following these steps and using the provided code, you will create a new PDF document that imports pages from two existing PDFs (Barcode.pdf and Invoice.pdf). Each section of the new document will have its own page labels with different numbering styles. The first section uses upper Roman numerals, and the second.Screenshot (1719).png

Take a moment to peruse the documentation for working with pages, where you will find other options like inserting, removing, and rearranging pages in PDF document, adding margin, and importing pages from the existing PDF document.

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied