diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index b3bb10ed..069b3e59 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -59,10 +59,15 @@ /// 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. /// - internal Letter(string value, PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, decimal width, decimal fontSize, string fontName, decimal pointSize) + internal Letter(string value, PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, decimal width, decimal fontSize, string fontName, decimal pointSize, int textSequence) { Value = value; GlyphRectangle = glyphRectangle; @@ -71,6 +76,7 @@ PointSize = pointSize; Width = width; StartBaseLine = startBaseLine; + TextSequence = textSequence; EndBaseLine = endBaseLine; TextDirection = GetTextDirection(); } diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 6d4d9aa2..4c40a770 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -30,6 +30,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 @@ -169,7 +172,7 @@ .Transform(TextMatrices.TextMatrix .Transform(renderingMatrix.Transform(new PdfRectangle(0, 0, boundingBox.Width, 0)))); - ShowGlyph(font, transformedGlyphBounds, transformedPdfBounds.BottomLeft, transformedPdfBounds.BottomRight, transformedPdfBounds.Width, unicode, fontSize, pointSize); + ShowGlyph(font, transformedGlyphBounds, transformedPdfBounds.BottomLeft, transformedPdfBounds.BottomRight, transformedPdfBounds.Width, unicode, fontSize, pointSize, textSequence); decimal tx, ty; if (font.IsVertical) @@ -191,6 +194,8 @@ public void ShowPositionedText(IReadOnlyList tokens) { + textSequence++; + var currentState = GetCurrentState(); var textState = currentState.FontState; @@ -336,9 +341,9 @@ TextMatrices.TextMatrix = newMatrix; } - private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, decimal width, string unicode, decimal fontSize, decimal pointSize) + private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, decimal width, string unicode, decimal fontSize, decimal pointSize, int textSequence) { - var letter = new Letter(unicode, glyphRectangle, startBaseLine, endBaseLine, width, fontSize, font.Name.Data, pointSize); + var letter = new Letter(unicode, glyphRectangle, startBaseLine, endBaseLine, width, fontSize, font.Name.Data, pointSize, textSequence); Letters.Add(letter); } diff --git a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs index c1c447dc..193b575d 100644 --- a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs @@ -21,6 +21,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; /// @@ -239,6 +243,8 @@ var width = 0m; + textSequence++; + for (var i = 0; i < text.Length; i++) { var c = text[i]; @@ -258,7 +264,7 @@ var documentSpace = textMatrix.Transform(renderingMatrix.Transform(fontMatrix.Transform(rect))); - var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, advanceRect.BottomRight, width, fontSize, font.Name, fontSize); + var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, advanceRect.BottomRight, width, fontSize, font.Name, fontSize, textSequence); letters.Add(letter); var tx = advanceRect.Width * horizontalScaling;