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 (3 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 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>\LEADTOOLS22\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 System; 
using System.Windows.Forms; 
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Codecs; 
using Leadtools.Annotations.Automation; 
using Leadtools.Annotations.WinForms; 
using Leadtools.Pdf; 
using Leadtools.Pdf.Annotations; 

For the purposes of this tutorial, the following code snippets will be added to the Form1_Load event handler.

To avoid rendering the embedded annotations on the image itself when loading the PDF in the ImageViewer control, use the following code.

C#
// Initialize a new RasterCodecs object  
RasterCodecs codecs = new RasterCodecs(); 
codecs.Options.Pdf.Load.HideAnnotations = true; 

Modify the codecs.Load() method to load an annotated PDF file in the ImageViewer. This tutorial utilizes this PDF file.

C#
// Annotated PDF file to load 
string pdfFilename = "Annotated.pdf"; 
// Load the main image into the viewer  
viewer.Image = codecs.Load(pdfFilename); 

Use the code below after the Automation container has been created and its size set, to parse the annotation objects embedded in the loaded PDF and add them to the Automation's container as LEADTOOLS annotation objects.

C#
// Parse PDF annotations 
PDFDocument pdfDoc = new PDFDocument(pdfFilename); 
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 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.