Merge PDF Files to Single PDF File - WinForms C#

This tutorial shows how to combine multiple PDF files into a single PDF document in a WinForms C# application using the LEADTOOLS SDK.

Overview  
Summary This tutorial shows how to merge PDF files in a WinForms C# application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (10 KB)
Platform Windows WinForms C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Merge PDF Files to Single PDF File - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Create the User Interface

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, double-click Form1.cs to display the Designer. Open the Toolbox and double-click MenuStrip to add a menu to the form. Add a &File dropdown menu item to the new MenuStrip. Add three menu items to the dropdown, &Add, &Merge, and &Clear. Adding & will underline the first letter of the menu item. Respectively, leave the new items' names as addToolStripMenuItem, mergeToolStripMenuItem, and clearToolStripMenuItem.

Adding a MenuStrip to the form

Add a ListBox control by double-clicking on the ListBox tool in the Toolbox. Set the name of the ListBox to sourceFilesListBox and set its Dock property to Fill.

Adding ListBox from Toolbox.

Add PDF Files to Merge Code

Double-click the Add.. menu item to create its event handler, this will also bring up the code behind Form1. Add the using statements below to the top.

C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Pdf; 
using System; 
using System.IO; 
using System.Windows.Forms; 

Add the following code in the addToolStripMenuItem_Click event handler that will allow the application to add a PDF file to the list of files to be merged:

C#
private void addToolStripMenuItem_Click(object sender, System.EventArgs e) 
{ 
   try 
   { 
      using (OpenFileDialog dlg = new OpenFileDialog()) 
      { 
         dlg.Filter = "PDF Documents|*.pdf|All Files|*.*"; 
         dlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
 
         if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) 
         { 
            sourceFilesListBox.Items.Add(dlg.FileName); 
         } 
      } 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Add the Merge PDF Code

In the Solution Explorer, double-click Form1.cs to go back to the Designer. Double-click the Merge menu item to create its event handler, this will also bring up the code behind Form1. Add the following code in the mergeToolStripMenuItem_Click event handler to merge all the listed PDF files into a single PDF that is then saved to the file path you indicate to the SaveFileDialog.

C#
private void mergeToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      if (sourceFilesListBox.Items.Count < 2) 
      { 
         MessageBox.Show("Please have at least 2 or more PDF files in order to merge them"); 
         return; 
      } 
      else 
      { 
         // Save output location 
         SaveFileDialog saveDlg = new SaveFileDialog(); 
         saveDlg.Filter = "PDF Document|*.pdf"; 
         if (saveDlg.ShowDialog(this) != DialogResult.OK) 
            return; 
 
         // Create PDFFile object using first item 
         PDFFile pdfFile = new PDFFile(sourceFilesListBox.Items[0].ToString()); 
 
         // Output remainder ListBox to string array 
         string[] sourceFiles = new string[sourceFilesListBox.Items.Count-1]; 
         for (int i = 1; i < sourceFilesListBox.Items.Count; i++) 
         { 
            sourceFiles[i-1] = sourceFilesListBox.Items[i].ToString(); 
         } 
 
         // Merge PDF files 
         using (RasterCodecs codecs = new RasterCodecs()) 
         { 
            // Specify location of PDF Runtime Files 
            codecs.Options.Pdf.InitialPath = @"C:\LEADTOOLS22\Bin\Dotnet4\x64"; 
            // Merge 
            pdfFile.MergeWith(sourceFiles, saveDlg.FileName); 
            MessageBox.Show("Merged Output PDF saved to " + saveDlg.FileName); 
         } 
      } 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Note

Alternatively, you can copy the Leadtools.Pdf.Utilities.dll component to the application build output instead of setting the path to the runtime file in the InitialPath property.

Add the Code for the Clear Menu Item

Go back to the form's Designer and double-click the Clear menu item to create its event handler. Add the following code in it to clear the list of loaded files:

C#
private void clearToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   sourceFilesListBox.Items.Clear(); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and the form should appear. To test, follow the steps below:

  1. Click on File -> Add... to bring up the OpenFileDialog.

  2. Select a PDF file to be loaded.

  3. Repeat steps 1 and 2 till all the PDF files to merge are loaded.

  4. Click on File -> Merge to bring up the SaveFileDialog.

  5. Select the output file path and click Save to merge all the PDFs to a single PDF file.

Wrap-up

This tutorial showed how to merge PDF files using the PDFFile class.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.