private void OpenDocument(string fileName) { using (LoadDocument.LoadDocumentDialog dlg = new LoadDocument.LoadDocumentDialog(fileName)) { if (dlg.ShowDialog(this) == DialogResult.OK) { try { using (WaitCursor wait = new WaitCursor()) { PDFDocument document = dlg.PDFDocument; // Parse the document text Dictionary <int, MyWord[]> documentText = MyWord.BuildWord(document); // Initialize the document with annotations SetDocument(document, documentText, fileName); } } catch (Exception ex) { Messager.ShowError(this, ex.Message); } finally { UpdateUIState(); } } } }
private void CopySelectedText() { // Convert the selected words to a single string MyWord[] words = _selectedText[_currentPageNumber]; if (words != null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { MyWord word = words[i]; sb.Append(word.Value); if (word.IsEndOfLine) { sb.AppendLine(); } else if (i != (words.Length - 1)) { sb.Append(" "); } } Clipboard.SetText(sb.ToString()); } }
private string BuildPageText(MyWord[] words) { if (words == null || words.Length == 0) { return(string.Empty); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { MyWord word = words[i]; sb.Append(word.Value); if (word.IsEndOfLine) { sb.AppendLine(); } else if (i != (words.Length - 1)) { sb.Append(" "); } } return(sb.ToString()); }
private void SelectAllText() { if (_document == null || _documentText[_currentPageNumber] == null) { return; } // Get the text for the current page _selectedText[_currentPageNumber] = new MyWord[_documentText[_currentPageNumber].Length]; _documentText[_currentPageNumber].CopyTo(_selectedText[_currentPageNumber], 0); // Select it all (by replacing the selected text for the page for all text in the page) // Re-paint the viewer _viewerControl.RasterImageViewer.Invalidate(); }
private void BuildWordItems(Dictionary <int, MyWord[]> documentText, int pageNumber) { List <FindWordItem> findWordItems = new List <FindWordItem>(); MyWord[] words = null; if (documentText[pageNumber] != null) { words = documentText[pageNumber]; } if (words != null) { int beginIndex = 0; StringBuilder sb = new StringBuilder(); for (int wordIndex = 0; wordIndex < words.Length; wordIndex++) { MyWord word = words[wordIndex]; sb.Append(word.Value); // Add it as a FindWordItem FindWordItem item = new FindWordItem(); item.BeginTextIndex = beginIndex; item.EndTextIndex = beginIndex + word.Value.Length - 1; item.WordIndex = wordIndex; findWordItems.Add(item); sb.Append(" "); beginIndex = item.EndTextIndex + 2; } _currentPageText = sb.ToString(); } else { _currentPageText = string.Empty; } _findWordItems = findWordItems.ToArray(); }
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); }