LEADTOOLS Support
Document
Document SDK Questions
Programatically Created text annotation is not visible in the file
#1
Posted
:
Thursday, June 11, 2020 9:07:40 AM(UTC)
Groups: Registered
Posts: 60
I'm creating a text annotation programmatically but it does not gets displayed in the file. Here is the code:
Code:
using (RasterCodecs acodecs = new RasterCodecs())
{
acodecs.Options.Pdf.InitialPath = @$"{currentPath}\C DLL";
var aimage = acodecs.Load(files[0]);
var textAnnotation = new AnnTextObject();
textAnnotation.Font.FontSize = 12;
textAnnotation.Text = "Programatically created";
textAnnotation.HorizontalAlignment = AnnHorizontalAlignment.Left;
textAnnotation.VerticalAlignment = AnnVerticalAlignment.Bottom;
textAnnotation.Font.FontFamilyName = "Arial";
textAnnotation.Guid = Guid.NewGuid().ToString();
textAnnotation.Font.FontStyle = AnnFontStyle.Italic;
textAnnotation.IsVisible = true;
//textAnnotation.TextForeground = color to set
//textObj.Points.Add(LeadPointD.Create(1 * inch, 1 * inch));
//textObj.Points.Add(LeadPointD.Create(2 * inch, 1 * inch));
//textObj.Points.Add(LeadPointD.Create(2 * inch, 2 * inch));
//textObj.Points.Add(LeadPointD.Create(1 * inch, 2 * inch));
AnnCodecs annCodecs = new AnnCodecs();
AnnContainer container = new AnnContainer();
int imageHeight = aimage.ImageHeight / aimage.GetImageHeightDpi(true);
int imageWidth = aimage.ImageWidth / aimage.GetImageWidthDpi(true);
container.Size = LeadSizeD.Create(imageWidth, imageHeight);
container.Children.Add(textAnnotation);
annCodecs.Save(@$"{currentPath}\Image To Pdf\timestamp.xml", container, AnnFormat.Annotations, 1);
container = annCodecs.Load(@$"{currentPath}\Image To Pdf\timestamp.xml", 1);
AnnDrawRenderingEngine renderingEngine = new AnnDrawRenderingEngine();
//renderingEngine.RenderOnImage(container, aimage);
var burnedImage = RasterImage.Create(aimage.Width, aimage.Height, aimage.BitsPerPixel, aimage.XResolution, RasterColor.Black);
burnedImage = renderingEngine.RenderOnImage(container, aimage);
acodecs.Save(burnedImage, @$"{currentPath}\Image To Pdf\timestamp.pdf", RasterImageFormat.RasPdf, 0);
}
Please assist if I'm missing anything.
Thanks,
Abdul
#2
Posted
:
Friday, June 12, 2020 8:31:55 AM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Hello Abdul,
After reviewing your code snippet above I was able to pinpoint the issue. There are actually two aspects that are not allowing you to see the burned AnnTextObject. First, you commented out the section of the code where you set the bounding rectangle bounds. When you do not set these or set the bounding LeadRectD, your annotation is going to have an empty rectangle with a height and width of 0. Second, The below line is causing the AnnContainer to have a size of (3, 3) or something similar with your test files (Either way this is too small).
Code:
int imageHeight = aimage.ImageHeight / aimage.GetImageHeightDpi(true);
int imageWidth = aimage.ImageWidth / aimage.GetImageWidthDpi(true);
container.Size = LeadSizeD.Create(imageWidth, imageHeight);
I believe I provided you those lines of code, which worked for what we had worked on previously, however does not seem to be efficient enough. I would recommend setting the AnnContainer size as I have done below and then use the AnnContainerMapper to map the container to the specific image resolution:
Code:
var container = new Leadtools.Annotations.Engine.AnnContainer();
container.Size = new LeadSizeD(image.ImageWidth, image.ImageHeight);
container.Mapper.MapResolutions(image.XResolution, image.YResolution, image.XResolution, image.YResolution);
So you should be able to burn you AnnTextObject that you created to an image with the below code:
Code:
double inch = 720.0;
AnnDrawRenderingEngine _renderingEngine = new AnnDrawRenderingEngine();
string input = @"INPUT PATH";
RasterImage image = codecs.Load(input);
var container = new AnnContainer();
container.Size = new LeadSizeD(image.ImageWidth, image.ImageHeight);
var textAnnotation = new AnnTextObject();
textAnnotation.Font.FontSize = 12;
textAnnotation.Text = "Programatically created";
textAnnotation.HorizontalAlignment = AnnHorizontalAlignment.Left;
textAnnotation.VerticalAlignment = AnnVerticalAlignment.Bottom;
textAnnotation.Font.FontFamilyName = "Arial";
textAnnotation.Guid = Guid.NewGuid().ToString();
textAnnotation.Font.FontStyle = AnnFontStyle.Italic;
textAnnotation.IsVisible = true;
// Set the points for the hotspot
textAnnotation.Points.Add(LeadPointD.Create(1 * inch, 1 * inch));
textAnnotation.Points.Add(LeadPointD.Create(2 * inch, 1 * inch));
textAnnotation.Points.Add(LeadPointD.Create(2 * inch, 2 * inch));
textAnnotation.Points.Add(LeadPointD.Create(1 * inch, 2 * inch));
container.Mapper.MapResolutions(image.XResolution, image.YResolution, image.XResolution, image.YResolution);
container.Children.Add(textAnnotation);
var burnedImage = RasterImage.Create(image.Width, image.Height, image.BitsPerPixel, image.XResolution, RasterColor.Black);
burnedImage = _renderingEngine.RenderOnImage(container, image);
string output = @"OUTPUT PATH";
codecs.Save(burnedImage, output, RasterImageFormat.Tif, 0);
If you have any further questions please feel free to reach back out to me.
Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
#3
Posted
:
Friday, June 12, 2020 10:19:28 AM(UTC)
Groups: Registered
Posts: 60
Dear Matt,
Why is inch always set to 720? and how points are calculated? the annotation is now rendered but it is not appearing on the bottom left Also how to change the text color?. I need the text annotation to appear on the bottom left of the first page. Please assist.
Thanks,
Abdul
#4
Posted
:
Friday, June 12, 2020 1:48:57 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Hello Abdul,
I passed in 720 as the integer just because I needed a number to set inside the points to create the rectangle bounds of the AnnTextObject. The annotation did not appear at the bottom left because that is not what the HorizontalAlignment and Vertical Alignment does. Those properties decide the alignment of the text inside the text box and not the alignment on the image. If you want to position the AnnTextObject in the bottom left corner of the image you will need to set the bounding rectangle using the below options:
Code:
// Set the points for the hotspot
textAnnotation.Points.Add(LeadPointD.Create(x, y));
textAnnotation.Points.Add(LeadPointD.Create(x, y));
textAnnotation.Points.Add(LeadPointD.Create(x, y));
textAnnotation.Points.Add(LeadPointD.Create(x, y));
or
Code:
textAnnotation.Rect = new LeadRectD(x, y, width, height);
If you want to change the color of the text inside the AnnTextObject you can use the TextForeground property.
Code:
textAnnotation.TextForeground = AnnSolidColorBrush.Create("Blue");
If you have any further questions please let me know.
Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
#5
Posted
:
Friday, June 12, 2020 3:15:29 PM(UTC)
Groups: Registered
Posts: 60
Thanks Matt. That works like a charm. One final question. How to change border color or make border transparent?
#6
Posted
:
Friday, June 12, 2020 3:46:07 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Hello Abdul,
To change the color of the border you will need to use the Stroke property:
https://www.leadtools.co...ac/annobject-stroke.htmlCode:
textAnnotation.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("White"), new LeadLengthD(2));
Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Document
Document SDK Questions
Programatically Created text annotation is not visible in the file
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.