Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jan 05, 2024 12:40 am

I have one PDF that has two pages. Each page is 4 x 6. I want to combine both pages into one page of the same PDF doc to become 4 x12. Is this possible?

accessdude
 
Posts: 1
Joined: Thu Mar 24, 2022 4:09 pm

Fri Jan 05, 2024 2:51 am

Hello,

Thank you for your inquiry.
To fulfill your requirement of merging two pages into one, please refer to the following code snippet for implementation:
Code: Select all
// Create a new instance of PdfDocument
PdfDocument pdfDocument = new PdfDocument();

// Load the PDF file from "input.pdf"
pdfDocument.LoadFromFile("input.pdf");

// Retrieve the first page from the loaded document
PdfPageBase page1 = pdfDocument.Pages[0];

// Get the width of the first page
float width = page1.Size.Width;

// Get the height of the first page
float height1 = page1.Size.Height;

// Retrieve the second page from the loaded document
PdfPageBase page2 = pdfDocument.Pages[1];

// Get the height of the second page
float height2 = page2.Size.Height;

// Create a new PdfDocument instance to store the merged pages
PdfDocument newPdf = new PdfDocument();

// Set the margins for the new document
PdfMargins margins = new PdfMargins(0);

// Add a new page to the new document with the combined width and height of the first and second pages
PdfPageBase newpage = newPdf.Pages.Add(new System.Drawing.SizeF(width, height1 + height2), margins);

// Draw the template of the first page onto the new page's canvas at the top-left corner
newpage.Canvas.DrawTemplate(page1.CreateTemplate(), System.Drawing.PointF.Empty);

// Draw the template of the second page onto the new page's canvas starting from the top-left corner with an offset equal to the height of the first page
newpage.Canvas.DrawTemplate(page2.CreateTemplate(), new System.Drawing.PointF(0, height1));

// Save the merged document to "result.pdf"
newPdf.SaveToFile("result.pdf");

This code will assist you in achieving your desired outcome of merging two pages into a single page. If you have any further questions or need additional assistance, please don't hesitate to reach out to us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF