You can use PFD library, like itext7 NuGet package to merge pdf files.
So, first install the itext7 NuGet package in your project. Then the copy the following piece of code in your class:
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
internal class PdfHelper
{
public static void MergeFiles(string[] sourceFileNames, string destinationFileName)
{
var destinationPdf = new PdfDocument(new PdfWriter(destinationFileName));
PdfMerger merger = new PdfMerger(destinationPdf);
foreach (string file in sourceFileNames)
{
var inputPdf = new PdfDocument(new PdfReader(file));
merger.Merge(inputPdf, 1, inputPdf.GetNumberOfPages());
inputPdf.Close();
}
destinationPdf.Close();
}
}
Now you can use it like this:
private void Form1_Load(object sender, EventArgs e)
{
var files = Directory.GetFiles(
@"C:\pathtoyourpdffolder",
"*.pdf");
checkedListBox1.Items.AddRange(files);
}
private void button1_Click(object sender, EventArgs e)
{
var sourceFileNames = checkedListBox1.CheckedItems.Cast<string>().ToArray();
var destinationFileName = @"C:\pathtoputputfolder\temp.pdf";
PdfHelper.MergeFiles(sourceFileNames, destinationFileName);
MessageBox.Show("Merged!");
}
CheckedItems is the collection that contains the checked items. Items that we have added are all strings, so you can get the checked files like this:
var sourceFileNames = checkedListBox1.CheckedItems.Cast<string>().ToArray();