embed font into pdf using itextsharp

2019-05-21 01:02发布

问题:

I am trying to embed a font using itextsharp 5.2.1.0, but am getting errors. the font is KozGoPro-Light.otf which after doing a bit of research is a japenese font. I have tried the following...

        Dim tblx1 As PdfPTable = New PdfPTable(1)
        Dim tblrightcell_margin2 As PdfPCell
        Dim bfR As iTextSharp.text.pdf.BaseFont

        **bfR = iTextSharp.text.pdf.BaseFont.CreateFont("KozGoPro-Light.otf", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED)
        fntKozgoProExtra = New Font(bfR, 18)**

        tblrightcell_margin2 = New PdfPCell(New Phrase("testing....", fntKozgoProExtra))

        tblx1.AddCell(tblrightcell_margin2)

When i try add the font to a phrase, it will say Object reference not set to an instance of an object. If I change iTextSharp.text.pdf.BaseFont.IDENTITY_H to CP1252 or any of those encodings it will just show incorrectly on the pdf. Any clues?

回答1:

When you use IDENTITY_H for an encoding iTextSharp automatically turns on font subsetting. Unfortunately there appears to still be a bug in that code. The solution for now is to just turn off subsetting for that font:

bfR = iTextSharp.text.pdf.BaseFont.CreateFont(FontFile, iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED)
bfR.Subset = False