Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

.net - Unicode symbols in iTextSharp

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:WindowsFontswingding.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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...