Added the form controls and AppSettings keys to allow the user to select the format of the date appended to the output file. Further update to issue #1.
90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
namespace PDF_Merge
|
|
{
|
|
using System;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using PdfSharp.Pdf;
|
|
using PdfSharp.Pdf.IO;
|
|
class MergePDFs
|
|
{
|
|
public static void MergePdfFiles(string[] filePaths, string outputPath)
|
|
{
|
|
|
|
using (var output = new PdfDocument())
|
|
{
|
|
if (ConfigurationManager.AppSettings["overwrite"] == true.ToString())
|
|
{
|
|
if (File.Exists(outputPath))
|
|
{
|
|
try
|
|
{
|
|
|
|
System.IO.File.Delete(outputPath);
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("Error deleting the old PDF.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
foreach (var file in filePaths)
|
|
{
|
|
try
|
|
{
|
|
using (var input = PdfReader.Open(file, PdfDocumentOpenMode.Import))
|
|
{
|
|
for (int i = 0; i < input.Pages.Count; i++)
|
|
{
|
|
output.AddPage(input.Pages[i]);
|
|
}
|
|
}
|
|
} catch { }
|
|
}
|
|
|
|
try
|
|
{
|
|
|
|
output.Save(outputPath);
|
|
if (File.Exists(outputPath))
|
|
{
|
|
MessageBox.Show("PDF saved.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("PDF cannot be empty. There were no documents processed.", "Error" ,MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
public static string[] CollectPdfFiles()
|
|
{
|
|
string dirPath = ConfigurationManager.AppSettings["PDF-Path"];
|
|
Console.WriteLine(dirPath);
|
|
string[] pdfFiles = Directory.EnumerateFiles(dirPath, "*.pdf").ToArray();
|
|
Array.Sort(pdfFiles);
|
|
if (pdfFiles.Length != 0)
|
|
{
|
|
foreach (string file in pdfFiles)
|
|
{
|
|
Console.WriteLine(file);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("No PDFs were found in the configured directory.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
return pdfFiles;
|
|
}
|
|
|
|
public static string GetDate()
|
|
{
|
|
DateTime date = DateTime.Now;
|
|
string dateFormat = ConfigurationManager.AppSettings["dateFormat"];
|
|
return date.ToString(dateFormat);
|
|
}
|
|
}
|
|
|
|
}
|