3

I'm trying to use a Unicode symbol in my PDF file with iTextSharp.

Dim base As BaseFont = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\WINGDING.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)

Dim wd As Font = New Font(base, 12, Font.NORMAL, BaseColor.BLACK)
phrase = New Phrase("q", wd)

It's the Q.Key in Wingding. But in the PDF file it's not working. It just prints nothing where the Char should be.

Where is the error?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Doc Snuggles
  • 241
  • 1
  • 3
  • 15
  • I suggest you inspect `base`... `getFontFamilyName()` and `getPostscriptFontName()` in particular. `getEncoding()` wouldn't hurt either. Make sure you're getting the font you're asking for. – Mark Storer Feb 18 '11 at 16:52
  • Also, what version of iTextSharp are you using. Chris's basically identical code worked fine... makes me suspect a config issue. Version, font path, something like that. – Mark Storer Feb 18 '11 at 16:53

2 Answers2

3

I just did the following and it worked exactly as it should. The Wingdings font appears in between the two words as a square box with a bottom-right drop shadow. The only thing is that I can't actually get the Wingdings font to actually embed itself and I believe that its an iTextSharp implicit rule because its assumed to be everywhere. I tried with WINGDNG2.TTF and that embedded correctly.

Are you maybe not adding the Phrase correctly? Or are you opening this on a machine without Wingdings maybe?

    ''//Create a new document
    Dim Doc As New iTextSharp.text.Document(PageSize.LETTER, 20, 20, 20, 20)
    ''//Store the document on the desktop
    Dim writer = PdfWriter.GetInstance(Doc, New FileStream(Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Output.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))

    ''//Open the PDF for writing
    Doc.Open()

    ''//Insert a page
    Doc.NewPage()

    ''//Add a regular text block using the default font
    Dim Phrase = New Phrase("Hello")
    Doc.Add(Phrase)


    ''//Create our base font
    Dim base As BaseFont = BaseFont.CreateFont("C:\Windows\Fonts\wingding.ttf", BaseFont.CP1252, BaseFont.EMBEDDED)
    ''//Create our usable font
    Dim wd As Font = New Font(base, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

    ''//Add a text block using Wingdings
    Phrase = New Phrase("q", wd)
    Doc.Add(Phrase)

    ''//Add a trailing text block using the default font again
    Phrase = New Phrase("Bye")
    Doc.Add(Phrase)

    ''//Close the PDF
    Doc.Close()
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • "ZapfDingbats" and "Symbol" are the two symbolic fonts in PDF's "Base 14 Fonts". Wingdings should be fine. – Mark Storer Feb 18 '11 at 16:46
  • @Mark Storer, this isn't really related to the OP anymore but can you get Wingdings to actually embed? Or maybe I'm having an Acrobat issue instead. When I run the above code and I try to select the box I get a warning saying it can't find the original font. When I change it to WINGDNG2.TTF it doesn't warn me. – Chris Haas Feb 18 '11 at 17:11
  • It#s getting embarrasing for me... I´n the testing phase I must have removed the code to add the phrase... Everything works perfectly... I#m sorry that I annoyed you with this stupid "bug"... My fault! Thanks everybody for the solution attempts... I'm in the cellar... Whining.. – Doc Snuggles Feb 18 '11 at 18:32
  • @Doc Snuggles, we've all been there before. I spent half and hour on a very simple math equation that wasn't working only to find out I wasn't actually saving the file before testing. – Chris Haas Feb 18 '11 at 18:47
2

The code appears to be a Visual Basic port of a C# project, since the string that holds the path to the font file has escaped backslashes in it -meaning "C:\WINDOWS\FONTS\WINGDING.TTF".

This is an error in VB, which is probably trying to find a file located at "C:\\WINDOWS\\FONTS\\WINGDING.TTF".

Chris' code, which apparently works, also points in that direction; since the escaped backslashes are one of its few differences.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Diego
  • 21
  • 1