Home arrow Samples arrow Split Document Make Text BiggerMake Text SmallerReset Text Size
Split Document

This sample shows how to convert a PDF document with n pages into n documents with one page each.

// Get a fresh copy of the sample PDF file
string filename = "Portable Document Format.pdf";
File.Copy(Path.Combine("../../../../PDFs/", filename), 
  Path.Combine(Directory.GetCurrentDirectory(), filename), true);
 
// Open the file
PdfDocument inputDocument = PdfReader.Open(filename, PdfDocumentOpenMode.ReadOnly);
 
string name = Path.GetFileNameWithoutExtension(filename);
for (int idx = 0; idx < inputDocument.PageCount; idx++)
{
  // Create new document
  PdfDocument outputDocument = new PdfDocument();
  outputDocument.Version = inputDocument.Version;
  outputDocument.Info.Title =
    String.Format("Page {0} of {1}", idx + 1, inputDocument.Info.Title);
  outputDocument.Info.Creator = inputDocument.Info.Creator;
 
  // Add the page and save it
  outputDocument.AddPage(inputDocument.Pages[idx]);
  outputDocument.Save(String.Format("{0} - Page {1}.pdf", name, idx + 1));
}
 
< Prev   Next >
New! We no longer maintain this site. Visit the new PDFsharp and MigraDoc Foundation Homepage. © 2009 empira Software GmbH | Impressum | Created with Joomla!