Add and Remove Pages from a LEADDocument - WinForms C#

This tutorial shows how to add pages to LEADDocument and remove pages from it, in a WinForms C# application.

Summary This tutorial shows how to modify the pages of a document in a WinForms C# application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (10 KB)
Platform WinForms C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and the Display Files in the Document Viewer tutorials, before working on the Add and Remove Pages from a LEADDocument - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Display Files in the Document Viewer tutorial. If you don't have that project, follow the steps in that tutorial to create it.

Ensure the project has the below necessary LEADTOOLS references.

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

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:


How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License tutorial.

For a complete list of which DLLs are required for specific features, refer to Files to be Included in 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:


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

Add Document Viewer Menu Items

To allow manipulation of the Pages collection, the LEADDocument must not be read-only. That's why in the 'Getting Started' tutorial a 'virtual document' has been created, since such a document will not be read-only.

Ensure that you have added all the necessary set license code in your Winforms C# application as covered in the Add References and Set a License.

Add a new menu titled &Pages with two items titled &Add.. and &Remove.., respectively, and keep the names of the new items as addToolStripMenuItem and removeToolStripMenuItem.

A function that enables the user to select pages from a document is needed, so add the following method to the form:

// The following function displays a dialog to get a number between zero and (totalPages - 1) 
// Returns -1 if the user clicks Cancel 
private int GetPageNumber(int totalPages, string prompt) 
   using (Form GetPageForm = new Form()) 
      GetPageForm.FormBorderStyle = FormBorderStyle.FixedDialog; 
      GetPageForm.ClientSize = new Size(175, 88); 
      GetPageForm.Text = "Page"; 
      Label Label1 = new Label(); 
      Label1.Size = new Size(165, 24); 
      Label1.Text = prompt; 
      Label1.Location = new Point(5, 5); 
      Label Label2 = new Label(); 
      Label2.Size = new Size(80, 24); 
      Label2.Text = "Page &Number"; 
      Label2.Location = new Point(5, 33); 
      NumericUpDown updownPage = new NumericUpDown(); 
      updownPage.Size = new Size(80, 24); 
      updownPage.Location = new Point(88, 33); 
      updownPage.Minimum = 0; 
      updownPage.Maximum = totalPages - 1; 
      updownPage.Value = 0; 
      Button buttonOK = new Button(); 
      buttonOK.DialogResult = DialogResult.OK; 
      buttonOK.Size = new Size(80, 24); 
      buttonOK.Text = "&OK"; 
      buttonOK.Location = new Point(5, 61); 
      Button buttonCancel = new Button(); 
      buttonCancel.DialogResult = DialogResult.Cancel; 
      buttonCancel.Size = new Size(80, 24); 
      buttonCancel.Text = "&Cancel"; 
      buttonCancel.Location = new Point(88, 61); 
      GetPageForm.AcceptButton = buttonOK; 
      GetPageForm.CancelButton = buttonCancel; 
      if (GetPageForm.ShowDialog() == DialogResult.OK) 
         return (int)updownPage.Value; 
   return -1; 

Add the Code to Insert a Page

Add the following function to load a new document, take a page from it and add it at the start of the Document Viewer's virtual document:

private void InsertPageFromFile(string filename) 
   LEADDocument document = DocumentFactory.LoadFromFile(filename, new LoadDocumentOptions { UseCache = false }); 
   int page = GetPageNumber(document.Pages.Count, "Select page to insert"); 
   if (page < 0) 
   documentViewer.Document.Pages.Insert(0, document.Pages[page]); 

Call the InsertPageFromFile method in the Add menu item's event handler.

private void addToolStripMenuItem_Click(object sender, EventArgs e) 
   if (!documentViewer.HasDocument) 
      MessageBox.Show("Must load a document first"); 
   OpenFileDialog dlg = new OpenFileDialog(); 
   dlg.Title = "Select file to insert first page from"; 
   dlg.Filter = "PDF File|*.pdf|All Files|*.*"; 
   dlg.InitialDirectory = @"C:\LEADTOOLS21\Resources\Images"; 
   if (dlg.ShowDialog(this) == DialogResult.OK) 

Add the Code to Remove a Page

Add the following code in the Remove menu item's event handler.

private void removeToolStripMenuItem_Click(object sender, EventArgs e) 
   int page = -1; 
   if ((!documentViewer.HasDocument) || (documentViewer.Document.Pages.Count < 2)) 
      MessageBox.Show("Must have document loaded with at least two pages"); 
   page = GetPageNumber(documentViewer.Document.Pages.Count, "Select page to remove"); 
   if (page > -1) 

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 after loading a document into the Document Viewer, the items inside the Pages menu should enable inserting a page at the start of the document or removing a page from it.


This tutorial showed how to manipulate pages in a document using the LEADDocument class.

See Also

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