2013-05-02 93 views
3

从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; 
    } 
+0

你也必须采取当前变换矩阵进去,看到编辑我的回答[这里]这将是有益的(http://stackoverflow.com/questions/15739221/how-to-implement-smallcaps-in-itextsharp/15752789#15752789)。 – mkl 2013-05-02 05:48:59

+0

@mkl它是在Java中,如果我没有错。我试图使用java转换代码到c#软件,但它不可能。任何人都可以帮助使用csharp请 – Pragya 2013-05-02 05:56:11

+0

如果c#中的反省和反思与Java中的反思和反思太不同,只需复制iTextSharp解析器类并公开所需的成员和方法即可。 – mkl 2013-05-02 06:55:48

回答

-3

希望

` 
Font arial = FontFactory.GetFont("Arial", 28, Color.GRAY); 
Font verdana = FontFactory.GetFont("Verdana", 16, Font.BOLDITALIC, new Color(125, 88, 15)); 
Font palatino = FontFactory.GetFont("palatino linotype italique",BaseFont.CP1252, BaseFont.EMBEDDED, 
    10, 
    Font.ITALIC, 
    Color.GREEN 
); 
Font smallfont = FontFactory.GetFont("Arial", 7); 
Font x = FontFactory.GetFont("nina fett"); 
x.Size = 10; 
x.SetStyle("Italic"); 
x.SetColor(100, 50, 200);` 

可以用来设置字体大小

+2

如果我没有错,这可以用于创建pdf。我想从PDF中提取文本。我需要确定PDF中的字体大小 – Pragya 2013-05-04 03:54:15