diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index dc34f3677..24de94bf5 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -65,6 +65,11 @@ public class Letter /// internal decimal PointSize { get; } + /// + /// Sequence number of the ShowText operation that printed this letter. + /// + public int TextSequence { get; } + /// /// Create a new letter to represent some text drawn by the Tj operator. /// @@ -75,7 +80,8 @@ internal Letter(string value, PdfRectangle glyphRectangle, decimal fontSize, string fontName, IColor color, - decimal pointSize) + decimal pointSize, + int textSequence) { Value = value; GlyphRectangle = glyphRectangle; @@ -86,6 +92,7 @@ internal Letter(string value, PdfRectangle glyphRectangle, FontName = fontName; Color = color ?? GrayColor.Black; PointSize = pointSize; + TextSequence = textSequence; TextDirection = GetTextDirection(); } diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 972049bfa..2dfd77f1e 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -31,6 +31,9 @@ internal class ContentStreamProcessor : IOperationContext private Stack graphicsStack = new Stack(); private IFont activeExtendedGraphicsStateFont = null; + //a sequence number of ShowText operation to determine whether letters belong to same operation or not (letters that belong to different operations have less changes to belong to same word) + private int textSequence = 0; + public TextMatrices TextMatrices { get; } = new TextMatrices(); public TransformationMatrix CurrentTransformationMatrix @@ -187,7 +190,8 @@ public void ShowText(IInputBytes bytes) unicode, fontSize, color, - pointSize); + pointSize, + textSequence); decimal tx, ty; if (font.IsVertical) @@ -209,6 +213,8 @@ public void ShowText(IInputBytes bytes) public void ShowPositionedText(IReadOnlyList tokens) { + textSequence++; + var currentState = GetCurrentState(); var textState = currentState.FontState; @@ -361,7 +367,8 @@ private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, string unicode, decimal fontSize, IColor color, - decimal pointSize) + decimal pointSize, + int textSequence) { var letter = new Letter(unicode, glyphRectangle, startBaseLine, @@ -370,7 +377,8 @@ private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, fontSize, font.Name.Data, color, - pointSize); + pointSize, + textSequence); Letters.Add(letter); } diff --git a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs index 0288496ae..32c306044 100644 --- a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs @@ -22,6 +22,10 @@ public class PdfPageBuilder { private readonly PdfDocumentBuilder documentBuilder; private readonly List operations = new List(); + + //a sequence number of ShowText operation to determine whether letters belong to same operation or not (letters that belong to different operations have less changes to belong to same word) + private static int textSequence = 0; + internal IReadOnlyList Operations => operations; /// @@ -240,6 +244,8 @@ private static List DrawLetters(string text, IWritingFont font, Transfor var width = 0m; + textSequence++; + for (var i = 0; i < text.Length; i++) { var c = text[i]; @@ -261,7 +267,9 @@ private static List DrawLetters(string text, IWritingFont font, Transfor var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, advanceRect.BottomRight, width, fontSize, font.Name, GrayColor.Black, - fontSize); + fontSize, + textSequence); + letters.Add(letter); var tx = advanceRect.Width * horizontalScaling;