使用iTextSharp嵌入字体

时间:2012-03-22 13:38:20

标签: fonts embed itextsharp

我在使用iTextSharp嵌入字体时遇到问题。 基本上我有一个pdf文档作为模板与几个文本字段。使用了3种字体,需要嵌入,ITCCharterCom-Regular,ITCCharterCom-Italic和ITCCharterCom-BlackItalic。

一切都很好,除了ITCCharterCom-BlackItalic字体。嵌入斜体和常规,但不是黑色斜体。任何关于我做错的想法?​​

以下是简化的代码摘录。

    Private Function saveBusinessCard() As String

    ' init
    Dim template As String = Server.MapPath("~/Resources/BC_Template.pdf")
    Dim pdfOutput As String = String.Format("~/Temp/{0}.pdf", HttpContext.Current.Session.SessionID.ToString())
    Dim output As String = Server.MapPath(pdfOutput)
    Dim reader As New PdfReader(template)
    Dim pdfOutputFile As FileStream = New FileStream(output, FileMode.Create)
    Dim stamper As New PdfStamper(reader, pdfOutputFile)
    Dim form As AcroFields = stamper.AcroFields

    ' font property name
    Dim textFontPropertyName = "textfont"

    ' font installation files
    Dim pathBlackItalic As String = HttpContext.Current.Server.MapPath("~\Resources\Fonts\ITCCharterCom-BlackItalic.ttf")
    Dim pathItalic As String = HttpContext.Current.Server.MapPath("~\Resources\Fonts\ITCCharterCom-Italic.ttf")
    Dim pathRegular As String = HttpContext.Current.Server.MapPath("~\Resources\Fonts\ITCCharterCom-Regular.ttf")

    ' create embedded fonts
    Dim blackItalic As BaseFont = BaseFont.CreateFont(pathBlackItalic,
                                                      BaseFont.IDENTITY_H,
                                                      BaseFont.EMBEDDED)
    Dim italic As BaseFont = BaseFont.CreateFont(pathItalic,
                                                 BaseFont.IDENTITY_H,
                                                 BaseFont.EMBEDDED)
    Dim regular As BaseFont = BaseFont.CreateFont(pathRegular,
                                                  BaseFont.IDENTITY_H,
                                                  BaseFont.EMBEDDED)

    ' assign fonts to text fields
    form.SetFieldProperty("txtContact", textFontPropertyName, italic, Nothing)
    form.SetFieldProperty("txtName", textFontPropertyName, blackItalic, Nothing)
    form.SetFieldProperty("txtTitle", textFontPropertyName, regular, Nothing)

    ' set pdf fields in document
    form.SetField("txtContact", "Contact here")
    form.SetField("txtName", "Name here")
    form.SetField("txtTitle", "Title here")

    ' make input fields plain text
    stamper.FormFlattening = True

    ' clean up
    stamper.Close()
    reader.Close()
    pdfOutputFile.Close()

    ' return output path
    Return pdfOutput

End Function

谢谢, 汤姆

0 个答案:

没有答案