diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index dc34f367..24de94bf 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -65,6 +65,11 @@ /// 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 @@ decimal fontSize, string fontName, IColor color, - decimal pointSize) + decimal pointSize, + int textSequence) { Value = value; GlyphRectangle = glyphRectangle; @@ -86,6 +92,7 @@ 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 972049bf..2dfd77f1 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -31,6 +31,9 @@ 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 @@ unicode, fontSize, color, - pointSize); + pointSize, + textSequence); decimal tx, ty; if (font.IsVertical) @@ -209,6 +213,8 @@ public void ShowPositionedText(IReadOnlyList tokens) { + textSequence++; + var currentState = GetCurrentState(); var textState = currentState.FontState; @@ -361,7 +367,8 @@ string unicode, decimal fontSize, IColor color, - decimal pointSize) + decimal pointSize, + int textSequence) { var letter = new Letter(unicode, glyphRectangle, startBaseLine, @@ -370,7 +377,8 @@ 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 0288496a..32c30604 100644 --- a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs @@ -22,6 +22,10 @@ { 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 @@ var width = 0m; + textSequence++; + for (var i = 0; i < text.Length; i++) { var c = text[i]; @@ -261,7 +267,9 @@ 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;