This tutorial shows how to convert annotations embedded in a PDF to LEADTOOLS Annotation objects in a WinForms C# 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 (79 KB) |
Platform | WinForms C# Application |
IDE | Visual Studio 2019 |
Development License | Download LEADTOOLS |
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 you do not have that project, 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:
Leadtools.Annotations.WinForms
Leadtools.Pdf
Leadtools.Viewer.Controls.WinForms
If using local DLL references, the following DLLs are needed.
The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Annotations.Automation.dll
Leadtools.Annotations.Designers.dll
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.Rendering.WinForms.dll
Leadtools.Annotations.WinForms.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Bmp.dll
Leadtools.Codecs.Fax.dll
Leadtools.Codecs.Png.dll
Leadtools.Codecs.Tif.dll
Leadtools.Controls.WinForms.dll
Leadtools.Pdf.dll
Leadtools.Pdf.Annotations.dll
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:
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.
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.
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.
// 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.
// 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[0].Annotations, automation.Container, LeadSizeD.Create(pdfDoc.Pages[0].Width, pdfDoc.Pages[0].Height));
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 AnnPDFConvertor
and PDFDocument
classes.