Convert PDF Annotations to LEADTOOLS Annotations - WinForms C# .NET 6

This tutorial shows how to convert annotations embedded in a PDF to LEADTOOLS Annotation objects in a WinForms C# .NET 6 application using the LEADTOOLS SDK.

Note: This conversion is done implicitly when using a Document Viewer control that is configured to use automated annotations and load embedded PDF annotations. For more details, see the Draw and Edit Annotations on Documents tutorial.

Overview  
Summary This tutorial shows how to convert PDF annotations to LEAD annotations in a C# WinForms Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (78 KB)
Platform WinForms C# Application
IDE Visual Studio 2022
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 Draw and Edit Annotations on Images tutorials, before working on the Convert PDF Annotations to LEADTOOLS Annotations - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Draw and Edit Annotations on Images tutorial. If that project is unavailable, 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>\LEADTOOLS23\Bin\net:

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.

Add the Annotations Convert Code

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

In the Solution Explorer, double-click Form1.cs to display the Designer. Right-click on the Designer and select View Code, or press F7, to bring up the code behind the form. Add the using statements below to the top of the Form1 class.

C#
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Codecs; 
using Leadtools.Annotations.Automation; 
using Leadtools.Annotations.WinForms; 
using Leadtools.Pdf; 
using Leadtools.Pdf.Annotations; 

Add the below code to the Form1_Load method:

C#
private void Form1_Load(object sender, EventArgs e) 
{ 
   // Initialize Image Viewer object  
   _viewer = new(); 
   _viewer.Dock = DockStyle.Fill; 
 
   // Initialize Automation Control for Image Viewer  
   _automationControl = new(); 
   _automationControl.ImageViewer = _viewer; 
 
   // Initialize a new RasterCodecs object   
   RasterCodecs codecs = new(); 
   codecs.Options.Pdf.Load.HideAnnotations = true; 
 
   // Change file reference to PDF with annotations 
   string pdfFilename = @"C:\DevopsMaps\v23\tutorials\Dotnet-WinForms-Convert-PDF-Annotations-to-LEADTOOLS-Annotations\Annotated.pdf"; 
 
   // Load the main image into the viewer 
   //Comment out if handling files with MemoryStream 
   _viewer.Image = codecs.Load(pdfFilename); 
 
   //Uncomment if handling files with MemoryStream 
   //byte[] pdfData = File.ReadAllBytes(pdfFilename); 
   //MemoryStream pdfStream = new MemoryStream(pdfData); 
   //viewer.Image = codecs.Load(pdfStream); 
 
   // Initialize the Interactive Mode for the Image Viewer   
   AutomationInteractiveMode automationInteractiveMode = new(); 
   automationInteractiveMode.AutomationControl = _automationControl; 
 
   // Add the Interactive Mode to the Image Viewer   
   _viewer.InteractiveModes.BeginUpdate(); 
   _viewer.InteractiveModes.Add(automationInteractiveMode); 
   _viewer.InteractiveModes.EndUpdate(); 
 
   if (_viewer.Image != null) 
   { 
         // Create and set up the Automation Manager   
         _annAutomationManager = new(); 
         _annAutomationManager.RestrictDesigners = true; 
 
         // Instruct the Manager to create all the default Automation objects.   
         _annAutomationManager.CreateDefaultObjects(); 
 
         // Initialize the Manager Helper and create the Toolbar  
         // Add the Toolbar and the Image Viewer to the Controls   
         AutomationManagerHelper managerHelper = new(_annAutomationManager); 
         managerHelper.CreateToolBar(); 
         Controls.Add(managerHelper.ToolBar); 
         Controls.Add(_viewer); 
 
         // Set up the Automation (it will create the Container as well)   
         _automation = new(_annAutomationManager, _automationControl); 
         // Set this Automation as the active one   
         _automation.Active = true; 
 
         // Set the size of the Container to the size of the Image Viewer   
         _automation.Container.Size = _automation.Container.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(_viewer.Image.ImageWidth, _viewer.Image.ImageHeight)); 
 
         // Parse PDF annotations 
         PDFDocument pdfDoc = new(pdfFilename); 
 
         //Use if handling files with MemoryStream 
         //PDFDocument pdfDoc = new PDFDocument(pdfStream); 
 
         pdfDoc.ParsePages(PDFParsePagesOptions.Annotations, 1, -1); 
         // Make sure mapping resolution is correct  
         _automation.Container.Mapper.MapResolutions(_viewer.Image.XResolution, _viewer.Image.YResolution, _viewer.Image.XResolution, _viewer.Image.YResolution); 
         // Convert annotations  
         AnnPDFConvertor.ConvertFromPDF(pdfDoc.Pages[0].Annotations, _automation.Container, LeadSizeD.Create(pdfDoc.Pages[0].Width, pdfDoc.Pages[0].Height)); 
   } 
} 

Handling Streams

To handle the files using MemoryStream, replace the code line viewer.Image = codecs.Load(pdfFilename); with the following:

C#
byte[] pdfData = File.ReadAllBytes(pdfFilename); 
MemoryStream pdfStream = new MemoryStream(pdfData); 
viewer.Image = codecs.Load(pdfStream); 

Then replace the code line PDFDocument pdfDoc = new PDFDocument(pdfFilename); with the following:

C#
PDFDocument pdfDoc = new PDFDocument(pdfStream); 

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 sample PDF is loaded into the viewer and the embedded PDF annotation objects will be converted and loaded as LEADTOOLS annotation objects.

PDF Annotations Converted to LEADTOOLS Annotations

Wrap-up

This tutorial showed how to use the AnnPDFConvertor and PDFDocument classes.

See Also

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


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