【问题标题】:How to override C# method under ironPython如何在ironPython下覆盖C#方法
【发布时间】:2012-02-01 05:41:23
【问题描述】:

我想通过iText + HTMLWorker - How to change default font?这个帖子在 IronPython 中实现 iTextSharp FontProvider。

我的代码总是抛出: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; }
}

【问题讨论】:

标签: python overriding itextsharp ironpython


【解决方案1】:

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

我记得,我的问题是我以静态方式调用实例方法,因此“缺失”参数是类的实例。

我在博客上写了here,这足以触发我的记忆,但我显然认为它不足以展示实际的修复。

我知道,这不是您问题的完整解决方案,但也许它会为您提供正确解决方案的指针。在我看来, super() 调用可能是以静态方式调用的(即“调用此实例的父类的 foo 方法”,您实际上没有父类的实例来调用 GetFont() )。

【讨论】:

    【解决方案2】:

    代替:

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

    使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      相关资源
      最近更新 更多