How to merge multiple PDF pages into a single page using C#
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 combine multiple PDF pages into a single page using C#.
Steps to combine multiple PDF pages into a single page programmatically:
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to combine multiple PDF pages into a single page.
C#
// Get all PDF files from the specified directory
string[] documents = Directory.GetFiles(@"../../../", "*.pdf");
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Set all margins to zero
document.PageSettings.Margins.All = 0;
// Count the number of PDF documents to be merged
int docuCount = documents.Length;
// Set the page size to A4
document.PageSettings.Size = PdfPageSize.A4;
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Get the graphics object for the page
PdfGraphics graphics = page.Graphics;
// Initialize location variables for drawing
float yLocation = 0;
float xLocation = 0;
// Calculate the height for each page in the merged document
float pageHeight = PdfPageSize.A4.Height / docuCount;
// Loop through each document to merge them
for (int i = 0; i < docuCount; i++)
{
// Set the x location based on the current index and page height
xLocation = pageHeight * i;
// Load the current PDF document
PdfLoadedDocument pdfLoaded = new PdfLoadedDocument(File.OpenRead(documents[i]));
// Get the first page of the loaded PDF
PdfLoadedPage loadedPage = pdfLoaded.Pages[0] as PdfLoadedPage;
// Create a template from the loaded page
PdfTemplate template = loadedPage.CreateTemplate();
// Save the current graphics state
graphics.Save();
// Translate the graphics context to the right position
graphics.TranslateTransform(page.GetClientSize().Width, 0);
// Rotate the graphics context to align the PDF correctly
graphics.RotateTransform(-270);
// Draw the PDF template onto the page at the specified location
graphics.DrawPdfTemplate(template, new PointF(xLocation, yLocation), new SizeF(pageHeight, page.GetClientSize().Width));
// Restore the graphics state to its previous state
graphics.Restore();
}
// Create a file stream to save the document
FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite);
// Save the document
document.Save(outputFileStream);
// Close the document
document.Close();
A complete working sample can be downloaded from Multiple_PDF_pages_into_a_single_page.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation to merge the multiple PDF documents with string array and importing pages from multiple documents with Essential PDF. Also, brief details about split, delete and rotate PDF pages are available with code examples.
Refer here to explore the rich set of Syncfusion Essential® PDF features.