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.

Overview  
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 2022
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 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>\LEADTOOLS22\Bin\net:

Note

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:

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.

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.

In the Solution Explorer, double-click Form1.cs to display the Designer. Open the Toolbox and add a MenuStrip.

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:

C#
// 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); 
      GetPageForm.Controls.Add(Label1); 
 
      Label Label2 = new Label(); 
      Label2.Size = new Size(80, 24); 
      Label2.Text = "Page &Number"; 
      Label2.Location = new Point(5, 33); 
      GetPageForm.Controls.Add(Label2); 
 
      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; 
      GetPageForm.Controls.Add(updownPage); 
 
      Button buttonOK = new Button(); 
      buttonOK.DialogResult = DialogResult.OK; 
      buttonOK.Size = new Size(80, 24); 
      buttonOK.Text = "&OK"; 
      buttonOK.Location = new Point(5, 61); 
      GetPageForm.Controls.Add(buttonOK); 
 
      Button buttonCancel = new Button(); 
      buttonCancel.DialogResult = DialogResult.Cancel; 
      buttonCancel.Size = new Size(80, 24); 
      buttonCancel.Text = "&Cancel"; 
      buttonCancel.Location = new Point(88, 61); 
      GetPageForm.Controls.Add(buttonCancel); 
 
      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:

C#
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) 
      return; 
   _documentViewer.BeginUpdate(); 
   _documentViewer.Document.Pages.Insert(0, document.Pages[page]); 
   _documentViewer.EndUpdate(); 
} 

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

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

Add the Code to Remove a Page

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

C#
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"); 
      return; 
   } 
   page = GetPageNumber(_documentViewer.Document.Pages.Count, "Select page to remove"); 
   if (page > -1) 
   { 
      _documentViewer.BeginUpdate(); 
      _documentViewer.Document.Pages.RemoveAt(page); 
      _documentViewer.EndUpdate(); 
   } 
} 

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.

Wrap-up

This tutorial showed how to manipulate pages in a document using the LEADDocument 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.