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