【问题标题】:How to Convert System.Drawing.Font to iTextSharp.text.pdf.BaseFont?如何将 System.Drawing.Font 转换为 iTextSharp.text.pdf.BaseFont?
【发布时间】:2020-12-15 01:56:45
【问题描述】:

我想在一些 pdf 上添加水印。我使用 iTextSharp 来完成这个功能。但是如何将字体转换为 BaseFont?我搜索了很多,没有任何帮助。

font = new System.Drawing.Font("cambria", 26f, FontStyle.Bold);
BaseFont basefont = BaseFont.CreateFont(font);

【问题讨论】:

    标签: c# winforms itext


    【解决方案1】:

    现在我找到了转换的方法...

    private static BaseFont ConvertFont2BaseFont(System.Drawing.Font _font)
    {
        var fontFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
                
        if(!FontFactory.IsRegistered(_font.Name)) FontFactory.RegisterDirectory(fontFolderPath);
        BaseFont baseFont = FontFactory.GetFont(_font.Name, _font.Size, ConvertFontStyle(_font.Style)).BaseFont;
        return baseFont;
    }
    
    private static int ConvertFontStyle(FontStyle _fontStyle)
    {
        int style = -1;
                
        if((_fontStyle & FontStyle.Regular) != 0)
        {
            style |= iTextSharp.text.Font.NORMAL;
        }
        if((_fontStyle & FontStyle.Bold) != 0)
        {
            style |= iTextSharp.text.Font.BOLD;
        }
        if((_fontStyle & FontStyle.Italic) != 0)
        {
            style |= iTextSharp.text.Font.ITALIC;
        }                    
        if((_fontStyle & FontStyle.Underline) != 0)
        {
            style |= iTextSharp.text.Font.UNDERLINE;
        }
        if((_fontStyle & FontStyle.Strikeout) != 0)
        {
            style |= iTextSharp.text.Font.STRIKETHRU;
        }
        return style;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-05
      • 2010-11-20
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2010-09-08
      • 2013-09-21
      相关资源
      最近更新 更多