如何在IronPython中重写C#方法

0 投票
2 回答
1143 浏览
提问于 2025-04-17 12:05

我想在ironPython中实现iTextSharp的FontProvider,参考了这篇帖子 iText + HTMLWorker - 如何更改默认字体?

我的代码总是报错:TypeError: GetFont()需要8个参数,但我只给了7个。

$ ipy itexthtml.py
[INFO] __init__ .. AngsanaUPC
Traceback (most recent call last):
  File "itexthtml.py", line 74, in <module>
  File "itexthtml.py", line 65, in main
TypeError: GetFont() takes exactly 8 arguments (7 given)

我不知道哪里出错了,有人能帮忙吗?

这是我的python代码。

import clr

clr.AddReference("itextsharp")

from iTextSharp.text import Document, PageSize, FontFactoryImp
from iTextSharp.text.pdf import PdfWriter
from iTextSharp.text.html.simpleparser import HTMLWorker,StyleSheet
from System.Collections.Generic import List, Dictionary
from System.IO import FileStream, MemoryStream, FileMode, SeekOrigin, StreamReader
from System.Text import UTF8Encoding
from System import String, Object

class DefaultFontProvider (FontFactoryImp) :

    def __init__(self, string_def) :

        print "[INFO] __init__ ..", string_def
        self.default = string_def

    def GetFont(self, fontname,  encoding,  embedded,  size,  style,  color,  cached) :
        print "[INFO] getFont ..", fontname

        if (fontname == None) :
            fontname = self.default

        print "[INFO] return .."
        return super(DefaultFontProvider, self).GetFont(fontname, encoding, embedded, size, style, color, cached)


def main() :

    output_pdf_file = "test.pdf"
    f = open("test.html", "r")
    html_data = f.readlines()
    html_data = "".join(html_data)

    document =  Document(PageSize.A4);
    PdfWriter.GetInstance(document, FileStream(output_pdf_file, FileMode.Create))
    document.Open()

    providers = Dictionary[String,Object]()
    providers.Add(HTMLWorker.FONT_PROVIDER, DefaultFontProvider("AngsanaUPC"));

    h = HTMLWorker(document)

    h.SetInterfaceProps(providers)

    file_list = List[String]()

    styles = StyleSheet();
    #styles.LoadTagStyle("body", "font-family", "AngsanaUPC");
    #styles.LoadTagStyle("body", "font-face", "AngsanaUPC");

    for idx in range(1) :

        document.NewPage()

        mem = MemoryStream()
        b = UTF8Encoding.UTF8.GetBytes(html_data)
        mem.Write(b, 0, b.Length)
        mem.Seek(0, SeekOrigin.Begin)

        sr = StreamReader(mem, UTF8Encoding.UTF8, styles)
        h.Parse(sr)

        sr.Dispose()
        mem.Dispose()

    document.Close()


if __name__ == "__main__" :
    main()

还有iTextSharp的FontFactoryImp类的定义。

public class FontFactoryImp : IFontProvider
{
    // Fields
    private bool defaultEmbedding;
    private string defaultEncoding;
    private Dictionary<string, List<string>> fontFamilies;
    private static readonly ILogger LOGGER;
    private Dictionary<string, string> trueTypeFonts;
    private static string[] TTFamilyOrder;

    // Methods
    static FontFactoryImp();
    public FontFactoryImp();
    public virtual Font GetFont(string fontname);
    public virtual Font GetFont(string fontname, float size);
    public virtual Font GetFont(string fontname, string encoding);
    public virtual Font GetFont(string fontname, float size, BaseColor color);
    public virtual Font GetFont(string fontname, float size, int style);
    public virtual Font GetFont(string fontname, string encoding, bool embedded);
    public virtual Font GetFont(string fontname, string encoding, float size);
    public virtual Font GetFont(string fontname, float size, int style, BaseColor color);
    public virtual Font GetFont(string fontname, string encoding, bool embedded, float size);
    public virtual Font GetFont(string fontname, string encoding, float size, int style);
    public Font GetFont(string fontname, string encoding, bool embedded, float size, int style);
    public virtual Font GetFont(string fontname, string encoding, float size, int style, BaseColor color);
    public virtual Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color);
    public virtual Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached);
    public virtual bool IsRegistered(string fontname);
    public virtual void Register(string path);
    public virtual void Register(string path, string alias);
    public virtual int RegisterDirectories();
    public virtual int RegisterDirectory(string dir);
    public int RegisterDirectory(string dir, bool scanSubdirectories);
    public void RegisterFamily(string familyName, string fullName, string path);

    // Properties
    public virtual bool DefaultEmbedding { get; set; }
    public virtual string DefaultEncoding { get; set; }
    public virtual ICollection<string> RegisteredFamilies { get; }
    public virtual ICollection<string> RegisteredFonts { get; }
}

2 个回答

0

不要使用:

 return super(DefaultFontProvider, self).GetFont(fontname, encoding, embedded, size, style, color, cached)

而是使用:

return DefaultFontProvider.GetFont(self, fontname, encoding, embedded, size, style, color, cached)
1

我不是IronPython(甚至连Python)方面的专家,但几年前我在用IronPython作为C#库代码的轻量级测试工具时,遇到过类似的问题。

我记得当时的问题是,我以静态的方式调用了一个实例方法,所以“缺失”的参数其实是这个类的一个实例。

我在我的博客上写过这件事,可以在这里找到,这让我想起了当时的情况,但显然我觉得没有必要详细说明具体的解决办法。

我知道这并不是你问题的完整解决方案,但或许能给你一些提示,帮助你找到正确的解决办法。看起来你调用的super()可能是以静态的方式进行的(也就是说,“调用这个实例的父类的foo方法”,但实际上你并没有这个父类的实例来调用GetFont())。

撰写回答