我将我的代码从 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 获得上述内容。有什么建议吗?
谢谢, 秀
答案 0 :(得分:2)
我找不到像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);
}
}