![](/img/trans.png)
[英]Extract font height and rotation from PDF files with iText/iTextSharp
[英]Porting from itextsharp to to itext 7 - how to get the font weight of text from a PDF document?
我正在将代码从itextsharp移植到itext7,并遇到字体加粗的困难。 这是来自itextsharp的代码片段(不是最好的代码!),但是效果很好,并在可用的地方给了我字体粗细。
public class MyLocationTextExtractionStrategy : LocationTextExtractionStrategy
{
public override void RenderText(TextRenderInfo renderInfo)
{
var oFont = renderInfo.GetFont();
var fieldFontWeight = oFont.GetType().BaseType.GetField(
"fontWeight",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance);
System.Single fontWeight = (System.Single)fieldFontWeight.
GetValue(oFont);
}
}
我无法使用itext 7获得与上述相同的结果 。 有什么建议么?
谢谢秀
我也找不到像iText 5中那样的显式fontWeight
成员,但是没有什么可以阻止我们自己查看FontDescriptor ,例如:
public class MyLocationTextExtractionStrategy : LocationTextExtractionStrategy
{
public override void EventOccurred(IEventData data, EventType type)
{
if (data is TextRenderInfo renderInfo)
{
var oFont = renderInfo.GetFont();
PdfDictionary fontDescriptor = oFont.GetPdfObject().GetAsDictionary(PdfName.FontDescriptor);
PdfNumber number = fontDescriptor?.GetAsNumber(PdfName.FontWeight);
double? weight = number?.GetValue();
[... process weight, it is null if not set in the descriptor ...]
}
base.EventOccurred(data, type);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.