Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created February 3, 2014 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncansmart/8784183 to your computer and use it in GitHub Desktop.
Save duncansmart/8784183 to your computer and use it in GitHub Desktop.
Split PDFs into PDF per page (which can then be edited by InkScape) and merge
static void pdfSplitIntoPages()
{
// EO.Pdf.Runtime.AddLicense("...");
string sourceFile = @"C:\TEMP\Blah.pdf";
var pdf = new PdfDocument(sourceFile);
var pages = pdf.Split(Enumerable.Range(1, pdf.Pages.Count-1).ToArray());
var i = 1;
foreach (PdfDocument page in pages)
{
string newName = Path.Combine(Path.GetDirectoryName(sourceFile), Path.GetFileNameWithoutExtension(sourceFile) + " - Page " + (i++).ToString("000") + ".pdf");
page.Save(newName);
}
}
static void pdfMergePages()
{
string sourcePattern = @"C:\TEMP\Blah - Page *.pdf";
string targetFile = @"C:\TEMP\Blah - MERGED.pdf";
var sourceFiles = Directory.EnumerateFiles(Path.GetDirectoryName(sourcePattern), Path.GetFileName(sourcePattern))
.OrderBy(f => f);
var newDoc = PdfDocument.Merge(sourceFiles.Select(f => new PdfDocument(f)).ToArray());
newDoc.Save(targetFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment