←Select platform

AnnCodecs Class

Summary
Provides support for loading and saving objects to annotation files.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public class AnnCodecs 
@interface LTAnnCodecs : NSObject 
public class AnnCodecs 
public: 
   ref class AnnCodecs 
class AnnCodecs: 
Remarks

This class supports loading and saving annotation objects from/to XML strings as well as getting information about previously saved annotations data.

This class also supports saving and loading multipage annotation data where each page contains a full Annotation Container.

Use the AnnCodecs.Save or AnnCodecs.SaveToString methods to save an annotation container as XML file or data. You can then use the browser capabilities to save this XML data to disk or use a Web Service to store it on a server. The SerializeOptions property can be used to set saving options and monitor the objects being saved.

Use the AnnCodecs.Load or AnnCodecs.LoadFromString methods to load an annotation container from XML file or data. The XML data must be previously loaded from disk using the browser capabilities or obtained from a server using a Web Service. The DeserializeOptions property can be used to set loading options, monitor the objects being loaded and handle errors.

Use the AnnCodecs.LoadAll or LoadAllFromString methods to load all the annotation containers from a multipage XML file or data.

Use the AnnCodecs.SaveAll or SaveAllToString methods to save an array of annotation container as multipage XML file or data.

Use GetInfo, GetInfoFromString to check if an XML data contains valid annotations data such as the format and number of pages.

LEADTOOLS supports saving multiple annotations into the same data. This can be used to save the annotations of a multipage document in the same data.

Use SaveLayer(String,AnnLayer,AnnFormat,Int32), SaveLayer(Stream,AnnLayer,AnnFormat,Int32) and AnnCodecs.SaveLayerToString to save individual annotation layers.

Example

This example will create a container, adds a line and rectangle objects to it. Then it will save it, obtain its information and load it back.

C#
Java
using Leadtools.Annotations.Automation; 
using Leadtools.Annotations.Engine; 
using Leadtools.Annotations.Rendering; 
 
public void AnnCodecs_AnnCodecs() 
{ 
   // Create a new annotation container, 8.5 by 11 inches 
   AnnContainer container = new AnnContainer(); 
   // Size must be in annotation units (1/720 of an inch) 
   container.Size = LeadSizeD.Create(8.5 * 720, 11 * 720); 
 
   double inch = 720.0; 
   // Add a red line object, from 1in 1in to 2in 2in 
   AnnPolylineObject lineObj = new AnnPolylineObject(); 
   lineObj.Points.Add(LeadPointD.Create(1 * inch, 1 * inch)); 
   lineObj.Points.Add(LeadPointD.Create(2 * inch, 2 * inch)); 
   lineObj.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Red"), LeadLengthD.Create(1)); 
   container.Children.Add(lineObj); 
 
   // Add a blue on yellow rectangle from 3in 3in to 4in 4in 
   AnnRectangleObject rectObj = new AnnRectangleObject(); 
   rectObj.Rect = LeadRectD.Create(3 * inch, 3 * inch, 1 * inch, 1 * inch); 
   rectObj.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Blue"), LeadLengthD.Create(1)); 
   rectObj.Fill = AnnSolidColorBrush.Create("Yellow"); 
   container.Children.Add(rectObj); 
 
   // Show the container 
   ShowContainer("Before save", container); 
 
   // Create the codecs object to save and load annotations 
   AnnCodecs codecs = new AnnCodecs(); 
 
   // Save the container 
 
   string destFileName = @"container.xml"; 
   codecs.Save(destFileName, container, AnnFormat.Annotations, 1); 
 
   // delete the container 
   container = null; 
 
   // Show information about the data we just saved 
   AnnCodecsInfo info = codecs.GetInfo(destFileName); 
   string message; 
   if (info.Format == AnnFormat.Annotations) 
   { 
      message = "Version: "; 
      message += info.Version; 
      message += " No. of pages: "; 
      message += info.Pages.Length; 
      message += " page nos: "; 
      for (int i = 0; i < info.Pages.Length; i++) 
      { 
         message += info.Pages[i] + " "; 
      } 
   } 
   else 
   { 
      message = "Invalid annotations data"; 
   } 
 
   Debug.WriteLine(message); 
 
 
   // Show information about the XML data we just saved 
   XmlDocument xmlDoc = new XmlDocument(); 
   xmlDoc.Load(destFileName); 
   AnnCodecsInfo infoFromString =  codecs.GetInfoFromString(xmlDoc.OuterXml); 
   Debug.WriteLine($"===================="); 
   Debug.WriteLine($"Get Info From String"); 
   Debug.WriteLine($"Format:  {infoFromString.Format}"); 
   Debug.WriteLine($"Version: {infoFromString.Version}"); 
   Debug.WriteLine($"Pages:   {infoFromString.Pages.Length}"); 
   Debug.WriteLine($"===================="); 
 
   // Load the container we just saved 
   container = codecs.Load(destFileName, 1); 
 
   // Show it 
   ShowContainer("After load", container); 
 
   // delete the container 
   container = null; 
 
   // Load the XML container just saved  
   container = codecs.LoadFromString(xmlDoc.OuterXml, 1); 
 
   // Show it 
   ShowContainer("After load from string", container); 
 
   // Save Active Layer using a Stream output 
   AnnLayer layer = AnnLayer.Create("MyLayer"); 
   container.ActiveLayer = layer; 
   Stream stream = File.Create("StreamActiveLayer.xml"); 
   codecs.SaveLayer(stream, container.ActiveLayer, AnnFormat.Annotations, 1); 
 
   // Save Active Layer using a File output 
   AnnLayer layer2 = AnnLayer.Create("MyLayer2"); 
   container.ActiveLayer = layer2; 
   codecs.SaveLayer("StringActiveLayer.xml", layer2, AnnFormat.Annotations, 1); 
          
   // Save Layer to string 
   AnnLayer layer4 = AnnLayer.Create("MyLayer4"); 
   string stringLayer = codecs.SaveLayerToString(layer4, AnnFormat.Annotations); 
   Debug.WriteLine($"{stringLayer}"); 
 
   // Use SelectionObject, set ActiveLayer, Change Offset, Change PageNumber, and Save to string 
   AnnLayer layer5 = AnnLayer.Create("MyLayer5"); 
   AnnSelectionObject annSelectionObject = container.SelectionObject; 
   annSelectionObject.IsSelected = true; 
   container.ActiveLayer = layer5; 
   container.Offset = new LeadPointD(10, 10); 
   container.PageNumber = 3; 
   string saveToString = codecs.SaveToString(container, AnnFormat.Annotations, 1); 
   Debug.WriteLine($"{saveToString}"); 
 
   //Load from String 
   AnnContainer annContainer = codecs.LoadFromString(saveToString, 1); 
   Debug.WriteLine($"{annContainer.PageNumber}"); 
 
} 
 
private void ShowContainer(String message, AnnContainer container) 
{ 
   string str = message + "\nContainer size: "; 
 
   // Add the size 
   double inch = 720; 
   double width = container.Size.Width / inch; 
   double height = container.Size.Height / inch; 
   str += width + " by " + height + " inches" + "\n"; 
 
   // Add the objects 
   str += "Contains " + container.Children.Count + " objects(s)\n"; 
   for (int i = 0; i < container.Children.Count; i++) 
   { 
      AnnObject annObj = container.Children[i]; 
 
      str += "Object: " + annObj.FriendlyName + " at "; 
      for (int j = 0; j < annObj.Points.Count; j++) 
      { 
         LeadPointD pt = annObj.Points[j]; 
         double x = pt.X / inch; 
         double y = pt.Y / inch; 
         str += "(" + x + ", " + y + ") "; 
      } 
 
      str += "\n"; 
   } 
 
   Debug.WriteLine(str); 
} 
 
import java.io.File; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.FileOutputStream; 
import java.io.FileWriter; 
import java.util.Scanner; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.annotations.engine.*; 
 
 
public void annCodecsAnnCodecs() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
   // Create a new annotation container, 8.5 by 11 inches 
   AnnContainer container = new AnnContainer(); 
 
   // Size must be in annotation units (1/720 of an inch) 
   container.setSize(LeadSizeD.create(8.5 * 720, 11 * 720)); 
 
   double inch = 720.0; 
 
   // Add a red line object, from 1in 1in to 2in 2in 
   AnnPolylineObject lineObj = new AnnPolylineObject(); 
   lineObj.getPoints().add(LeadPointD.create(1 * inch, 1 * inch)); 
   lineObj.getPoints().add(LeadPointD.create(2 * inch, 2 * inch)); 
   lineObj.setStroke(AnnStroke.create(AnnSolidColorBrush.create("Red"), LeadLengthD.create(1))); 
   container.getChildren().add(lineObj); 
 
   // Add a blue on yellow rectangle from 3in 3in to 4in 4in 
   AnnRectangleObject rectObj = new AnnRectangleObject(); 
   rectObj.setRect(LeadRectD.create(3 * inch, 3 * inch, 1 * inch, 1 * inch)); 
   rectObj.setStroke(AnnStroke.create(AnnSolidColorBrush.create("Blue"), LeadLengthD.create(1))); 
   rectObj.setFill(AnnSolidColorBrush.create("Yellow")); 
   container.getChildren().add(rectObj); 
 
   // Show the container 
   showContainer("Before save", container); 
 
   // Create the codecs object to save and load annotations 
   AnnCodecs codecs = new AnnCodecs(); 
 
   AnnContainer[] containers = new AnnContainer[1]; 
   containers[0] = container; 
   String xmlData; 
 
   // Save the container 
   xmlData = codecs.saveAll(containers, AnnFormat.ANNOTATIONS); 
 
   // Delete the container 
   container = null; 
 
   // Show information about the data we just saved 
   AnnCodecsInfo info = codecs.getInfo(xmlData); 
   String message; 
   if (info.getFormat() == AnnFormat.ANNOTATIONS) { 
      message = "Version: "; 
      message += info.getVersion(); 
      message += " No. of pages: "; 
      message += info.getPages().length; 
      message += " page nos: "; 
      for (int i = 0; i < info.getPages().length; i++) { 
         message += info.getPages()[i] + " "; 
      } 
   } else 
      message = "Invalid annotations data"; 
 
   System.out.println(message); 
 
   // Show information about the XML data we just saved 
   AnnCodecsInfo infoFromString = codecs.getInfo(xmlData); 
 
   System.out.println("===================="); 
   System.out.println("Get Info From String"); 
   System.out.println("Format:  " + infoFromString.getFormat()); 
   System.out.println("Version: " + infoFromString.getVersion()); 
   System.out.println("Pages:   " + infoFromString.getPages().length); 
   System.out.println("===================="); 
 
   // Load the container we just saved 
   container = codecs.load(xmlData, 1); 
 
   // Show it 
   showContainer("After load", container); 
 
   // For testing correctness 
   AnnContainer savedContainer = container; 
 
   // Delete the container 
   container = null; 
 
   // Load the XML container just saved 
   container = codecs.load(xmlData, 1); 
 
   // Show it 
   showContainer("After load from string", container); 
 
   // Save Active Layer using a Stream output 
   AnnLayer layer = AnnLayer.create("MyLayer"); 
   container.setActiveLayer(layer); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "StreamActiveLayer.xml"); 
   File file = new File(destFileName); 
   if (!file.exists()) { 
      file.delete(); 
   } 
   file.createNewFile(); 
   OutputStream stream = new FileOutputStream(file.getPath()); 
   codecs.saveLayer(stream, container.getActiveLayer(), AnnFormat.ANNOTATIONS, 1); 
 
   // Save Active Layer using a File output 
   destFileName = combine(LEAD_VARS_IMAGES_DIR, "FileActiveLayer.xml"); 
   FileWriter writer = new FileWriter(destFileName); 
   AnnLayer layer2 = AnnLayer.create("MyLayer2"); 
   container.setActiveLayer(layer2); 
   codecs.saveLayer(writer, layer2, AnnFormat.ANNOTATIONS, 1); 
 
   // Save Layer to string 
   AnnLayer layer4 = AnnLayer.create("MyLayer4"); 
   String stringLayer = codecs.saveLayer(layer4, AnnFormat.ANNOTATIONS, xmlData); 
 
   System.out.println("Layer:\n" + stringLayer); 
 
   // Use SelectionObject, set ActiveLayer, Change Offset, Change PageNumber, and 
   // Save to string 
   AnnLayer layer5 = AnnLayer.create("MyLayer5"); 
   AnnSelectionObject annSelectionObject = container.getSelectionObject(); 
   annSelectionObject.setSelected(true); 
   container.setActiveLayer(layer5); 
   container.setOffset(new LeadPointD(10, 10)); 
   container.setPageNumber(3); 
   String saveToString = codecs.save(container, AnnFormat.ANNOTATIONS, xmlData, 1); 
   System.out.println("Container:\n" + saveToString); 
 
   // Load from String 
   AnnContainer annContainer = codecs.load(saveToString, 1); 
   System.out.println(annContainer.getPageNumber()); 
 
   for (int i = 0; i < container.getChildren().size(); i++) { 
      AnnObject annObj = container.getChildren().get(i); 
      AnnObject savedAnnObj = savedContainer.getChildren().get(i); 
      assertTrue("Check loaded and saved containers are equal", 
            annObj.getFriendlyName().equals(savedAnnObj.getFriendlyName())); 
   } 
       
   assertTrue("File unsuccessfully saved", new File(destFileName).exists()); 
   System.out.printf("Command run, image saved to %s\n", destFileName); 
} 
 
private void showContainer(String message, AnnContainer container) { 
 
   String str = message + "\nContainer size: "; 
 
   // Add the size 
   double inch = 720; 
   double width = container.getSize().getWidth() / inch; 
   double height = container.getSize().getHeight() / inch; 
   str += width + " by " + height + " inches" + "\n"; 
 
   // Add the objects 
   str += "Contains " + container.getChildren().size() + " objects(s)\n"; 
   for (int i = 0; i < container.getChildren().size(); i++) { 
      AnnObject annObj = container.getChildren().get(i); 
 
      str += "Object: " + annObj.getFriendlyName() + " at "; 
      for (int j = 0; j < annObj.getPoints().size(); j++) { 
         LeadPointD pt = annObj.getPoints().get(j); 
         double x = pt.getX() / inch; 
         double y = pt.getY() / inch; 
         str += "(" + x + ", " + y + ") "; 
      } 
 
      str += "\n"; 
   } 
 
   System.out.println(str); 
 
} 
Requirements

Target Platforms

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

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