從PDF中提取文本時,我也需要提取字體大小。首先,我已經提取這樣的:使用itextsharp從PDF獲取字體大小
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(
curBaseline[Vector.I1],
curBaseline[Vector.I2],
topRight[Vector.I1],
topRight[Vector.I2]);
在此,我無法得到確切的字體大小。之後我嘗試使用renderinfo.gs.fontsize;
。在這renderinfo.gs.fontsize
我會得到幾個文字字體大小確切的一個,但很少我不會得到確切的字體大小。在哪裏我會得到字體大小有「1.0」。任何人都可以告訴我,我使用的方法是正確的。如果沒有任何其他方法使用iTextSharp提取字體大小。我正在使用iTextSharp 5.4版本。先謝謝你。
using System;
using System.Collections;
// code java to C# conversion
public void renderText(TextRenderInfo renderInfo)
{
LineSegment curBaseline = renderInfo.Baseline;
LineSegment curAscentline = renderInfo.AscentLine;
Rectangle rect = new Rectangle(curBaseline.StartPoint.get(ArrayList.I1), curBaseline.StartPoint.get(ArrayList.I2), curAscentline.EndPoint.get(ArrayList.I1), curAscentline.EndPoint.get(ArrayList.I2));
try
{
Console.Write(" [{0,6:F2}, {1,6:F2}, {2,6:F2}] \"{3}\" ({4} at {5,6:F2})\n", rect.Width, rect.Height, getEffectiveFontSize(renderInfo), renderInfo.Text, renderInfo.Font.FullFontName[0], getFontSize(renderInfo));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.Write(e.StackTrace);
}
}
float getEffectiveFontSize(TextRenderInfo renderInfo) throws System.ArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException
{
Method convertHeight = typeof(TextRenderInfo).getDeclaredMethod("convertHeightFromTextSpaceToUserSpace", float.TYPE);
convertHeight.Accessible = true;
return (float?)convertHeight.invoke(renderInfo, getFontSize(renderInfo));
}
float getFontSize(TextRenderInfo renderInfo) throws SecurityException, NoSuchFieldException, System.ArgumentException, IllegalAccessException
{
Field gsField = typeof(TextRenderInfo).getDeclaredField("gs");
gsField.Accessible = true;
GraphicsState gs = (GraphicsState) gsField.get(renderInfo);
return gs.FontSize;
}
你也必須採取當前變換矩陣進去,看到編輯我的回答[這裏]這將是有益的(http://stackoverflow.com/questions/15739221/how-to-implement-smallcaps-in-itextsharp/15752789#15752789)。 – mkl 2013-05-02 05:48:59
@mkl它是在Java中,如果我沒有錯。我試圖使用java轉換代碼到c#軟件,但它不可能。任何人都可以幫助使用csharp請 – Pragya 2013-05-02 05:56:11
如果c#中的反省和反思與Java中的反思和反思太不同,只需複製iTextSharp解析器類並公開所需的成員和方法即可。 – mkl 2013-05-02 06:55:48