Custom Annotations: 25 Projects in 25 Days

CustomAnnotations Screenshot
CustomAnnotations Screenshot

As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Hadi.

What it Does

This C# application will create several custom annotations using LEADTOOLS Version 19.

Features Used

Development Progress Journal

Hello, my name is Hadi and I am writing a sample application that will demonstrate the various capabilities of the LEADTOOLS Annotation SDK, more specifically, how to create Custom Annotations and Designers.

My plan is to start off by writing a simple project that uses the default Annotations and ImageViewer so that I have a base to start with. The WinForms Annotations demo located in the \Examples\DotNet\CS\AnnotationsDemo directory will work perfectly.

Now that I have the sample project created, I will start with my first custom object, the AnnCircleObject. I want this custom object to be very similar to the AnnEllipseObject but instead of an oval, it will be a perfect circle.

To get started, I created new directories to keep things organized and will start by creating the new object which is based on the Ellipse.

Next, I want to control how the object is drawn, so I will need to create my own custom Draw Designer based on the AnnEllipseDrawDesigner. I will override the OnPointerDown and OnPointerMove events so that I can control the ellipse and force it into a perfect circle.

In the OnPointerDown event I set the begin and end points, and in the OnPointerMove event I do some math to get the width and height of the drawing rectangle so I can force the circle to be the same width and height, therefore creating a perfect circle.

I also don’t want the new AnnCircleObject to be modified and changed to an oval or ellipse, so I have created a custom ObjectRenderer based on the AnnEllipseObjectRenderer and overrode the RenderThumbs event to not render the top, left, right and bottom resize thumbs so the user can’t modify the height or width individually.

Next I will create a text-based custom object, the AnnNumberObject. This object will be a custom text-based Annotation object that will, when drawn, count and display the number of other AnnNumberObjects in the same AnnContainer.

I based the AnnNumberObject on the AnnTextObject and its Draw Designer on the AnnRectangleDrawDesigner. In the Draw Designer I overrode the OnPointerDown event so that it will cycle through each object in the AnnContainer and increment a counter by 1 for each object it finds, setting the Text property of the NumberObject to that value.

For the NumberObject, I also had to create a custom Edit Designer which I based on the AnnTextEditDesigner so that I can have the font size change as the size of the object changes. To do this, I overrode the OnPointerDown event to capture the starting values for the NumberObject‘s bounds and font size, then I overrode the OnEdit event to do some math to get a ratio to see how the width was being edited and forces the font size to change with it.

The third object I created was the AnnStarObject based on the AnnPolylineObject. This object will let the user click and drag their mouse to set the center and radius of a perfect gold star. To achieve this, I created a custom Draw Designer based on the AnnPolylineDrawDesigner. I overrode the OnPointerDown to set the center of the star then I overrode the OnPointerMove event and did the math necessary to create the perfect star. I calculated the distance between the center point and the current location of the mouse to generate the radius, then used a formula to find the 10 points of the star and add them to the object.

The final object added was the AnnTinyStampObject which is based on the AnnStampObject. This object is very similar to the StampObject except I have it draw the stamp on a single click. To achieve this, I created a custom Draw Designer which isn’t based on another object’s draw designer. I overrode the OnPointerDown event and calculated the 4 points of the rectangle based on the mouse location and added those to the tiny stamp object and ended the designer in the OnPointerUp event.

After all my objects were created, I had to add them to my AnnAutomationManager.AutomationObject list so that the user can access and use them. I created a method to handle adding the objects, as well as setting the ToolbarImage to make it easy for the user to find the object in the toolbar.

Overall this project took me less than a day to write since I had the AnnotationDemo to get started with and a helpful tutorial within the LEADTOOLS documentation about implementing custom annotations located here:

Documentation: Implementing User-Defined Objects With LEADTOOLS Annotations in Windows Forms

Download the Project

The source code for this sample project can be downloaded from here. To run the project, extract it to the C:\LEADTOOLS 19\Examples\DotNet\CS directory.

This entry was posted in Document Imaging and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *