Tutorial: How To Load, Save, and Split Annotations

Posted on 2020-10-20 Katie

Being an important part of many document and imaging workflows, LEADTOOLS Annotations SDK provides an interface to mark up images with various shapes, notes, highlights, rulers, and redactions with options to either permanently alter the image or store information to undo and alter the annotations.

LEADTOOLS Annotations are portable and flexible enough to be used across a wide variety of different image and document formats. Additional annotation types offered by the LEADTOOLS SDK include private DICOM data elements, Wang annotation stored in TIFF tags, IBM FileNet P8 annotations, Daeja annotations, and PDF markups and annotations. Annotations can become a permanent part of the data or exported as XML, SVG, and EMF.

The code below shows the basics of what is needed to create a solution that loads, saves, and splits annotations. If you want a complete step-by-step tutorial, check out our console C# tutorial: How to Load, Save, and Split Annotations.

// Add this global variable 
static AnnContainer annContainer; 


// LOAD
static void LoadTifAnnotationsExample() 
{ 
   // Load the annotations from the TIFF file 
   AnnCodecs annCodecs = new AnnCodecs(); 
 
   annContainer = annCodecs.Load(@"TestFileTifAnnotations.tif", 1); 
 
   // Print out the objects in the container to show they are loaded 
   Console.WriteLine("ANNOTATIONS LOADED: From Test TIF File that Already Contained Annotations:\n"); 
   foreach (var annObject in annContainer.Children) 
   { 
      Console.WriteLine($"Annotation: {annObject}"); 
   } 
      Console.WriteLine("\n"); 
}

// SAVE
static void SaveTifAnnotationsExample() 
{ 
   using (RasterCodecs rasterCodecs = new RasterCodecs()) 
   { 
      AnnCodecs annCodecs = new AnnCodecs(); 
 
      RasterTagMetadata tag = annCodecs.SaveToTag(annContainer, false); 
 
      rasterCodecs.WriteTag(@"SaveAnnotationsToTif.tif", 1, tag); 
 
      // Now load the annotations from the TIFF file we just saved to ensure they were saved correctly 
      AnnContainer savedTifContainer = annCodecs.Load(@"SaveAnnotationsToTif.tif", 1); 
 
      // Print out the objects in the container to show they are loaded 
      Console.WriteLine("ANNOTATIONS LOADED: From TIFF File that we Saved Annotations to:\n"); 
      foreach (var annObject in savedTifContainer.Children) 
      { 
         Console.WriteLine($"Annotation: {annObject}"); 
      } 
      Console.WriteLine("\n"); 
   } 
} 

// SPLIT
static void SplitContainerToTifAndXmlExample() 
{ 
   // Save all the Rectangle annotations from the container to XML 
   AnnCodecs annCodecs = new AnnCodecs(); 
   AnnContainer xmlContainer = annContainer.Clone(); 
 
   for (int i = 0; i < xmlContainer.Children.Count; i++) 
   { 
      if (xmlContainer.Children[i].Id != AnnObject.RectangleObjectId) 
      { 
         xmlContainer.Children.Remove(xmlContainer.Children[i]); 
         i--; 
      } 
   } 
 
   annCodecs.Save(@"RectangleAnnotationsXml.xml", xmlContainer, AnnFormat.Annotations, 1); 
   // Now load the annotations from the XML file we just saved to ensure they were saved correctly 
   AnnContainer rectangleXmlContainer = annCodecs.Load(@"RectangleAnnotationsXml.xml", 1); 
 
   // Print out the objects in the container to show they are loaded 
   Console.WriteLine("ANNOTATIONS LOADED: From XML File that we Saved ONLY Rectangle Annotations to:"); 
   foreach (var annObject in rectangleXmlContainer.Children) 
   { 
      Console.WriteLine($"Annotation: {annObject}"); 
   } 
   Console.WriteLine(""); 
 
   // Save all the Note annotations from the container to TIFF 
   AnnContainer tifContainer = annContainer.Clone(); 
 
   for (int i = 0; i < tifContainer.Children.Count; i++) 
   { 
      if (tifContainer.Children[i].Id != AnnObject.NoteObjectId) 
      { 
         tifContainer.Children.Remove(tifContainer.Children[i]); 
         i--; 
      } 
   } 
 
   using (RasterCodecs rasterCodecs = new RasterCodecs()) 
   { 
      RasterTagMetadata tag = annCodecs.SaveToTag(tifContainer, false); 
 
      rasterCodecs.WriteTag(@"SaveJustNoteAnnotationsToTif.tif", 1, tag); 
   } 
 
   // Now load the annotations from the XML file we just saved to ensure they were saved correctly 
   AnnContainer noteTifContainer = annCodecs.Load(@"SaveJustNoteAnnotationsToTif.tif", 1); 
 
   // Print out the objects in the container to show they are loaded 
   Console.WriteLine("ANNOTATIONS LOADED: From TIFF File from which we Saved ONLY Note Annotations to:"); 
   foreach (var annObject in noteTifContainer.Children) 
   { 
      Console.WriteLine($"Annotation: {annObject}"); 
   } 
   Console.WriteLine("\n"); 

Try it out!

To test this for yourself, make sure to get the latest LEADTOOLS SDK code for free straight from our site if you have not already. This trial is good for 60 days and comes with unlimited chat and email support.

Support

Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.

LEADTOOLS Blog

LEADTOOLS Powered by Apryse,the Market Leading PDF SDK,All Rights Reserved