Split Pages from PDFs in C#

The other day, I received a PDF that was almost 50 pages long. After I received the PDF, it turns out that I only needed to keep 4 of those pages on file. Using the LEADTOOLS PDF SDK, I was able to add PDF extraction libraries and extract those 4 pages. This only took 10 lines of code to create while other APIs and libraries can take up to 40 lines of complex code.

Below is the code that was thrown together in less than 10 minutes. I will show you how you can extract every page in a PDF file as well as specifics. The method behind this extraction is, as expected, the ExtractPages method. The method is associated with the PDFFile object.

C# Code to Split/Extract a PDF

PDFFile file = new PDFFile(@"PATH TO MULTI-PAGE PDF FILE");

// Extract all the pages
for (int i = 1; i <= file.GetPageCount(); i++)
{
    string output = $@"PATH TO WHERE YOU WANT THE FILE SAVED/NAME_{i}.pdf";
    file.ExtractPages(i, i, output);
}

// Or if you want specific pages
string output1 = $@"PATH TO WHERE YOU WANT THE FILE SAVED/NAME_1.pdf";
string output2 = $@"PATH TO WHERE YOU WANT THE FILE SAVED/NAME_2.pdf";
string output3 = $@"PATH TO WHERE YOU WANT THE FILE SAVED/NAME_3.pdf";
string output4 = $@"PATH TO WHERE YOU WANT THE FILE SAVED/NAME_4.pdf";

file.ExtractPages(5, 5, output1);
file.ExtractPages(14, 14, output2);
file.ExtractPages(26, 26, output3);
file.ExtractPages(46, 46, output4);

Try it out!

To test this for yourself, you can download my C# .NET console project. Make sure to get the latest LEADTOOLS SDK code for free straight from our site if you have not already. This trial is good for 60 days and comes with unlimited chat and email support.

Support

Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.

About 

Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in PDF. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *