LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

Spire.PDF如何添加简体和繁体汉字到PDF文件(C #,VB.NET)

admin
2018年1月23日 15:30 本文热度 6010

spire.pdf为.NET提供了几个字体类:PdfFont、PdfTrueTypeFont、PdfCjkStandardFont,使开发人员能够添加各种字体的PDF文件。在这篇文章中我们将学习如何利用C #和VB.NET生成字库、支持汉字的字体添加简体和繁体汉字的PDF文件。

在开始之前,请确保您已经在系统中安装相应的字体。如果没有,你可以从下面的链接下载:

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

详细步骤和代码片段:

步骤1:创建一个新的PDF文档,并为其添加新页。

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


步骤2使用pdftruetypefont类和pdfcjkstandardfont类来生成字体,支持简体和繁体汉字。

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


步骤3使用PdfCanvas.DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y) 画出汉字的PDF文件的位置指定方法和生成的字体。

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


步骤4:保存PDF文件到磁盘。

pdf.SaveToFile("result.pdf");


运行该项目,我们将得到如下结果的PDF文件:

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


文章来源:这里


该文章在 2018/1/23 15:33:28 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved