Thursday, November 6, 2014

PdfSharp Chinese Encoding

Recently I started to use PdfSharp open source project, this is a great tool and its document rendering speed is great with small-medium sized pdf docs.

Anyway, in our program's pdf export function, it has a requirement to be able to cater for different languages such as CJK (Chinese, Japanese, Korean), etc. I got some hints from pdfsharp's forum, here's what's going to solve the issue:

  Private Sub DefineStyles()
        ' Get the predefined style Normal.
        Dim style As Style = Me.document.Styles("Normal")
        ' Because all styles are derived from Normal, the next line changes the
        ' font of the whole document. Or, more exactly, it changes the font of
        ' all styles and paragraphs that do not redefine the font.
        style.Font.Name = "Arial Unicode MS" 'This font will make Chinese characters display
        ' Create a new style called Table based on style Normal
        style = Me.document.Styles.AddStyle("Table", "Normal")
        style.Font.Name = "Arial Unicode MS"  
        style.Font.Size = 10    'Default font
    End Sub