c# - 如何按具有特定大小或字体的标题拆分 PDF
问题描述
我正在尝试根据他的大小或字体按他的标题拆分 PDF。
目前我只能提取 PDF 的字体,但我需要知道大小或字体以查看该文本是否是标题。我不知道如何通过具有特定大小或字体的标题阅读 PDF,我知道它可以提取文本,但就是这样,简单的文本,你怎么知道哪个大小或字体有该文本?
顺便说一句,我已经能够通过他的书签以及书签的“孩子”来拆分 PDF
但我需要更深入地拆分 PDF。这就是为什么我试图让标题按它们拆分 PDF 的原因。
我做了一些研究,但我找不到对这种情况非常有用的东西。
这里有一些问题:
你怎么得到尺寸?
你如何获得字体?
你如何逐行迭代PDF?
如果有特定字体,如何检查文本(行)?
一些代码
HashSet<String> fontNames = new HashSet<string>();
PdfDictionary resources;
for (int p = 1; p <= reader.NumberOfPages; p++)
{
PdfDictionary dic = reader.GetPageN(p);
resources = dic.GetAsDict(PdfName.RESOURCES);
if (resources != null)
{
//get fonts dictionary
PdfDictionary fonts = resources.GetAsDict(PdfName.FONT);
if (fonts != null)
{
PdfDictionary font;
foreach (PdfName key in fonts.Keys)
{
font = fonts.GetAsDict(key);
String name = font.GetAsName(PdfName.BASEFONT).ToString();
fontNames.Add(name);
}
}
}
}
另一种方式
List<object[]> fonts2 = BaseFont.GetDocumentFonts(reader);
其他代码:获取文本
ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
words = currentText.Split('\n');
for (int j = 0, len = words.Length; j < len; j++)
{
line = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(words[j]));
}
解决方案
我用这个:
public void RenderText(iTextSharp.text.pdf.parser.TextRenderInfo renderInfo)
renderInfo参数有我需要的所有东西。
renderInfo.GetFont().PostscriptFontName; // Font Name
renderInfo.GetBaseline().GetStartPoint(); // Coordinates - (56.6929 , 727.8466, 1)
renderInfo.GetAscentLine().GetEndPoint(); // Coordinates - (96.78018, 737.1749, 1)
推荐阅读
- javascript - React Router - 处理来自服务器和渲染组件的 403 响应
- vue.js - 在 render() 中以编程方式访问 Vue 过滤器
- mysql - 运行 MYSQL 生成数据库
- java - 在循环中将 1 添加到 Integer.MAX_VALUE 会导致意外行为(而不是它转到 Integer.MIN_VALUE 的行为)
- c - 如果我 printf 一个没有匹配参数的整数,具体确定输出的是什么?
- exception - React 和 sequelize 错误处理以显示来自前端的消息
- javascript - Openlayers 3 地图,如何在没有焦点的情况下刷新
- java - 将元素分配给空数组
- javascript - 测试使用 mongoDB 和 mocha/chai 的 ejs/nodejs 函数
- oauth-2.0 - OAuth 2.0 隐式授权流程 - 读取日历 API 返回 403