How to extract pages from a PDF document
One of the great things about being a programmer is that when you need software that you don't have, you can usually write a small utility application to do what you need without having to purchase the software.
If you own Adobe Acrobat ($299 USD) or FoxIt Editor ($99.00 USD), then you can just right click and extract pages from an existing PDF to create a new PDF document. However, if you don't want to shell out the money for it, then you can always write your own code to perform the same task.
I used iTextSharp in a previous application to provide a capability of exporting documents to PDF, so I was familiar with the iTextSharp project. In that application, I was able to create PDF documents for various paper sizes ranging from 8 1/2 x 11 and legal sizes to large format plotter pages sizes such as D and E-size pages. The library worked great and produce near perfect PDF documents of our displays.
Today, I needed to extract a couple pages from a PDF file to create a new PDF document and I realized that I didn't have a PDF editor... So I went out and downloaded iTextSharp library and wrote a small program to do the work for me.
I'll just brush over the basic program setup.
- I used a C# console application as my starting point
- There are 4 command line arguments (input file, output file, starting page, and ending page)
- Validate that the input file exists
- Validate that the input file is a PDF document
- Validate that the starting and ending page numbers are valid
- Add two using directives for iTextSharp.text and iTextSharp.text.pdf
Okay, here's the primary method of the application. You can see the four input parameters and the code comments should provide enough information to walk you through the steps.
private static void ExtractPages(string inputFile, string outputFile, int start, int end) { // get input document PdfReader inputPdf = new PdfReader(inputFile); // retrieve the total number of pages int pageCount = inputPdf.NumberOfPages; if (end < start || end > pageCount) { end = pageCount; } // load the input document Document inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1)); // create the filestream using (FileStream fs = new FileStream(outputFile, FileMode.Create)) { // create the output writer PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs); inputDoc.Open(); PdfContentByte cb1 = outputWriter.DirectContent; // copy pages from input to output document for (int i = start; i <= end; i++) { inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i)); inputDoc.NewPage(); PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i); int rotation = inputPdf.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height); } else { cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } } inputDoc.Close(); } }
Resources
- iTextSharp home page (SourceForge)
- iTextSharp Split example (SourceForge)
November 15th, 2008 - 14:40
hi its good to see this rarely available snippet on the net.But can u teel me where can we get the PDFReader class from …
November 15th, 2008 - 16:22
The PdfReader class is part of the iTextSharp library.
November 16th, 2008 - 16:15
Found it here ..> http://sourceforge.net/projects/itextsharp/
November 16th, 2008 - 22:46
There’s links to everything at the end of the blog entry above.
March 18th, 2009 - 22:43
hi, would it be able to extract pdf to images such as jpg or png with this library? if so can u show me pls tks
March 31st, 2009 - 17:56
I don’t know. There’s documentation associated with the library that you can read to figure it out.
October 9th, 2009 - 06:00
Great Article, but my PDF don´t copy exactly, there is a simbols and images don´t copy.
why? Decode library´s?
thanks
November 3rd, 2010 - 12:55
Hi ,
First of all congratulation for writing such good article.
I have issue with this example.
When i delete(Skip) page and generate document . The document is not fillable. While the source document is fillable.
Please help me on this.
Thanks
November 3rd, 2010 - 13:05
Chirag,
When you’re deleting a page are you saving the document before trying to extract or are you doing it at the same time? You may just be having an off by one error because if you delete page 2, then page 2 becomes page 3, etc. It’s easier to just loop through the document twice, once to delete pages and the second time to extract pages, or just skip pages and extract only what you need.
November 3rd, 2010 - 22:23
Jim,
Same issue as chirag is facing.
Here are my steps.
1. I am generating temp.pdf first. using the same code which you have suggested in this post.
2. And I am trying to fill this pdf in next step.
But the prob is temp.pdf remain no longer fillable though source pdf is fillable.
I am not changing anything in source document. I am just adding source document into temp.pdf. so prob like page 3 become page 2 is also not an issue in my case.
Please help me out.
Hitesh
November 17th, 2010 - 10:15
Hitesh,
I’m not sure what could be the problem. It could be something in the PDF file or just a minor coding issue.
April 12th, 2011 - 23:51
Thanx! awesome example