Create an Annotation Object Programmatically

 

The following example demonstrates how to create a Text Annotation object programmatically and save it to the Tiff File as an editable annotation or burn it to the Tiff image.

 

If the annotation object(s) is burn in with the AnnoBurnin function, the annotation object(s) will be visible for all Tiff Viewer applications, but annotation object(s) will not be editable anymore. If the annotations is saved into a TIFF tag (with the AnnoSaveToTiff function), the annotation object(s) can be displayed later with the AnnoLoadFromTiff function for editing, but the annotation object(s) will not be visible in non-Black Ice applications. If the burn parameter is set to true, the annotations will be burnt into the image, otherwise the annotations are saved to a TIFF tag.

 

The sample code uses TIFF Group 4 compression which only supports 1 bit (black and white) images. For 8 bit or 24 bit images, one can check the bit depth of the DIB (Device Independent Bitmap) using the GetDIBBitCount function and use another compression method, like JPEG or LZW.

Another option is to convert the 8 bit or 24 bit image to 1 bit with one of the dithering functions e.g.: with the DitherFS4 function:

DitherFS4  Method

 

For the available compression types and supported bit depths, please see the following page in the manual:

TIFF compression modes

 

When creating the annotation object, the page and layer has to be selected before calling CreateAnnoObj. Please see the sample code below.

 

[C++]

        public void AnnotateImage(string fileName, string annoText, string newFile, bool burn)

        {

            long hDib = BiTiff.LoadTiffIntoDIB(fileName, 0, false);

 

            BiAnno.AnnoLoadFromTiff(fileName, 0);

 

            BiAnno.AnnoUISetExtent(BiDIB.GetDIBWidth(hDib), BiDIB.GetDIBHeight(hDib));

 

            // selecting the page and layer before creating the object

            BiAnno.AnnoSelectPage(0);

            BiAnno.SetNumberOfLayers(1);

            BiAnno.SetLayerNumberAt(5, 0);

            BiAnno.SetLayersExt();

            BiAnno.SelectLayer(5);

 

            // creating text object

            BiAnno.CreateAnnoObj((short)BIANNOLib.ObjectType.aotText);

 

            // setting the properties of the new object

            BiAnno.AnnoObjSetPos(10, 10);

            BiAnno.AnnoObjSetSize(800, 80);

            BiAnno.AnnoObjSetFont(80, 0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, "Arial");

            BiAnno.AnnoObjSetRGBColorComponents(0, 0, 0);

            BiAnno.AnnoObjSetText(annoText);

 

            // burning or saving the image

            if (burn)

            {

                var hNewDib = BiAnno.AnnoBurnin(hDib);

                BiTiff.DropDIB(hDib);

                BiAnno.DeleteObjectsFromPage(0);

 

                bool b = BiTiff.InsertTiffImage(newFile, hNewDib, 208, false, 1, 0, false, true);

                BiTiff.DropDIB(hNewDib);

            }

            else

            {

                bool b = BiTiff.InsertTiffImage(newFile, hDib, 208, false, 1, 0, false, true);

                BiTiff.DropDIB(hDib);

                bool bres = BiAnno.AnnoSaveToTiff(newFile, 0);

            }

 

        }