Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using PdfSharp.Pdf;
  5. using PdfSharp.Pdf.IO;
  6.  
  7. namespace MergePDFs
  8. {
  9. internal class Program
  10. {
  11. private static void CopyPages(string inFile, PdfDocument outPDF)
  12. {
  13. if (!File.Exists(inFile))
  14. {
  15. Console.WriteLine($"{inFile} doesn't exist!");
  16. return;
  17. }
  18.  
  19. try
  20. {
  21. using (var inPDF = PdfReader.Open(inFile, PdfDocumentOpenMode.Import))
  22. {
  23. for (var i = 0; i < inPDF.PageCount; i++)
  24. {
  25. outPDF.AddPage(inPDF.Pages[i]);
  26. }
  27. }
  28. }
  29. catch (Exception e)
  30. {
  31. Console.WriteLine($"Problem processing {inFile}:");
  32. Console.WriteLine(e);
  33. }
  34. }
  35. private static void Main(string[] args)
  36. {
  37. if (!args.Any())
  38. {
  39. Console.WriteLine("You need to provide path to PDF(s).");
  40. return;
  41. }
  42.  
  43. using (var outPDF = new PdfDocument())
  44. {
  45. foreach (var inFile in args)
  46. {
  47. CopyPages(inFile, outPDF);
  48. }
  49.  
  50. outPDF.Save("./merged.pdf");
  51. }
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement