user3413042 user3413042 - 1 year ago 70
Vb.net Question

How can i display Vietnamese characters in a .pdf document using itextsharp?

I have read a number of threads that discuss this and none have come up with a solution for this problem. Most of them revolve around using a suitable font. I have tried every single one of them with no success. I know this string is UTF-8 and Vietnamese because if i paste it into Notepad++ as an ASCII string and then change encoding to UTF-8 it works.

The input string looks like this;

"Có sẵn dịch vụ thông dịch miễn phí khi bạn yêu cầu."

The output string should look like this;

"Có sẵn dịch vụ thông dịch miễn phí khi bạn yêu cầu."

My code just produces the first string.
Any help is greatly appreciated. I am tearing my hair out here
Here is my code;

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Module TextToPdf
Dim pdfWrite As PdfWriter
Dim pdfDoc As Document
Dim pdfFont As Font
Sub Main()

pdfDoc = New Document(PageSize.LETTER)

pdfFont = New Font(BaseFont.CreateFont(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "vuArial.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 15)

pdfWrite = PdfWriter.GetInstance(pdfDoc, New FileStream("../tmp/vietnamese.pdf", FileMode.Create))
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Có sẵn dịch vụ thông dịch miễn phí khi bạn yêu cầu.", pdfFont))
pdfDoc.Close()

End Sub

End Module

Answer Source

You could probably just save your source code as UTF-8 and paste in the "output" (UTF-8) string, but assuming the source code is ANSI encoding, maybe you can convert it to UTF-8 manually:

Dim ansi = Encoding.Default.GetBytes("Có sẵn dịch vụ thông dịch miễn phí khi bạn yêu cầu.")
Dim utf8 = Encoding.UTF8.GetString(ansi)
pdfDoc.Add(New Paragraph(utf8, pdfFont))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download