【发布时间】:2021-06-25 16:00:03
【问题描述】:
我正在尝试使用 iText 7.1.14 根据字体、字体大小和字体样式从各种 pdf 文件中提取文本。
public class FontSizeSimpleTextExtractionStrategy : SimpleTextExtractionStrategy
{
FieldInfo _textField = typeof(TextRenderInfo).GetField("text", BindingFlags.NonPublic | BindingFlags.Instance);
public override void EventOccurred(IEventData data, EventType type)
{
if (type.Equals(EventType.RENDER_TEXT))
{
TextRenderInfo renderInfo = (TextRenderInfo)data;
string fontName = renderInfo.GetFont()?.GetFontProgram()?.GetFontNames()?.GetFontName();
iText.Kernel.Colors.Color color = renderInfo.GetFillColor();
float size = renderInfo.GetFontSize();
if (fontName != null)
{
_textField.SetValue(renderInfo, "#Data|" + fontName + "|" + size.ToString() + "|" + ColorToString(color) + "|Data#" + renderInfo.GetText());
}
}
base.EventOccurred(data, type);
}
}
在某些文件中,“大小”的值始终为“1”,尽管 Adobe Acrobat 显示正确的字体大小,即 this example file 中的 25 和 11。
是否有机会使用 iText 获得正确的大小?
【问题讨论】: