namespace UglyToad.PdfPig.DocumentLayoutAnalysis { using Content; using Core; using System; using System.Collections.Generic; using System.Linq; /// /// A block of text. /// public class TextBlock { /// /// The separator used between lines in the block. /// public readonly string Separator; /// /// The text of the block. /// public string Text { get; } /// /// The text orientation of the block. /// public TextOrientation TextOrientation { get; } /// /// The rectangle completely containing the block. /// public PdfRectangle BoundingBox { get; } /// /// The text lines contained in the block. /// public IReadOnlyList TextLines { get; } /// /// The reading order index. Starts at 0. A value of -1 means the block is not ordered. /// public int ReadingOrder { get; private set; } /// /// Create a new . /// /// The words contained in the line, in the correct order. /// The separator used between lines in the block. public TextBlock(IReadOnlyList lines, string separator = "\n") { if (lines == null) { throw new ArgumentNullException(nameof(lines)); } if (lines.Count == 0) { throw new ArgumentException("Empty lines provided.", nameof(lines)); } Separator = separator; ReadingOrder = -1; TextLines = lines; Text = string.Join(" ", lines.Select(x => x.Text)); var minX = lines.Min(x => x.BoundingBox.Left); var minY = lines.Min(x => x.BoundingBox.Bottom); var maxX = lines.Max(x => x.BoundingBox.Right); var maxY = lines.Max(x => x.BoundingBox.Top); BoundingBox = new PdfRectangle(minX, minY, maxX, maxY); TextOrientation = lines[0].TextOrientation; } /// /// Sets the 's reading order. /// /// public void SetReadingOrder(int readingOrder) { if (readingOrder < -1) { throw new ArgumentException("The reading order should be more or equal to -1. A value of -1 means the block is not ordered.", nameof(readingOrder)); } this.ReadingOrder = readingOrder; } /// public override string ToString() { return Text; } } }