SetAnnRenderingEngineInstance Method (original) (raw)
Summary
Sets the rendering engine to use when overlaying annotations during document conversion.
Syntax
public void setAnnRenderingEngineInstance(AnnRenderingEngine instance)
def SetAnnRenderingEngineInstance(self,instance):
Parameters
instance
The rendering engine to use when overlaying annotations during document conversion.
Example
This example will create an annotation file and use it as the overlay during raster PDF conversion.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Document.Writer;
using Leadtools.Svg;
using LeadtoolsExamples.Common;
using Leadtools.Document;
using Leadtools.Caching;
using Leadtools.Annotations.Engine;
using Leadtools.Ocr;
using Leadtools.Document.Converter;
using Leadtools.Annotations.Rendering;
public static void SetAnnRenderingEngineExample()
{
// Input document file
string inputDocumentFile = Path.Combine(ImagesPath.Path, "Leadtools.pdf");
string inputAnnotationsFile = Path.Combine(ImagesPath.Path, "my-annotations.xml");
string outputDocumentFile = Path.Combine(ImagesPath.Path, "my-output.pdf");
// We will overlay the annotations
var annotationsMode = DocumentConverterAnnotationsMode.Overlay;
// Create the annotations file we will use for this sample
CreateTextAnnotations(DocumentFactory.RasterCodecsTemplate, inputDocumentFile, inputAnnotationsFile);
// Convert the input file to a raster PDF with the annotations overlaid
using (var documentConverter = new DocumentConverter())
{
// If we run the example without the next line then we would get an error since we did not set a valid annotations rendering engine
// This example will use the Windows Forms (GDI+) one
documentConverter.SetAnnRenderingEngineInstance(new Leadtools.Annotations.Rendering.AnnWinFormsRenderingEngine());
// Fill in the job data, passing the input file, input annotations file, output file,
// format and annotations mode
var jobData = new DocumentConverterJobData();
jobData.InputDocumentFileName = inputDocumentFile;
jobData.InputAnnotationsFileName = inputAnnotationsFile;
jobData.OutputDocumentFileName = outputDocumentFile;
jobData.RasterImageFormat = RasterImageFormat.RasPdfJpeg422;
jobData.DocumentFormat = DocumentFormat.User;
jobData.AnnotationsMode = annotationsMode;
// Create a job and run it
DocumentConverterJob job = documentConverter.Jobs.CreateJob(jobData);
jobData.JobName = "conversion job";
documentConverter.Jobs.RunJob(job);
if (job.Status == DocumentConverterJobStatus.Success)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine($"{job.Status} Errors");
foreach (DocumentConverterJobError err in job.Errors)
{
Console.WriteLine($" {err.Operation} at {err.InputDocumentPageNumber}: {err.Error.Message}");
}
}
}
}
// Create an annotation file that contains a single text annotations for each page in the document
private static void CreateTextAnnotations(RasterCodecs rasterCodecs, string documentFile, string annotationsFile)
{
if (File.Exists(annotationsFile))
File.Delete(annotationsFile);
var annCodecs = new AnnCodecs();
// Get the size for each page, create a container for it
int pageCount = rasterCodecs.GetTotalPages(documentFile);
// The size of each annotations object we will add (1 by 0.5 inches)
LeadSizeD annObjectSize = LeadSizeD.Create(720, 360);
// The template for the AnnObject we will use
var annTextObjectTemplate = new AnnTextObject();
annTextObjectTemplate.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("red"), LeadLengthD.Create(2));
annTextObjectTemplate.HorizontalAlignment = AnnHorizontalAlignment.Center;
annTextObjectTemplate.VerticalAlignment = AnnVerticalAlignment.Center;
annTextObjectTemplate.Font = new AnnFont("Arial", 12);
for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++)
{
// Get the size of this page in pixels and convert to annotations units
var annContainer = new AnnContainer();
using (CodecsImageInfo info = rasterCodecs.GetInformation(documentFile, false, pageNumber))
{
double resolution = Math.Max(info.XResolution, info.YResolution);
if (resolution == 0)
resolution = 96;
annContainer.Mapper.MapResolutions(resolution, resolution, resolution, resolution);
annContainer.Size = annContainer.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(info.ImageWidth, info.ImageHeight));
}
annContainer.PageNumber = pageNumber;
// Add the text annotations object, 1 inch by half an inch, centered at the bottom of the page. Use the template
var annTextObject = annTextObjectTemplate.Clone() as AnnTextObject;
annTextObject.Rect = LeadRectD.Create(
(annContainer.Size.Width - annObjectSize.Width) / 2,
annContainer.Size.Height - annObjectSize.Height,
annObjectSize.Width,
annObjectSize.Height);
annTextObject.Text = "Page " + pageNumber.ToString();
annContainer.Children.Add(annTextObject);
// Save the container to the output file
annCodecs.Save(annotationsFile, annContainer, AnnFormat.Annotations, annContainer.PageNumber);
}
}