Copying Pages between PDF Files
Alter the structure of your PDF documents to best fit your needs with ease by utilizing IronPDF to copy pages between your PDF files. Now, you can easily copy a specific PDF page, and either insert it, prepend it, or append it to another PDF document. This gives you full control over where the page will appear and how your PDF documents look.
5 Steps for Copying Pages between PDF Files
// Create an instance of ChromePdfRenderer to render HTML as a PDF
var renderer = new ChromePdfRenderer();
// Convert HTML strings into PDF documents
var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
// Step 1: Select a page to copy from pdfdoc_b
// This line copies the first page from pdfdoc_b and stores it in the page_to_insert variable
var page_to_insert = pdfdoc_b.CopyPage(1);
// Step 2: Append the copied page to the end of pdfdoc_a
// Appends page_to_insert to the end of pdfdoc_a
pdfdoc_a.AppendPdf(page_to_insert);
// Step 3: Prepend the copied page to the beginning of pdfdoc_a
// Prepends page_to_insert at the beginning of pdfdoc_a
pdfdoc_a.PrependPdf(page_to_insert);
// Step 4: Insert the copied page into pdfdoc_a at a specific location
// Inserts page_to_insert at the second position in pdfdoc_a
pdfdoc_a.InsertPdf(page_to_insert, 1);
// Step 5: Save the newly structured PDF document
// Saves the modified pdfdoc_a to CopiedPages.pdf
pdfdoc_a.SaveAs("CopiedPages.pdf");
// Create an instance of ChromePdfRenderer to render HTML as a PDF
var renderer = new ChromePdfRenderer();
// Convert HTML strings into PDF documents
var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
// Step 1: Select a page to copy from pdfdoc_b
// This line copies the first page from pdfdoc_b and stores it in the page_to_insert variable
var page_to_insert = pdfdoc_b.CopyPage(1);
// Step 2: Append the copied page to the end of pdfdoc_a
// Appends page_to_insert to the end of pdfdoc_a
pdfdoc_a.AppendPdf(page_to_insert);
// Step 3: Prepend the copied page to the beginning of pdfdoc_a
// Prepends page_to_insert at the beginning of pdfdoc_a
pdfdoc_a.PrependPdf(page_to_insert);
// Step 4: Insert the copied page into pdfdoc_a at a specific location
// Inserts page_to_insert at the second position in pdfdoc_a
pdfdoc_a.InsertPdf(page_to_insert, 1);
// Step 5: Save the newly structured PDF document
// Saves the modified pdfdoc_a to CopiedPages.pdf
pdfdoc_a.SaveAs("CopiedPages.pdf");
' Create an instance of ChromePdfRenderer to render HTML as a PDF
Dim renderer = New ChromePdfRenderer()
' Convert HTML strings into PDF documents
Dim pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
Dim pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)
' Step 1: Select a page to copy from pdfdoc_b
' This line copies the first page from pdfdoc_b and stores it in the page_to_insert variable
Dim page_to_insert = pdfdoc_b.CopyPage(1)
' Step 2: Append the copied page to the end of pdfdoc_a
' Appends page_to_insert to the end of pdfdoc_a
pdfdoc_a.AppendPdf(page_to_insert)
' Step 3: Prepend the copied page to the beginning of pdfdoc_a
' Prepends page_to_insert at the beginning of pdfdoc_a
pdfdoc_a.PrependPdf(page_to_insert)
' Step 4: Insert the copied page into pdfdoc_a at a specific location
' Inserts page_to_insert at the second position in pdfdoc_a
pdfdoc_a.InsertPdf(page_to_insert, 1)
' Step 5: Save the newly structured PDF document
' Saves the modified pdfdoc_a to CopiedPages.pdf
pdfdoc_a.SaveAs("CopiedPages.pdf")
Before we get started, for our example, we first created two HTML strings, html_a
and html_b
, representing the content to be converted into PDF. Then, by creating a new ChromePdfRenderer
instance, we are able to convert these two HTML strings into new PDF documents using the RenderHtmlAsPdf
method. Now we have two fresh new PDF documents for use in our example.
First, let's select a page to copy. Using CopyPage(1)
, we copy the first page from the specified PDF and store it in the page_to_insert
variable for further use. This process of extracting the PDF page can be helpful for situations where you might want to transfer data between PDF documents.
Next, we'll look at the three methods of adding the extracted page to another PDF, effectively copying it across the PDFs. The first method, AppendPdf
, adds the copied page to the end of the PDF document. PrependPdf
can be utilized when you want to add the copied page at the beginning of the PDF. The last method, InsertPdf
, will insert the page at the specified location within the PDF. With these three methods, no matter the size of the PDF you are copying the page to, you will always have full control over where the copied page is added.
Finally, save the PDF with the copied page using the SaveAs
method. Now, you will be able to copy pages and alter your PDF page structures with ease.
Click here to view the How-to Guide, including examples, sample code, and files