Extract pages from a PDF in C#
Extracting pages is the process of selecting pages from a source document and then saving the selected pages in a new document.
Download sampleExtract pages from multipage files of the following formats:
Extracting pages from a PDF
The code below extracts pages from a multipage PDF file to a multipage output PDF file using the ClonePages method:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");using GdPicturePDF gdpicturePDF = new GdPicturePDF();using GdPicturePDF gdpictureDestPDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");gdpictureDestPDF.NewPDF();// Extract specified pages to the output file.gdpictureDestPDF.ClonePages(gdpicturePDF, "2-3;5");gdpictureDestPDF.SaveToFile(@"output.pdf");Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports System.IOImports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()Using gdpictureDestPDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") gdpictureDestPDF.NewPDF() ' Extract specified pages to the output file. gdpictureDestPDF.ClonePages(gdpicturePDF, "2-3;5") gdpictureDestPDF.SaveToFile("output.pdf")End UsingEnd UsingTo check whether the last operation succeeded, use the GetStat method of the GdPicturePDF object. It returns a member of the GdPictureStatus enumeration.
Extracting pages from a TIFF
The code below extracts pages from a multipage TIFF file to a multipage output TIFF file using the TiffExtractPage method. Each page is extracted separately:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int sourceImageID;int outputID = 0;int imageID;string source = @"multipage-tiff.tif", output = @"output.tif";sourceImageID = gdpictureImaging.CreateGdPictureImageFromFile(source);// Set the page numbers you want to extract.int[] pages = { 1, 3, 5 };bool firstPage = true;foreach (int page in pages){ Stream pStream = new MemoryStream(); gdpictureImaging.TiffExtractPage(sourceImageID, page, pStream); // Move to the start of the stream before reading the extracted page. pStream.Position = 0; imageID = gdpictureImaging.CreateGdPictureImageFromStream(pStream); if (firstPage) { // If it's the first image, create a new multipage TIFF file. outputID = imageID; gdpictureImaging.TiffSaveAsMultiPageFile(outputID, output, TiffCompression.TiffCompressionAUTO); firstPage = false; } else { // If it's the second or later image, add to the previously created multipage TIFF file. gdpictureImaging.TiffAddToMultiPageFile(outputID, imageID); gdpictureImaging.ReleaseGdPictureImage(imageID); };}gdpictureImaging.TiffCloseMultiPageFile(outputID);gdpictureImaging.ReleaseGdPictureImage(outputID);gdpictureImaging.ReleaseGdPictureImage(sourceImageID);Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()Dim sourceImageID As IntegerDim outputID = 0Dim imageID As IntegerDim source = "multipage-tiff.tif", output = "output.tif"sourceImageID = gdpictureImaging.CreateGdPictureImageFromFile(source)' Set the page numbers you want to extract.Dim pages = {1, 3, 5}Dim firstPage = TrueFor each page In pages Dim pStream As Stream = New MemoryStream() gdpictureImaging.TiffExtractPage(sourceImageID, page, pStream) ' Move to the start of the stream before reading the extracted page. pStream.Position = 0 imageID = gdpictureImaging.CreateGdPictureImageFromStream(pStream) If firstPage Then ' If it's the first image, create a new multipage TIFF file. outputID = imageID gdpictureImaging.TiffSaveAsMultiPageFile(outputID, output, TiffCompression.TiffCompressionAUTO) firstPage = False Else ' If it's the second or later image, add to the previously created multipage TIFF file. gdpictureImaging.TiffAddToMultiPageFile(outputID, imageID) gdpictureImaging.ReleaseGdPictureImage(imageID) End IfNextgdpictureImaging.TiffCloseMultiPageFile(outputID)gdpictureImaging.ReleaseGdPictureImage(outputID)gdpictureImaging.ReleaseGdPictureImage(sourceImageID)End UsingTo check whether the last operation succeeded, use the GetStat method of the GdPictureImaging object. It returns a member of the GdPictureStatus enumeration.