2019-05-12 19:34:00 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-05-14 20:56:34 +01:00
|
|
|
|
using UglyToad.PdfPig.Geometry;
|
2019-05-12 19:34:00 +01:00
|
|
|
|
|
|
|
|
|
namespace UglyToad.PdfPig.Content
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-05-14 20:56:34 +01:00
|
|
|
|
/// A line of text.
|
2019-05-12 19:34:00 +01:00
|
|
|
|
/// </summary>
|
2019-05-14 20:56:34 +01:00
|
|
|
|
public class TextLine
|
2019-05-12 19:34:00 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text of the line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Text { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text direction of the line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TextDirection TextDirection { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The rectangle completely containing the line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public PdfRectangle BoundingBox { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The words contained in the line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<Word> Words { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-05-14 20:56:34 +01:00
|
|
|
|
/// Create a new <see cref="TextLine"/>.
|
2019-05-12 19:34:00 +01:00
|
|
|
|
/// </summary>
|
2019-05-14 20:56:34 +01:00
|
|
|
|
/// <param name="words">The words contained in the word.</param>
|
|
|
|
|
public TextLine(IReadOnlyList<Word> words)
|
2019-05-12 19:34:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (words == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(words));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (words.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Empty words provided.", nameof(words));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Words = words;
|
|
|
|
|
|
|
|
|
|
Text = string.Join(" ", words.Where(s => !string.IsNullOrWhiteSpace(s.Text)).Select(x => x.Text));
|
|
|
|
|
|
|
|
|
|
var minX = words.Min(x => x.BoundingBox.Left);
|
|
|
|
|
var minY = words.Min(x => x.BoundingBox.Bottom);
|
|
|
|
|
var maxX = words.Max(x => x.BoundingBox.Right);
|
|
|
|
|
var maxY = words.Max(x => x.BoundingBox.Top);
|
|
|
|
|
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
|
|
|
|
|
|
2019-05-14 20:56:34 +01:00
|
|
|
|
if (words.All(x => x.TextDirection == words[0].TextDirection))
|
|
|
|
|
{
|
|
|
|
|
TextDirection = words[0].TextDirection;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TextDirection = TextDirection.Unknown;
|
|
|
|
|
}
|
2019-05-12 19:34:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|