VintaSoft Annotation .NET Plug-in Discussions
Questions, comments and suggestions concerning VintaSoft Annotation .NET Plug-in.
Board index < VintaSoft Imaging < VintaSoft Annotation .NET Plug-in Discussions
public string GetRegionTextPage(
Vintasoft.Imaging.Pdf.Tree.PdfPage page,
Vintasoft.Imaging.UI.ImageViewer imageViewer,
System.Drawing.Rectangle selectedRegion)
{
// convert the rectangle from the control coordinates to the image coordinates
System.Drawing.RectangleF imageCoordinateSystemRectangle =
imageViewer.RectangleToImage(selectedRegion);
// get left-top point of the rectangle
System.Drawing.PointF pdfPageCoordinateSystemPoint1 = imageCoordinateSystemRectangle.Location;
// get rigth-bottom point of the rectangle
System.Drawing.PointF pdfPageCoordinateSystemPoint2 =
new System.Drawing.PointF(imageCoordinateSystemRectangle.Right, imageCoordinateSystemRectangle.Bottom);
// get resolution of the image
Vintasoft.Imaging.Resolution resolution = imageViewer.Image.Resolution;
// convert points from the image coordinate space to the page coordinate space
page.PointToUnit(ref pdfPageCoordinateSystemPoint1, resolution);
page.PointToUnit(ref pdfPageCoordinateSystemPoint2, resolution);
// create rectangle in the page's coordinate space
System.Drawing.RectangleF rectangle = new System.Drawing.RectangleF(new PointF(pdfPageCoordinateSystemPoint1.X, pdfPageCoordinateSystemPoint1.Y),
new System.Drawing.SizeF(
pdfPageCoordinateSystemPoint2.X - pdfPageCoordinateSystemPoint1.X,
pdfPageCoordinateSystemPoint2.Y - pdfPageCoordinateSystemPoint1.Y));
// get text region of the page
Vintasoft.Imaging.Text.TextRegion textRegion = page.TextRegion.GetSubregion(
rectangle,
Vintasoft.Imaging.Text.TextSelectionMode.Rectangle);
string textContent = string.Empty;
// if text region is found
if (textRegion != null)
textContent = textRegion.TextContent;
return textContent;
}
and I am calling this methodGetRegionTextPage(page, _annotationViewer, new Rectangle(new Point((int)_annotationViewer.AnnotationDataCollection[0].Location.X, (int)_annotationViewer.AnnotationDataCollection[0].Location.Y), new Size((int)_annotationViewer.AnnotationDataCollection[0].Size.Width, (int)_annotationViewer.AnnotationDataCollection[0].Size.Height)));where _annotationViewer.AnnotationDataCollection[0] is the rectangle annotation that I am using to mark text in pdf. All the time as result of this method I am getting text that is lower to the left from the text I want to read. Does anyone know what I am missing here? Thanks.
GetRegionTextPage(page, _annotationViewer, new Rectangle(new Point((int)_annotationViewer.AnnotationDataCollection[0].Location.X, (int)_annotationViewer.AnnotationDataCollection[0].Location.Y), ...you are specifing the "selectedRegion" parameter in annotation coordinate space.
...
MessageBox.Show(GetTextByAnnotation(annotationViewer1.Image, (RectangleAnnotationData)annotationViewer1.FocusedAnnotationData));
...
/// <summary>
/// Extracts the text that is located in specified rectangle.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="rect">The rectange, in DIP (Device Indepened Pixels) space.</param>
public static string ExtractText(
Vintasoft.Imaging.VintasoftImage image,
RectangleF rect)
{
// get text region of image
Vintasoft.Imaging.Text.TextRegion textRegion = image.Metadata.TextRegion;
if (textRegion == null)
return "";
// transform rect to TextRegion space
rect = Vintasoft.Imaging.Utils.GraphicsUtils.TransformRect(rect, textRegion.TrasformFromDipSpace);
// get text sub region for specified rect
textRegion = textRegion.GetSubregion(rect, Vintasoft.Imaging.Text.TextSelectionMode.Rectangle);
// return text
return textRegion.TextContent;
}
public static string GetTextByAnnotation(
Vintasoft.Imaging.VintasoftImage image,
Vintasoft.Imaging.Annotation.RectangleAnnotationData annotation)
{
// rect in DIP space
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(
annotation.Location.X - annotation.Size.Width / 2,
annotation.Location.Y - annotation.Size.Height / 2,
annotation.Size.Width,
annotation.Size.Height);
// extract text
return ExtractText(image, rect);
}
Best regards, Alexander