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.
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.
|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||
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.
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
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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.
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
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.
// Initialize a new RasterCodecs object
RasterCodecs codecs = new RasterCodecs();
codecs.Options.Pdf.Load.HideAnnotations = true;
codecs.Load() method to load an annotated PDF file in the
ImageViewer. This tutorial utilizes this PDF file.
// 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.
// 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.Annotations, automation.Container, LeadSizeD.Create(pdfDoc.Pages.Width, pdfDoc.Pages.Height));
To handle the files using
MemoryStream, replace the code line
viewer.Image = codecs.Load(pdfFilename); with the following:
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:
PDFDocument pdfDoc = new PDFDocument(pdfStream);
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.
This tutorial showed how to use the