using System; using System.Collections.Generic; using System.Linq; using UglyToad.PdfPig.Content; using UglyToad.PdfPig.Geometry; namespace UglyToad.PdfPig.DocumentLayoutAnalysis { /// /// A Leaf node used in the algorithm, i.e. a block. /// public class XYLeaf : XYNode { /// /// Returns true if this node is a leaf, false otherwise. /// public override bool IsLeaf => true; /// /// The words in the leaf. /// public IReadOnlyList Words { get; } /// /// The number of words in the leaf. /// public override int CountWords() => Words == null ? 0 : Words.Count; /// /// Returns null as a leaf doesn't have leafs. /// public override List GetLeafs() { return null; } /// /// Gets the lines of the leaf. /// public IReadOnlyList GetLines() { return Words.GroupBy(x => x.BoundingBox.Bottom).OrderByDescending(x => x.Key) .Select(x => new TextLine(x.ToList())).ToArray(); } /// /// Create a new . /// /// The words contained in the leaf. public XYLeaf(params Word[] words) : this(words == null ? null : words.ToList()) { } /// /// Create a new . /// /// The words contained in the leaf. public XYLeaf(IEnumerable words) : base(null) { if (words == null) { throw new ArgumentException("XYLeaf(): The words contained in the leaf cannot be null.", "words"); } decimal left = words.Min(b => b.BoundingBox.Left); decimal right = words.Max(b => b.BoundingBox.Right); decimal bottom = words.Min(b => b.BoundingBox.Bottom); decimal top = words.Max(b => b.BoundingBox.Top); BoundingBox = new PdfRectangle(left, bottom, right, top); Words = words.ToArray(); } } }