←Select platform

SetAnnRenderingEngineInstance Method

Summary

Sets the rendering engine to use when overlaying annotations during document conversion.

Syntax
C#
VB
C++
Java
public void SetAnnRenderingEngineInstance( 
   AnnRenderingEngine instance 
) 
Public Sub SetAnnRenderingEngineInstance( 
   ByVal instance As AnnRenderingEngine 
) 
public:  
   void SetAnnRenderingEngineInstance( 
      AnnRenderingEngine^ instance 
   ) 
public void setAnnRenderingEngineInstance(AnnRenderingEngine instance) 

Parameters

instance

The rendering engine to use when overlaying annotations during document conversion.

Remarks

The AnnRenderingEngine instance passed to this method will be used when any job has the value of AnnotationsMode set to DocumentConverterAnnotationsMode.Overlay and raster conversion is used.

You must initialize this object with the IAnnObjectRenderer's to use before starting any conversion if annotation containers are to be overlayed on images.

AnnRenderingEngineInstance will return the same value passed for instance.

The previously set AnnRenderingEngineInstance is removed when a new instance is set. You can set a value of null.

Example

This example will create an annotation file and use it as the overlay during raster PDF conversion.

C#
VB
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); 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Document.Writer 
Imports Leadtools.Svg 
Imports Leadtools.Document 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Engine 
Imports Leadtools.Ocr 
Imports Leadtools.Document.Converter 
Imports LeadtoolsDocumentConverterExamples.LeadtoolsExamples.Common 
 
Public Shared Sub SetAnnRenderingEngineExample() 
   ' Input document file 
   Dim inputDocumentFile As String = Path.Combine(ImagesPath.Path, "Leadtools.pdf") 
   Dim inputAnnotationsFile As String = Path.Combine(ImagesPath.Path, "my-annotations.xml") 
   Dim outputDocumentFile As String = Path.Combine(ImagesPath.Path, "my-output.pdf") 
   ' We will overlay the annotations 
   Dim annotationsMode As DocumentConverterAnnotationsMode = 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 documentConverter As 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 
      Dim jobData As 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 
      Dim job As DocumentConverterJob = documentConverter.Jobs.CreateJob(jobData) 
      jobData.JobName = "conversion job" 
 
      documentConverter.Jobs.RunJob(job) 
 
      If job.Status = DocumentConverterJobStatus.Success Then 
         Console.WriteLine("Success") 
      Else 
         Console.WriteLine($"{job.Status} Errors") 
         For Each err As DocumentConverterJobError In job.Errors 
            Console.WriteLine($"  {err.Operation} at {err.InputDocumentPageNumber}: {err.Error.Message}") 
         Next 
      End If 
   End Using 
End Sub 
 
' Create an annotation file that contains a single text annotations for each page in the document 
Private Shared Sub CreateTextAnnotations(rasterCodecs As RasterCodecs, documentFile As String, annotationsFile As String) 
   If File.Exists(annotationsFile) Then 
      File.Delete(annotationsFile) 
   End If 
 
   Dim annCodecs As New AnnCodecs() 
 
   ' Get the size for each page, create a container for it 
   Dim pageCount As Integer = rasterCodecs.GetTotalPages(documentFile) 
   ' The size of each annotations object we will add (1 by 0.5 inches) 
   Dim annObjectSize As LeadSizeD = LeadSizeD.Create(720, 360) 
   ' The template for the AnnObject we will use 
   Dim annTextObjectTemplate As 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 pageNumber As Integer = 1 To pageCount 
      ' Get the size of this page in pixels And convert to annotations units 
      Dim annContainer As New AnnContainer() 
      Using info As CodecsImageInfo = rasterCodecs.GetInformation(documentFile, False, pageNumber) 
         Dim resolution As Double = Math.Max(info.XResolution, info.YResolution) 
         If resolution = 0 Then 
            resolution = 96 
         End If 
 
         annContainer.Mapper.MapResolutions(resolution, resolution, resolution, resolution) 
         annContainer.Size = annContainer.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(info.ImageWidth, info.ImageHeight)) 
      End Using 
 
      annContainer.PageNumber = pageNumber 
 
      ' Add the text annotations object, 1 inch by half an inch, centered at the bottom of the page. Use the template 
      Dim annTextObject As AnnTextObject = CType(annTextObjectTemplate.Clone(), 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) 
   Next 
End Sub 
Requirements
Target Platforms
Help Version 21.0.2021.6.30
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

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