在PDF文件中,可繪製不同字型樣式、不同語言的文字,可通過使用Standard字型、TrueType字型、CJK字型或者自定義(私有)等字型型別。下面通過C#程式程式碼來展示如何實現使用以上型別的字型來繪製文字。
引入dll
本次程式中引入的是Spire.Pdf.dll,引入方法如下:
【方法1】通過NuGet安裝。
- 可以在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“管理NuGet包”,然後搜尋“Free Spire.PDF”,點選“安裝”。
- 也可以將以下內容複製到PM控制檯安裝:
Install-Package FreeSpire.PDF -Version 7.8.9
【方法2】手動安裝。
可通過手動下載Free Spire.PDF for .NET包,然後解壓,找到BIN資料夾下的Spire.Pdf.dll。在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“新增引用”,將本地路徑BIN資料夾下的dll檔案新增引用至程式。
應用字型
C#
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace ApplyFonts { class Program { static void Main(string[] args) { //建立PdfDocument物件 PdfDocument pdf = new PdfDocument(); //新增一頁 PdfPageBase page = pdf.Pages.Add(); //初始化y座標 float y = 30; //使用standard字型繪製文字 PdfFont standardFont = new PdfFont(PdfFontFamily.Helvetica, 14f); page.Canvas.DrawString("Standard Font - Helvetica", standardFont, PdfBrushes.Black, 0, y); standardFont = new PdfFont(PdfFontFamily.TimesRoman, 14f); page.Canvas.DrawString("Standard Font - Times_Roman", standardFont, PdfBrushes.Black, 0, (y = y + 16)); standardFont = new PdfFont(PdfFontFamily.Courier, 14f); page.Canvas.DrawString("Standard Font - Courier", standardFont, PdfBrushes.Black, 0, (y = y + 16)); //使用true type字型繪製文字 PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Arial", 12f), true); page.Canvas.DrawString("TrueType Font - Arial", trueTypeFont, PdfBrushes.Blue, 0, (y = y + 30f)); /*//使用私有字型繪製文字 string fontFileName = "C:\\Users\\Administrator\\Desktop\\fontfile.ttf"; trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f); page.Canvas.DrawString("Private Font: 私有字型", trueTypeFont, PdfBrushes.DarkGreen, 0, (y = y + 30f)); */ //使用cjk字型繪製文字 PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f); page.Canvas.DrawString("你 好", cjkFont, PdfBrushes.DeepPink, 0, (y = y + 30f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14f); page.Canvas.DrawString("こんにちは", cjkFont, PdfBrushes.OrangeRed, 0, (y = y + 16f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14f); page.Canvas.DrawString("안녕하세요", cjkFont, PdfBrushes.Purple, 0, (y = y + 16f)); //儲存文件 pdf.SaveToFile("ApplyFonts.pdf",FileFormat.PDF); System.Diagnostics.Process.Start("ApplyFonts.pdf"); } } }
VB.NET
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace ApplyFonts Class Program Private Shared Sub Main(args As String()) '建立PdfDocument物件 Dim pdf As New PdfDocument() '新增一頁 Dim page As PdfPageBase = pdf.Pages.Add() '初始化y座標 Dim y As Single = 30 '使用standard字型繪製文字 Dim standardFont As New PdfFont(PdfFontFamily.Helvetica, 14F) page.Canvas.DrawString("Standard Font - Helvetica", standardFont, PdfBrushes.Black, 0, y) standardFont = New PdfFont(PdfFontFamily.TimesRoman, 14F) page.Canvas.DrawString("Standard Font - Times_Roman", standardFont, PdfBrushes.Black, 0, (InlineAssignHelper(y, y + 16))) standardFont = New PdfFont(PdfFontFamily.Courier, 14F) page.Canvas.DrawString("Standard Font - Courier", standardFont, PdfBrushes.Black, 0, (InlineAssignHelper(y, y + 16))) '使用true type字型繪製文字 Dim trueTypeFont As New PdfTrueTypeFont(New Font("Arial", 12F), True) page.Canvas.DrawString("TrueType Font - Arial", trueTypeFont, PdfBrushes.Blue, 0, (InlineAssignHelper(y, y + 30F))) '//使用私有字型繪製文字 ' string fontFileName = "C:\\Users\\Administrator\\Desktop\\fontfile.ttf"; ' trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f); ' page.Canvas.DrawString("Private Font: 私有字型", trueTypeFont, PdfBrushes.DarkGreen, 0, (y = y + 30f)); ' '使用cjk字型繪製文字 Dim cjkFont As New PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14F) page.Canvas.DrawString("你 好", cjkFont, PdfBrushes.DeepPink, 0, (InlineAssignHelper(y, y + 30F))) cjkFont = New PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14F) page.Canvas.DrawString("こんにちは", cjkFont, PdfBrushes.OrangeRed, 0, (InlineAssignHelper(y, y + 16F))) cjkFont = New PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14F) page.Canvas.DrawString("안녕하세요", cjkFont, PdfBrushes.Purple, 0, (InlineAssignHelper(y, y + 16F))) '儲存文件 pdf.SaveToFile("ApplyFonts.pdf", FileFormat.PDF) System.Diagnostics.Process.Start("ApplyFonts.pdf") End Sub Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T target = value Return value End Function End Class End Namespace
字型繪製效果:
—End—