This tutorial shows how to create a new PDF document, load annotations to it in an AnnContainer
, and shows how to save the annotations to the output in a C# .NET 6 application.
Overview | |
---|---|
Summary | This tutorial covers how to load annotations into a new PDF document and save them to the output in a C# .NET 6 application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (2 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 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:
Leadtools.Annotations.WinForms
Leadtools.Document.Sdk
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.Rendering.WinForms.dll
Leadtools.Core.dll
Leadtools.dll
Leadtools.Document.Converter.dll
Leadtools.Document.Pdf.dll
Leadtools.Document.Writer.dll
Leadtools.Document.dll
Leadtools.Pdf.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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
and add the following using statements to the using
block at the top.
using Leadtools;
using Leadtools.Annotations.Rendering;
using Leadtools.Document;
using Leadtools.Document.Converter;
Add the following objects as global variables:
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 InitLEAD()
static void Main(string[] args)
{
try
{
string pdfFile = @"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf";
string annFile = @"FILE PATH TO ANNOTATIONS XML";
InitLEAD();
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);
}
Add the code below for the InitConverter()
method that will initialize the DocumentConverter
class
private static void InitConverter()
{
docConverter = new DocumentConverter();
docConverter.SetDocumentWriterInstance(new DocumentWriter());
docConverter.SetAnnRenderingEngineInstance(new AnnWinFormsRenderingEngine());
Console.WriteLine("Document Converter Initialized.");
}
Add the following code for the LoadAnnotations(string pdfFile, string annFile)
method that will create a new PDF and loads the annotations to it
private static void LoadAnnotations(string pdfFile, string annFile)
{
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]);
}
To handle loading the files using MemoryStream
, replace the existing code in the LoadAnnotations()
method and the code that calls it in the Main()
method with the following:
private static void LoadAnnotations(Stream pdfStream, Stream annStream)
{
virtualDocument = DocumentFactory.Create(new CreateDocumentOptions());
LEADDocument childDocument = DocumentFactory.LoadFromStream(pdfStream, new LoadDocumentOptions());
virtualDocument.Pages.Add(childDocument.Pages[0]);
virtualDocument.IsReadOnly = false;
Leadtools.Annotations.Engine.AnnCodecs annCodecs = new Leadtools.Annotations.Engine.AnnCodecs();
Leadtools.Annotations.Engine.AnnContainer[] annContainers = annCodecs.LoadAll(annStream);
if (annContainers != null)
virtualDocument.Annotations.SetAnnotations(annContainers);
virtualDocument.IsReadOnly = true;
}
static void Main(string[] args)
{
try
{
string pdfFile = @"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf";
string annFile = @"C:\LEADTOOLS23\Resources\Images\Leadtools.xml";
InitLEAD();
InitConverter();
byte[] pdfData = File.ReadAllBytes(pdfFile);
MemoryStream pdfStream = new MemoryStream(pdfData);
byte[] annData = File.ReadAllBytes(annFile);
MemoryStream annStream = new MemoryStream(annData);
LoadAnnotations(pdfStream, annStream);
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);
}
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
static void BurnAnnotations(LEADDocument virtualDocument)
{
var jobData = new DocumentConverterJobData
{
AnnotationsMode = DocumentConverterAnnotationsMode.Overlay,
Document = virtualDocument,
OutputDocumentFileName = @"C:\LEADTOOLS23\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}")
if (job.Errors.Count == 0)
Console.WriteLine(String.Format("- Annotations Overlaid(Burned) to: {0}", jobData.OutputDocumentFileName));
}
The EmbedAnnotations(LEADDocument virtualDocument)
method will embed the annotations into a PDF output.
private static void EmbedAnnotations(LEADDocument virtualDocument)
{
var jobData = new DocumentConverterJobData
{
AnnotationsMode = DocumentConverterAnnotationsMode.Embed,
Document = virtualDocument,
OutputDocumentFileName = @"C:\LEADTOOLS23\Resources\Images\EmbedAnnotationsDoc.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}")
if (job.Errors.Count == 0)
Console.WriteLine(String.Format("- Annotations Embedded in: {0}", jobData.OutputDocumentFileName));
}
The ExternalSaveAnnotations(LEADDocument virtualDocument)
method will export the annotations to an XML file on disk.
private static void ExternalSaveAnnotations(LEADDocument virtualDocument)
{
var jobData = new DocumentConverterJobData
{
AnnotationsMode = DocumentConverterAnnotationsMode.External,
Document = virtualDocument,
OutputDocumentFileName = @"C:\LEADTOOLS23\Resources\Images\ExternalAnnotationsDoc.pdf",
OutputAnnotationsFileName = @"C:\LEADTOOLS23\Resources\Images\ExternalAnnotationsDoc.xml",
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}");
if (job.Errors.Count == 0)
Console.WriteLine(String.Format("- Output document saved in: {0} \n Annotations saved in: {1}", jobData.OutputDocumentFileName, jobData.OutputAnnotationsFileName));
}
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.
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.