user984314
user984314

Reputation: 155

embed font into pdf using itextsharp

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?

Upvotes: 3

Views: 10659

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

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

Upvotes: 3

Related Questions