Extract Pages from a PDF

The code below shows you how to create a new PDF file for each page in an existing file.

It is recommended that you have already reviewed the Getting Started sample, since that includes Licensing and Framework initialization code required to make this sample run.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
using System; using System.IO; using SolidFramework; using SolidFramework.Model.Pdf.Pages; namespace CSharp_Tutorials { public static partial class Tutorials { public static bool ExtractPdfPages(string pdfPath, string outputFolder) { // Create a PagesModel using (var model = new PagesModel()) { // Open the PDF file model.OpenPDF(pdfPath); // Set the base path for the output files string basePath = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(pdfPath)); // Get the page count int pageCount = model.PageCount; Console.WriteLine("Extracting pages from " + pdfPath + " into " + outputFolder); // Iterate over each page for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { // Set the models selection to the current page index model.Selection = new PagesArray { pageIndex }; // Save the model's selection to the output folder model.Save(basePath + "-" + (pageIndex + 1) + ".pdf", true); } } Console.WriteLine("Successfully extracted pages from " + pdfPath + " into " + outputFolder); Console.WriteLine(); return true; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Imports System Imports System.IO Imports SolidFramework Imports SolidFramework.Model.Pdf.Pages Namespace VBNet_Tutorials Partial Module Tutorials Function ExtractPdfPages(ByVal pdfPath As String, ByVal outputFolder As String) As Boolean ' Create a PagesModel Using model As PagesModel = New PagesModel() ' Open the PDF file model.OpenPDF(pdfPath) ' Set the base path for the output files Dim basePath As String = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(pdfPath)) ' Get the page count Dim pageCount As Integer = model.PageCount Console.WriteLine("Extracting pages from " & pdfPath & " into " & outputFolder) For pageIndex As Integer = 0 To pageCount - 1 ' Set the models selection to the current page index model.Selection = New PagesArray From { pageIndex } ' Save the model's selection to the output folder model.Save(basePath & "-" & (pageIndex + 1) & ".pdf", True) Next End Using Console.WriteLine("Successfully extracted pages from " & pdfPath & " into " & outputFolder) Console.WriteLine() Return True End Function End Module End Namespace
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "Tutorials.h" using namespace SolidFramework::Platform; using namespace SolidFramework::Model::Pdf::Pages; bool ExtractPdfPages(const wstring & pdfPath, const wstring & outputFolder) { // Create a PagesModel auto model = make_shared<PagesModel>(); // Open the PDF file model->OpenPDF(pdfPath); // Set the base path for the output files const wstring basePath = Path::Combine(outputFolder, Path::GetFileNameWithoutExtension(pdfPath)); // Get the page count const int pageCount = model->GetPageCount(); // Create a std::vector<int> to store selected page indexes in vector<int> selectedPages; wcout << L"Extracting pages from " << pdfPath << L" into " << outputFolder << endl; // Iterate over each page for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { // Set the models selection to the current page index selectedPages.push_back(pageIndex); model->SetSelection(selectedPages); selectedPages.clear(); // Save the model's selection to the output folder model->Save(basePath + L"-" + to_wstring(pageIndex + 1) + L".pdf", true); } wcout << L"Successfully extracted pages from " << pdfPath << L" into " << outputFolder << endl << endl; return true; }

Previous sample Samples Next sample