←Select platform

SetAnnRenderingEngineInstance Method

Summary

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

Syntax
C#
C++/CLI
Java
Python
public void SetAnnRenderingEngineInstance( 
   AnnRenderingEngine instance 
) 
public:  
   void SetAnnRenderingEngineInstance( 
      AnnRenderingEngine^ instance 
   ) 
public void setAnnRenderingEngineInstance(AnnRenderingEngine instance) 
def SetAnnRenderingEngineInstance(self,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#
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); 
   } 
} 
Requirements

Target Platforms

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

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