PFranchise
PFranchise

Reputation: 6752

Making 1 page pdfs from a multipage pdf using itextsharp

I have a pdf that I want to make into several 1 page pdfs where each page in the original pdf is now its own pdf document. I am trying to do this using itextsharp, but am having some difficulty even getting started.

Am I going about this the wrong way? Anyone know how I would go about doing this?

Thanks a ton!

Upvotes: 1

Views: 812

Answers (3)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

using (FileStream fs = new FileStream(argPdfFileName, FileMode.Create))
{
  Document doc = new Document(argReader.GetPageSize(argPdfPageNumber));
  PdfCopy copy = new PdfCopy(doc, fs);
  doc.Open();
  PdfImportedPage page = copy.GetImportedPage(argReader, argPdfPageNumber);
  copy.AddPage(page);
  doc.Close();
}

Chopped this out of some working code. argReader is a PdfReader instance pointing at the source document. argPdfPageNumber is the pagenumber in question. Basically you just create a new pdf. Import the source page into it and then add it to the document and save.

Upvotes: 1

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90193

There are two different commandline tools which can do that, both for any of the major OS platforms available.

pdftk

Example commandline:

pdftk input.pdf burst output page_%04d-from-input.pdf

Ghostscript

Example commandline:

gs \
   -o page_%04d-from-input.pdf \
   -sDEVICE=pdfwrite \
    input.pdf

Caveats: It's only the most recent version of Ghostscript which supports this commandline syntax of %04d for page-wise different output files + names.

pdftk isn't able to do any PDF manipulation in the process and is very fast.

The new Ghostscript method somewhat slower, but has the advantage that it can apply a lot of its other PDF processing capabilities should you need them.

Upvotes: 1

DCNYAM
DCNYAM

Reputation: 12126

There is a different library, PDFSharp, that may be better suited for this. You can open a PDFDocument object, and go through each page individually, extracting it into is own, separate PDF document.

Upvotes: 1

Related Questions