Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Friday, January 5, 2018 4:07:52 PM(UTC)
Christopher Thompson

Groups: Tech Support, Administrators
Posts: 45

Was thanked: 2 time(s) in 2 post(s)

This example application shows how to programmatically manipulate Annotations in three key ways.
>>Loading
>>Saving
>>Splitting

This has been updated to use with LEADTOOLS v20
*Please note that at the bottom of this page are downloads for each of the test applications shown below

C#
Code:
class Program
    {
        private static AnnContainer annContainer;

        static void Main(string[] args)
        {
            //Set License and Key
            string licFile = @"";
            string keyFile = "";
            RasterSupport.SetLicense(licFile, keyFile);

            // Loads the annotations from an existing TIF file that already contains annotations
            LoadTifAnnotationsExample();

            // Saves the annotations loaded in LoadTifAnnotationsExample to a separate TIF file
            SaveTifAnnotationsExample();

            // Splits the annotations loaded in LoadTifAnnotationsExample into 2 containers and saves the AnnRectangleObjects to XML and AnnNoteObjects to TIF
            SplitContainerToTifAndXmlExample();

            Console.WriteLine("Done...");
            Console.ReadKey();
        }

        private static void LoadTifAnnotationsExample()
        {
            // Load the annotations from the TIF file
            AnnCodecs annCodecs = new AnnCodecs();

            annContainer = annCodecs.Load(@"C:\Users\cthompson\Desktop\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.ToString()}");
            }
            Console.WriteLine("\n");
        }

        private static void SaveTifAnnotationsExample()
        {
            using (RasterCodecs rasterCodecs = new RasterCodecs())
            {
                AnnCodecs annCodecs = new AnnCodecs();

                RasterTagMetadata tag = annCodecs.SaveToTag(annContainer, false);

                rasterCodecs.WriteTag(@"C:\Users\cthompson\Desktop\SaveAnnotationsToTif.tif", 1, tag);

                // Now load the annotations from the TIF file we just saved to to ensure they were saved correctly
                AnnContainer savedTifContainer = annCodecs.Load(@"C:\Users\cthompson\Desktop\SaveAnnotationsToTif.tif", 1);

                // Print out the objects in the container to show they are loaded
                Console.WriteLine("ANNOTATIONS LOADED: From TIF File the we Saved Annotations to:\n");
                foreach (var annObject in savedTifContainer.Children)
                {
                    Console.WriteLine($"Annotation: {annObject.ToString()}");
                }
                Console.WriteLine("\n");
            }
        }

        private 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(@"C:\Users\cthompson\Desktop\RectangleAnnotationsXml.xml", xmlContainer, AnnFormat.Annotations, 1);
            // Now load the annotations from the XML file we just saved to to ensure they were saved correctly
            AnnContainer rectangleXmlContainer = annCodecs.Load(@"C:\Users\cthompson\Desktop\RectangleAnnotationsXml.xml", 1);

            // Print out the objects in the container to show they are loaded
            Console.WriteLine("ANNOTATIONS LOADED: From XML File the we Saved ONLY Rectangle Annotations to:");
            foreach (var annObject in rectangleXmlContainer.Children)
            {
                Console.WriteLine($"Annotation: {annObject.ToString()}");
            }
            Console.WriteLine("");


            /********************************************************************************************************************/
            //                                                                                                                  //
            /********************************************************************************************************************/


            // Save all the Note annotations from the container to TIF
            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(@"C:\Users\cthompson\Desktop\SaveJustNoteAnnotationsToTif.tif", 1, tag);
            }

            // Now load the annotations from the XML file we just saved to to ensure they were saved correctly
            AnnContainer noteTifContainer = annCodecs.Load(@"C:\Users\cthompson\Desktop\SaveJustNoteAnnotationsToTif.tif", 1);

            // Print out the objects in the container to show they are loaded
            Console.WriteLine("ANNOTATIONS LOADED: From TIF File the we Saved ONLY Note Annotations to:");
            foreach (var annObject in noteTifContainer.Children)
            {
                Console.WriteLine($"Annotation: {annObject.ToString()}");
            }
            Console.WriteLine("\n");
        }
    }


File Attachment(s):
annotationsLoadSaveSplit_20.zip (36kb) downloaded 40 time(s).
Chris Thompson
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2018, Yet Another Forum.NET
This page was generated in 0.148 seconds.