Skip to content

Instantly share code, notes, and snippets.

@pczajkowski
Last active September 25, 2019 13:30
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 pczajkowski/934c916d820611150bc510f839f92f84 to your computer and use it in GitHub Desktop.
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
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