如何解决iText 7 获取所有文本块及其位置
我正在尝试使用 iText7 获取所有单独的文本夹头及其位置。我需要解析夹头并让用户选择要从未来相同的 PDF 中解析的特定文本。我需要让用户选择员工姓名、ID 和其他文本。然后我将存储该数据的位置以用于处理未来的 PDF。选择/映射是一次性设置。以下是我在 iText 5 中返回的数据示例:
X | Y | 块文本 |
---|---|---|
1.763889 | 282.9278 | 11225 north Sourth St,bedrock,ND,99780,Ph: 999 321-6543 |
15.166975 | 277.17752 | 员工 ID |
67.3675 | 277.17752 | 员工姓名 |
18.458744 | 272.22098 | 001159 |
68.33058 | 272.22098 | Fred Flintstone |
以下代码适用于 iText 5,我正在尝试将其迁移到 iText 7 以用于项目。 iText 7 中不再公开某些对象。
void Main()
{
string filename = @"C:\Employee Info.pdf";
using (var reader = new PdfReader(filename))
{
var parser = new PdfReaderContentParser(reader);
var strategy = parser.ProcessContent(1,new LocationTextExtractionStrategyWithPosition());
// work with the locatins on the chucks to provide list to user.
reader.Close();
}
}
public class LocationTextExtractionStrategyWithPosition : LocationTextExtractionStrategy
{
public readonly List<TextChunk> locationalResult = new List<TextChunk>();
private readonly ITextChunkLocationStrategy tclStrat;
public LocationTextExtractionStrategyWithPosition() : this(new TextChunkLocationStrategyDefaultImp())
{
}
public LocationTextExtractionStrategyWithPosition(ITextChunkLocationStrategy strat)
{
tclStrat = strat;
}
public override void RenderText(TextRenderInfo renderInfo)
{
Linesegment segment = renderInfo.GetBaseline();
if (renderInfo.GetRise() != 0)
{ // remove the rise from the baseline - we do this because the text from a super/subscript render operations should probably be considered as part of the baseline of the text the super/sub is relative to
Matrix riSEOffsetTransform = new Matrix(0,-renderInfo.GetRise());
segment = segment.TransformBy(riSEOffsetTransform);
}
TextChunk tc = new TextChunk(renderInfo.GetText(),tclStrat.CreateLocation(renderInfo,segment));
//tc.Dump();
locationalResult.Add(tc);
}
}
public class TextLocation
{
public float X { get; set; }
public float Y { get; set; }
public string Text { get; set; }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。