private void _getTextToolStripMenuItem_Click(object sender, EventArgs e) { var document = _viewer.CurrentDocument; if (document != null) { var documentText = DocumentText.FromSvgDocument(document); if (documentText != null) { documentText.BuildWords(); _viewer.DocumentList[_viewer.CurrentPageIndex].DocumentText = documentText; _viewer.DocumentList[_viewer.CurrentPageIndex].ShowText = _selectTextToolStripMenuItem.Checked; _viewer.Invalidate(); UpdateControls(); } } }
public SvgDocumentInformation(SvgDocument document) { _document = document; _documentText = null; _showText = false; }
public static DocumentText FromSvgDocument(SvgDocument document) { // Out list of charcaters var characters = new List <DocumentCharacter>(); // To strip whitespaces var hasWhitespaces = false; var prevCharacter = new DocumentCharacter(); // Callback SvgSortElementsCallback sortCallback = (callabackDocument, info, userData) => { // Is it text? var textData = info.TextData; if (textData != null) { // yes, does it have any characters? var length = textData.Text.Length; for (var i = 0; i < length; i++) { // Yes, convert to DocumentCharacter and add them to the list var flags = textData.CharacterFlags[i]; var positions = DocumentCharacterPositions.None; if ((flags & SvgTextCharacterFlags.EndOfWord) == SvgTextCharacterFlags.EndOfWord) { positions |= DocumentCharacterPositions.EndOfWord; } if ((flags & SvgTextCharacterFlags.EndOfLine) == SvgTextCharacterFlags.EndOfLine) { positions |= DocumentCharacterPositions.EndOfLine; } var character = new DocumentCharacter { Code = textData.Text[i], Bounds = textData.Bounds[i], Positions = positions, Direction = textData.Directions[i] }; // If the character duplicated, then remove the old one if (prevCharacter.Bounds == character.Bounds && prevCharacter.Code == character.Code && prevCharacter.Direction == character.Direction) { characters.Remove(prevCharacter); } characters.Add(character); if (!hasWhitespaces && Char.IsWhiteSpace(character.Code)) { hasWhitespaces = true; } prevCharacter = character; } } return(true); }; // Accept text elements only. SvgFilterElementsCallback filterElementsCallback = (svgDocument, nodeHandle, userData) => { // Accept text elements only. if (nodeHandle.ElementType != SvgElementType.Text) { return(false); } return(true); }; // Setup options and sort var options = new SvgSortOptions { ExtractText = SvgExtractText.Character, SortFlags = SvgSortFlags.Default }; document.SetFilterElementsCallback(filterElementsCallback, null); document.SortElements(options, sortCallback, null); document.SetFilterElementsCallback(null, null); // Characters is set, before using it, strip the whitespaces if (hasWhitespaces) { var temp = new List <DocumentCharacter>(); var count = characters.Count; var positions = DocumentCharacterPositions.None; var newIndex = -1; for (var index = 0; index < count; index++) { var character = characters[index]; // If this character is a whitespace, dont add it, just steal its position and add it to the previous character if (Char.IsWhiteSpace(character.Code)) { positions |= character.Positions; } else { // If have stolen flags from dropping white space characters, add them to the last non-white space character if (positions != DocumentCharacterPositions.None) { if (newIndex != -1) { var tempCharacter = temp[newIndex]; tempCharacter.Positions |= positions; temp[newIndex] = tempCharacter; } positions = DocumentCharacterPositions.None; } // Add it temp.Add(character); newIndex++; } } characters = temp; } var result = new DocumentText(characters); return(result); }