Export Annotations to Output using the Document Converter - Console C#

This tutorial shows how to create a C# Windows console application that creates a new PDF document, loads annotations to it in an AnnContainer, and shows how to save the annotations to the output.

Overview  
Summary This tutorial covers how to load annotations into a new PDF document and save them to the output in a C# Windows Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2019
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET Framework (Console)
  • Java: Java

Required Knowledge

Before working on the Export Annotations to Output using the Document Converter - Console C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you don't 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). For this project, the following references are needed:

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\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

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.

Setup Input Paths

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

In the Solution Explorer, open Program.cs and add the following using statements to the using block at the top.

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Annotations.Rendering; 
using Leadtools.Document; 
using Leadtools.Document.Converter; 

Add the following objects as global variables:

C#
private static DocumentConverter docConverter; 
private static LEADDocument virtualDocument; 

Add the paths to the Source Document file to be loaded and the annotations XML file.

In addition, Add and call the methods below after SetLicence()

C#
static void Main(string[] args) 
{ 
   try 
   { 
      string pdfFile = @"C:\LEADTOOLS22\Resources\Images\leadtools.pdf"; 
      string annFile = @"C:\LEADTOOLS22\Resources\Images\leadtools.xml"; 
      SetLicense(); 
      InitConverter(); 
      LoadAnnotations(pdfFile, annFile); 
      if (virtualDocument != null) 
      { 
         BurnAnnotations(virtualDocument); 
         EmbedAnnotations(virtualDocument); 
         ExternalSaveAnnotations(virtualDocument); 
      } 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.ToString()); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Intialize the Document Converter

Add the code below for the InitConverter() method that will initialize the DocumentConverter class

C#
private static void InitConverter() 
{ 
   try 
   { 
      docConverter = new DocumentConverter(); 
      docConverter.SetDocumentWriterInstance(new DocumentWriter()); 
      docConverter.SetAnnRenderingEngineInstance(new AnnWinFormsRenderingEngine()); 
 
      Console.WriteLine("Document Converter Initialized."); 
 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.ToString()); 
   } 
} 

Create a New PDF, Add Pages from the Source, and Load Annotations

Add the following code for the LoadAnnotations(string pdfFile, string annFile) method that will create a new PDF and loads the annotations to it

C#
private static void LoadAnnotations(string pdfFile, string annFile) 
{ 
   try 
   { 
      virtualDocument = DocumentFactory.Create(new CreateDocumentOptions()); 
      var annUri = new Uri(annFile); 
      LoadDocumentOptions loadOptions = new LoadDocumentOptions(); 
      loadOptions.AnnotationsUri = annUri; 
      LEADDocument childDocument = DocumentFactory.LoadFromFile(pdfFile, loadOptions); 
      virtualDocument.Pages.Add(childDocument.Pages[0]); 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.ToString()); 
   } 
} 

Save Annotations

Use the code below to save the annotations into the output using three approaches.

The BurnAnnotations(LEADDocument virtualDocument) method will overlay (burn) the annotations into a rasterized image output

C#
static void BurnAnnotations(LEADDocument virtualDocument) 
{ 
try { 
   var jobData = new DocumentConverterJobData 
   { 
      AnnotationsMode = DocumentConverterAnnotationsMode.Overlay, 
      Document = virtualDocument, 
      OutputDocumentFileName = @"C:\LEADTOOLS22\Resources\Images\BurnAnnotationsDoc.pdf", 
      DocumentFormat = DocumentFormat.User, 
      RasterImageFormat = RasterImageFormat.RasPdf 
   }; 
   var job = docConverter.Jobs.CreateJob(jobData); 
   docConverter.Jobs.RunJob(job); 
   foreach (var error in job.Errors) 
      Console.WriteLine($"There was an error:{error.Error}"); 
} 

The EmbedAnnotations(LEADDocument virtualDocument) method will embed the annotations into a PDF output.

C#
 
 
 

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 creates virtual document, adds a PDF document to the virtual document, burns annotations to it, and exports the document to file.

Wrap-up

This tutorial showed how to create a new virtual document and burn annotations to it. Also it covered how to use the LEADDocument, LoadDocumentOptions, and DocumentConverter classes.

See Also

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

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