PDFObject HitTestPDFImageObject(LeadPoint pt) { PDFObject retObject = new PDFObject(); PDFDocumentPage page = _document.Pages[_currentPageNumber - 1]; if (page.Objects != null && page.Objects.Count > 0) { foreach (PDFObject obj in page.Objects) { if (obj.ObjectType == PDFObjectType.Image) { PDFRect tempPDFRect = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds); LeadRect objectRect = ToLeadRect(tempPDFRect); if (objectRect.Contains(pt)) { // Point in rect then user pressed over image object, so return it. retObject = obj; break; } } } } return(retObject); }
private void _rasterImageViewer_PostImagePaint(object sender, PaintEventArgs e) { if (_selectedText != null && _selectedText[_currentPageNumber] != null) { HighlightSelectedWords(e.Graphics); } if (HighlightObjects && _document != null) { PDFDocumentPage page = _document.Pages[_currentPageNumber - 1]; HighlightObjectsData data = new HighlightObjectsData(); data.TextBrush = new SolidBrush(Color.FromArgb(128, Color.Yellow)); data.RetangleBrush = new SolidBrush(Color.FromArgb(128, Color.Black)); data.ImageBrush = new SolidBrush(Color.FromArgb(128, Color.Red)); data.HyperlinkBrush = new SolidBrush(Color.FromArgb(128, Color.Blue)); data.InternalLinkBrush = new SolidBrush(Color.FromArgb(128, Color.Green)); DrawHighlightObjects(e.Graphics, data, page); DrawLegends(e.Graphics, data); data.TextBrush.Dispose(); data.RetangleBrush.Dispose(); data.ImageBrush.Dispose(); data.HyperlinkBrush.Dispose(); data.InternalLinkBrush.Dispose(); } }
/// <summary> /// Called by the main form when the page number of the image is changed /// </summary> public void SetCurrentPageNumber(int pageNumber, RasterImage pageImage) { _currentPageNumber = pageNumber; ImageViewerAutoResetOptions savedOptions = _rasterImageViewer.AutoResetOptions; _rasterImageViewer.AutoResetOptions = ImageViewerAutoResetOptions.None; _rasterImageViewer.Image = pageImage; _rasterImageViewer.AutoResetOptions = savedOptions; if (_isNewDocument) { // Fit page when new document is set _isNewDocument = false; FitPage(false); } // Build the page internal links _pageLinks = new List <PageLink>(); // Loop through the document internal links if (_document != null) { // Get the page PDFDocumentPage page = _document.Pages[_currentPageNumber - 1]; if (_document.InternalLinks != null) { for (int i = 0; i < _document.InternalLinks.Count; i++) { if (_document.InternalLinks[i].SourcePageNumber == pageNumber) { // Our page PageLink link = new PageLink(); link.InternalLinkIndex = i; link.HyperLinkIndex = -1; // The internal links bounds are in PDF page coordinates, not objects link.ImageBounds = ToLeadRect(page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, _document.InternalLinks[i].SourceBounds)); _pageLinks.Add(link); } } } // Now add all hyperlinks if (page.Hyperlinks != null) { for (int i = 0; i < page.Hyperlinks.Count; i++) { PageLink link = new PageLink(); link.HyperLinkIndex = i; link.InternalLinkIndex = -1; link.ImageBounds = ToLeadRect(page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, page.Hyperlinks[i].Bounds)); _pageLinks.Add(link); } } } UpdatePageInfo(); UpdateUIState(); }
private void DrawHighlightObjects(Graphics g, HighlightObjectsData data, PDFDocumentPage page) { LeadMatrix mm = _rasterImageViewer.GetImageTransformWithDpi(true); Matrix m = new Matrix((float)mm.M11, (float)mm.M12, (float)mm.M21, (float)mm.M22, (float)mm.OffsetX, (float)mm.OffsetY); Transformer trans = new Transformer(m); // Clip to the current image bounds RectangleF clipRect = new RectangleF(0, 0, _rasterImageViewer.Image.ImageWidth, _rasterImageViewer.Image.ImageHeight); clipRect = trans.RectangleToPhysical(clipRect); g.SetClip(clipRect); // Draw objects if (page.Objects != null) { foreach (PDFObject obj in page.Objects) { RectangleF rc = ToRectangleF(page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds)); rc = trans.RectangleToPhysical(rc); // Highlight it Brush brush; if (obj.ObjectType == PDFObjectType.Image) { brush = data.ImageBrush; } else if (obj.ObjectType == PDFObjectType.Rectangle) { brush = data.RetangleBrush; } else { brush = data.TextBrush; } DrawRectangle(g, brush, rc); } } // Draw internal and hyper links foreach (PageLink link in _pageLinks) { RectangleF rc = trans.RectangleToPhysical(new RectangleF(link.ImageBounds.X, link.ImageBounds.Y, link.ImageBounds.Width, link.ImageBounds.Height)); Brush brush; if (link.InternalLinkIndex != -1) { brush = data.InternalLinkBrush; } else { brush = data.HyperlinkBrush; } DrawRectangle(g, brush, rc); } }
private void UpdatePageInfo() { StringBuilder sb = new StringBuilder(); if (_document != null) { PDFDocumentPage page = _document.Pages[_currentPageNumber - 1]; sb.AppendFormat(DemosGlobalization.GetResxString(GetType(), "resx_SizeBy"), page.WidthPixels, page.HeightPixels, page.WidthInches.ToString("F02"), page.HeightInches.ToString("F02")); } _pageInfoLabel.Text = sb.ToString(); }
private void DrawHighlightImageObject(Graphics g, Brush imageBrush, PDFDocumentPage page) { LeadMatrix mm = _rasterImageViewer.GetImageTransformWithDpi(true); Matrix m = new Matrix((float)mm.M11, (float)mm.M12, (float)mm.M21, (float)mm.M22, (float)mm.OffsetX, (float)mm.OffsetY); Transformer trans = new Transformer(m); // Clip to the current image bounds RectangleF clipRect = new RectangleF(0, 0, _rasterImageViewer.Image.ImageWidth, _rasterImageViewer.Image.ImageHeight); clipRect = trans.RectangleToPhysical(clipRect); g.SetClip(clipRect); // Draw image object RectangleF rc = ToRectangleF(page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, _selectedPdfImageObject.Bounds)); if (!rc.IsEmpty) { rc = trans.RectangleToPhysical(rc); // Highlight it DrawRectangle(g, imageBrush, rc); } }
void _rasterImageViewer_PostRender(object sender, Leadtools.Controls.ImageViewerRenderEventArgs e) { PDFDocumentPage page = _document.Pages[_currentPageNumber - 1]; if (_selectedText != null && _selectedText[_currentPageNumber] != null) { HighlightSelectedWords(e.PaintEventArgs.Graphics); } if (HighlightObjects && _document != null) { HighlightObjectsData data = new HighlightObjectsData(); data.TextBrush = new SolidBrush(Color.FromArgb(128, Color.Yellow)); data.RetangleBrush = new SolidBrush(Color.FromArgb(128, Color.Black)); data.ImageBrush = new SolidBrush(Color.FromArgb(128, Color.Red)); data.HyperlinkBrush = new SolidBrush(Color.FromArgb(128, Color.Blue)); data.InternalLinkBrush = new SolidBrush(Color.FromArgb(128, Color.Green)); DrawHighlightObjects(e.PaintEventArgs.Graphics, data, page); DrawLegends(e.PaintEventArgs.Graphics, data); data.TextBrush.Dispose(); data.RetangleBrush.Dispose(); data.ImageBrush.Dispose(); data.HyperlinkBrush.Dispose(); data.InternalLinkBrush.Dispose(); } if (_highlightSelectedImageObject && !HighlightObjects) { using (Brush imageBrush = new SolidBrush(Color.FromArgb(128, Color.Red))) { DrawHighlightImageObject(e.PaintEventArgs.Graphics, imageBrush, page); } } }
public static Dictionary <int, MyWord[]> BuildWord(PDFDocument document) { Dictionary <int, MyWord[]> pageWords = new Dictionary <int, MyWord[]>(); for (int pageNumber = 1; pageNumber <= document.Pages.Count; pageNumber++) { List <MyWord> words = new List <MyWord>(); PDFDocumentPage page = document.Pages[pageNumber - 1]; IList <PDFObject> objects = page.Objects; if (objects != null && objects.Count > 0) { int objectIndex = 0; int objectCount = objects.Count; // Loop through all the objects while (objectIndex < objectCount) { // Find the total bounding rectangle, begin and end index of the next word LeadRect wordBounds = LeadRect.Empty; int firstObjectIndex = objectIndex; // Loop till we reach EndOfWord or reach the end of the objects bool more = true; while (more) { PDFObject obj = objects[objectIndex]; // Must be text and not a white character if (obj.ObjectType == PDFObjectType.Text && !Char.IsWhiteSpace(obj.Code)) { // Add the bounding rectangle of this object PDFRect temp = page.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, obj.Bounds); LeadRect objectBounds = LeadRect.FromLTRB((int)temp.Left, (int)temp.Top, (int)temp.Right, (int)temp.Bottom); if (wordBounds.IsEmpty) { wordBounds = objectBounds; } else { wordBounds = LeadRect.Union(wordBounds, objectBounds); } } else { firstObjectIndex = objectIndex + 1; } objectIndex++; more = (objectIndex < objectCount) && !obj.TextProperties.IsEndOfWord && !obj.TextProperties.IsEndOfLine; } if (firstObjectIndex == objectIndex) { continue; } // From the begin and end index, collect the characters into a string StringBuilder sb = new StringBuilder(); for (int i = firstObjectIndex; i < objectIndex; i++) { if (objects[i].ObjectType == PDFObjectType.Text) { sb.Append(objects[i].Code); } } // Add this word to the list PDFObject lastObject = objects[objectIndex - 1]; MyWord word = new MyWord(); word.Value = sb.ToString(); word.Bounds = wordBounds; word.IsEndOfLine = lastObject.TextProperties.IsEndOfLine; words.Add(word); } } // Add "IsEndOfLine" to the last word in the page, just in case it does not have it if (words.Count > 0) { MyWord word = words[words.Count - 1]; word.IsEndOfLine = true; words[words.Count - 1] = word; } pageWords.Add(pageNumber, words.ToArray()); } return(pageWords); }