Add simplified and traditional Chinese characters to PDF in C#/VB.NET

2016-07-21 08:02:26 Written by  support iceblue
Rate this item
(0 votes)

Spire.PDF for .NET provides several font classes such as PdfFont, PdfTrueTypeFont and PdfCjkStandardFont which enable developers to add various fonts to PDF files. In this article we will learn how to generate fonts that support Chinese characters and use the fonts to add simplified and traditional Chinese characters to a PDF file in C# and VB.NET.

Before start, please ensure you have installed the corresponding font on system. If not, you can download it from the following link:

http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=5508&fileID=5521

Detail steps and code snippets:

Step 1: Create a new PDF document and add a new page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 2: Use PdfTrueTypeFont class and PdfCjkStandardFont class to generate fonts that support simplified and traditional Chinese characters.

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11f);

Step 3: Use PdfCanvas.DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y) method and the generated fonts to draw Chinese characters to specified location of the PDF file.

page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50);
page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70);

Step 4: Save the PDF file to disk.

pdf.SaveToFile("result.pdf");

Run the project and we'll get the following result PDF file:

How to add simplified and traditional Chinese characters to PDF in C#, VB.NET

Full codes:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Add_Chinese_Characters_to_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();
            
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
            PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11f);
            page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50);
            page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70);
            pdf.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace Add_Chinese_Characters_to_PDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As New PdfDocument()
			Dim page As PdfPageBase = pdf.Pages.Add()
			Dim font As New PdfTrueTypeFont(New Font("Arial Unicode MS", 11F), True)
			Dim font1 As New PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11F)
			page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50)
			page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70)
			pdf.SaveToFile("result.pdf")
			System.Diagnostics.Process.Start("result.pdf")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Add simplified and traditional Chinese characters to PDF
Last modified on Sunday, 26 September 2021 01:55