Last active
September 25, 2019 13:30
-
-
Save pczajkowski/934c916d820611150bc510f839f92f84 to your computer and use it in GitHub Desktop.
PoC to demonstrate how easy it is to merge PDFs. Based on https://stackoverflow.com/a/808699/7342859
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using PdfSharp.Pdf; | |
using PdfSharp.Pdf.IO; | |
namespace MergePDFs | |
{ | |
internal class Program | |
{ | |
private static void CopyPages(string inFile, PdfDocument outPDF) | |
{ | |
if (!File.Exists(inFile)) | |
{ | |
Console.WriteLine($"{inFile} doesn't exist!"); | |
return; | |
} | |
try | |
{ | |
using (var inPDF = PdfReader.Open(inFile, PdfDocumentOpenMode.Import)) | |
{ | |
for (var i = 0; i < inPDF.PageCount; i++) | |
{ | |
outPDF.AddPage(inPDF.Pages[i]); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"Problem processing {inFile}:"); | |
Console.WriteLine(e); | |
} | |
} | |
private static void Main(string[] args) | |
{ | |
if (!args.Any()) | |
{ | |
Console.WriteLine("You need to provide path to PDF(s)."); | |
return; | |
} | |
using (var outPDF = new PdfDocument()) | |
{ | |
foreach (var inFile in args) | |
{ | |
CopyPages(inFile, outPDF); | |
} | |
outPDF.Save("./merged.pdf"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment