mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-15 19:54:52 +08:00
lazily evaluate page text and remove linq from word constructor
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Page
|
public class Page
|
||||||
{
|
{
|
||||||
|
private readonly Lazy<string> textLazy;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The raw PDF dictionary token for this page in the document.
|
/// The raw PDF dictionary token for this page in the document.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -41,11 +43,11 @@
|
|||||||
/// The set of <see cref="Letter"/>s drawn by the PDF content.
|
/// The set of <see cref="Letter"/>s drawn by the PDF content.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyList<Letter> Letters => Content?.Letters ?? new Letter[0];
|
public IReadOnlyList<Letter> Letters => Content?.Letters ?? new Letter[0];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The full text of all characters on the page in the order they are presented in the PDF content.
|
/// The full text of all characters on the page in the order they are presented in the PDF content.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Text { get; }
|
public string Text => textLazy.Value;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the width of the page in points.
|
/// Gets the width of the page in points.
|
||||||
@@ -88,7 +90,7 @@
|
|||||||
CropBox = cropBox;
|
CropBox = cropBox;
|
||||||
Rotation = rotation;
|
Rotation = rotation;
|
||||||
Content = content;
|
Content = content;
|
||||||
Text = GetText(content);
|
textLazy = new Lazy<string>(() => GetText(Content));
|
||||||
|
|
||||||
Width = mediaBox.Bounds.Width;
|
Width = mediaBox.Bounds.Width;
|
||||||
Height = mediaBox.Bounds.Height;
|
Height = mediaBox.Bounds.Height;
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Text;
|
||||||
using Geometry;
|
using Geometry;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -53,12 +53,41 @@
|
|||||||
|
|
||||||
Letters = letters;
|
Letters = letters;
|
||||||
|
|
||||||
Text = string.Join(string.Empty, letters.Select(x => x.Value));
|
var builder = new StringBuilder();
|
||||||
|
|
||||||
var minX = letters.Min(x => x.Location.X);
|
var minX = decimal.MaxValue;
|
||||||
var minY = letters.Min(x => x.Location.Y);
|
var minY = decimal.MaxValue;
|
||||||
var maxX = letters.Max(x => x.Location.X + x.Width);
|
var maxX = decimal.MinValue;
|
||||||
var maxY = letters.Max(x => x.GlyphRectangle.Top);
|
var maxY = decimal.MinValue;
|
||||||
|
|
||||||
|
for (var i = 0; i < letters.Count; i++)
|
||||||
|
{
|
||||||
|
var letter = letters[i];
|
||||||
|
builder.Append(letter.Value);
|
||||||
|
|
||||||
|
if (letter.Location.X < minX)
|
||||||
|
{
|
||||||
|
minX = letter.Location.X;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (letter.Location.Y < minY)
|
||||||
|
{
|
||||||
|
minY = letter.Location.Y;
|
||||||
|
}
|
||||||
|
|
||||||
|
var right = letter.Location.X + letter.Width;
|
||||||
|
if (right > maxX)
|
||||||
|
{
|
||||||
|
maxX = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (letter.GlyphRectangle.Top > maxY)
|
||||||
|
{
|
||||||
|
maxY = letter.GlyphRectangle.Top;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Text = builder.ToString();
|
||||||
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
|
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
|
||||||
|
|
||||||
FontName = letters[0].FontName;
|
FontName = letters[0].FontName;
|
||||||
|
Reference in New Issue
Block a user