mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-22 20:13:58 +08:00
- Correction of the PdfLine's length formula;
- Moving Line to TextLine
This commit is contained in:
75
src/UglyToad.PdfPig/Content/TextLine.cs
Normal file
75
src/UglyToad.PdfPig/Content/TextLine.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UglyToad.PdfPig.Geometry;
|
||||
|
||||
namespace UglyToad.PdfPig.Content
|
||||
{
|
||||
/// <summary>
|
||||
/// A line of text.
|
||||
/// </summary>
|
||||
public class TextLine
|
||||
{
|
||||
/// <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>
|
||||
/// Create a new <see cref="TextLine"/>.
|
||||
/// </summary>
|
||||
/// <param name="words">The words contained in the word.</param>
|
||||
public TextLine(IReadOnlyList<Word> words)
|
||||
{
|
||||
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);
|
||||
|
||||
if (words.All(x => x.TextDirection == words[0].TextDirection))
|
||||
{
|
||||
TextDirection = words[0].TextDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
TextDirection = TextDirection.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return Text;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user