This tutorial shows how to burn the annotations from an external XML file to a PDF document in a C# .NET 6 Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to burn annotations to a PDF file using the LEADDocument class in a C# .NET 6 Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET 6 Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or Higher |
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 tutorial, before working on this tutorial.
Start with a copy of the project created in the Add References and Set a License 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 via NuGet packages.
This tutorial requires the following NuGet packages:
Leadtools.Annotations.NETStandard
Leadtools.Document.Sdk
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:
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
. Add the following statements to the using
block at the top of Program.cs
.
using System;
using System.IO;
using Leadtools;
using Leadtools.Annotations.Rendering;
using Leadtools.Document;
using Leadtools.Document.Converter;
Add a new method called BurnAnnotations(string documentFile, string annFile)
and call it inside the Main
method under InitLEAD();
.
static void Main(string[] args)
{
string pdfFile = @"FILE PATH TO PDF FILE";
string annFile = @"FILE PATH TO ANNOTATIONS XML FILE";
InitLEAD();
BurnAnnotations(pdfFile, annFile);
}
Add the below code to create a new virtual document, add a PDF document to the virtual document, burn annotations, and export the file using the Document Converter.
static void BurnAnnotations(string documentFile, string annFile)
{
LEADDocument virtualDocument = DocumentFactory.Create(new CreateDocumentOptions());
var annUri = new Uri(annFile);
LoadDocumentOptions loadOptions = new LoadDocumentOptions
{
AnnotationsUri = annUri,
};
LEADDocument childDocument = DocumentFactory.LoadFromFile(documentFile, loadOptions);
virtualDocument.Pages.Add(childDocument.Pages[0]);
DocumentConverter docConverter = new DocumentConverter();
docConverter.SetDocumentWriterInstance(new Leadtools.Document.Writer.DocumentWriter());
docConverter.SetAnnRenderingEngineInstance(new AnnDrawRenderingEngine());
var jobData = new DocumentConverterJobData
{
AnnotationsMode = DocumentConverterAnnotationsMode.Embed,
Document = virtualDocument,
OutputDocumentFileName = @"C:\LEADTOOLS23\Resources\Images\BurnAnnotationsDoc.pdf",
DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf,
};
var job = docConverter.Jobs.CreateJob(jobData);
docConverter.Jobs.RunJob(job);
foreach (var error in job.Errors)
Console.WriteLine($"There was an error:{error.Error}");
}
To load a document using memory stream, add the following code to the top of the BurnAnnotations
method:
using (var stream = File.OpenRead(documentFile))
{
var options = new LoadDocumentOptions();
using(var document = DocumentFactory.LoadFromStream(stream, options))
{
// Console commands to double check that the file was loaded properly
Console.WriteLine(document.DocumentId);
Console.WriteLine("Document loaded");
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application creates a virtual document, adds a PDF document to the virtual document, burns annotations to it, and exports the document to file.
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.