How to write all selected Files of checkedListBox in C# so i can merch the files

Andi 111 Reputation points
2023-01-23T10:35:01.4933333+00:00

Hi i already have following programm and i want that all selected files which are shown in the listBox will be merched in the end.

So what i need is how can i code, that i get the path of all checked entries and hwo can i then write them into my programm so i can merch it.

What i already have :

DirectoryInfo dinfo = new DirectoryInfo(@"C:\ProgramData\XXX\XXX");
            FileInfo[] Files = dinfo.GetFiles("*.Pdf");

            foreach (FileInfo file in Files)
            {
                checkedListBox4.Items.Add(file.Name);
   

Merch would look like this:

    // List of source file names.

    var fileNames = new string[]

    {

        "MergeFile01.pdf",

        "MergeFile02.pdf",

        "MergeFile03.pdf"

    };      
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,838 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
0 comments No comments
{count} vote

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-01-23T13:13:51.64+00:00

    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();
    

0 additional answers

Sort by: Most helpful