Split a PDF and Extract Pages in C#
IronPDF makes the process of editing your PDF document layouts through splitting and extracting pages a breeze. With IronPDF, you can extract singular or ranges of pages into new PdfDocument
objects, which can then be saved or further edited. This is all done thanks to the PdfDocument.CopyPage
method.
Steps to Split PDF Files in C#
using IronPdf; // Make sure to include the IronPdf namespace // Create an instance of ChromePdfRenderer var renderer = new ChromePdfRenderer(); // Convert the HTML string into a PDF document var pdf = renderer.RenderHtmlAsPdf(html); // Copy the first page of the PDF, storing it in the page1doc variable var page1Doc = pdf.CopyPage(0); page1Doc.SaveAs("Split1.pdf"); // Extract pages within a specific range (pages 2 and 3) and store them in page23Doc variable var page23Doc = pdf.CopyPages(1, 2); page23Doc.SaveAs("Split2.pdf");
using IronPdf; // Make sure to include the IronPdf namespace // Create an instance of ChromePdfRenderer var renderer = new ChromePdfRenderer(); // Convert the HTML string into a PDF document var pdf = renderer.RenderHtmlAsPdf(html); // Copy the first page of the PDF, storing it in the page1doc variable var page1Doc = pdf.CopyPage(0); page1Doc.SaveAs("Split1.pdf"); // Extract pages within a specific range (pages 2 and 3) and store them in page23Doc variable var page23Doc = pdf.CopyPages(1, 2); page23Doc.SaveAs("Split2.pdf");
Imports IronPdf ' Make sure to include the IronPdf namespace ' Create an instance of ChromePdfRenderer Private renderer = New ChromePdfRenderer() ' Convert the HTML string into a PDF document Private pdf = renderer.RenderHtmlAsPdf(html) ' Copy the first page of the PDF, storing it in the page1doc variable Private page1Doc = pdf.CopyPage(0) page1Doc.SaveAs("Split1.pdf") ' Extract pages within a specific range (pages 2 and 3) and store them in page23Doc variable Dim page23Doc = pdf.CopyPages(1, 2) page23Doc.SaveAs("Split2.pdf")
Before we get into the core process behind splitting PDF files, we create the HTML string which will be used to make the example PDF. The ChromePdfRenderer
class is then used to convert the HTML string into a PDF document. The RenderHtmlAsPdf
method performs the conversion, and the resulting PDF is stored in the PdfDocument
object.
Now, we can start splitting the PDF. First, we use the CopyPage(0)
method to copy the first page of the PDF, storing it in the page1Doc
variable. This simple line of code extracts the specified page from the PDF. Then, using the SaveAs
method, we save the page we extracted as a new, separate PDF file.
For extracting page ranges, the CopyPages
method is used to extract the pages within the specified page range from the original PDF. (Remember that pages are zero-indexed, so 1 and 2 refer to the second and third pages). The resulting extracted pages are stored in the page23Doc
variable. Now, you can go on to further manipulate these extracted pages, or utilize the SaveAs
method to save the extracted pages as a new PDF document.
Click here to view the How-to Guide, including examples, sample code, and files >