如何在c#中覆盖pdfBox的PDFTextStripper类的writeString方法? [英] How to override the writeString method of PDFTextStripper class of pdfBox in c#?

查看:223
本文介绍了如何在c#中覆盖pdfBox的PDFTextStripper类的writeString方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中覆盖pdfBox的PDFTextStripper类的writeString方法?



我想提取pdf文件内容的字体大小和样式使用pdfBox。当我搜索时,我发现我应该覆盖PDFTextStripper类的writeString方法。



我是c#的新手,我无法实现覆盖。



我使用的代码如下:



How can I override the writeString method of PDFTextStripper class of pdfBox in c#?

I want to extract the font size and style of the contents of a pdf file using pdfBox. When I searched, I found that I should override the writeString method of the PDFTextStripper class.

I am a newbie in c# and I am not able to implement the overriding.

The code that I used is as follows:

PDFTextStripper stripper = new PDFTextStripper(){

protected override void writeString(String text, List<TextPosition> textPositions)
        {
            String prevBaseFont = "";
            StringBuilder builder = new StringBuilder();

            foreach (TextPosition position in textPositions)
            {
                String baseFont = position.getFont().getBaseFont();
                if (baseFont != null && baseFont != (prevBaseFont))
                {
                    builder.Append('[').Append(baseFont).Append(']');
                    prevBaseFont = baseFont;
                }
                builder.Append(position.getCharacter());
            }

            writeString(builder.ToString());
        }

};





请帮助我。



Please help me.

推荐答案

为了覆盖一个方法,你需要创建一个继承自 PDFTextStripper 的类,前提是它不是密封

您想要覆盖的方法也必须标记为虚拟。

In order to override a method you need to create your own class that inherits from PDFTextStripper provided it is not sealed.
Also the method you want to override has to be marked virtual.
public class MyPDFTextStripper : PDFTextStripper
{
    protected override void writeString(String text, List textPositions)
    {
    }

    // Another option is to hide the original method by using the keyword new
    protected new void writeString(String text, List textPositions)
    {
    }
}



请参阅了解何时使用覆盖和新关键字 [ ^ ]



这篇文章可能会有用使用PdfBox和IKVM在C#中处理PDF文件 [ ^ ]


这篇关于如何在c#中覆盖pdfBox的PDFTextStripper类的writeString方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆