Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement