当前位置:网站首页>C # apply many different fonts in PDF documents
C # apply many different fonts in PDF documents
2022-06-11 11:52:00 【Eiceblue】
stay PDF In the document , Can draw different font styles 、 Words in different languages , By using Standard typeface 、TrueType typeface 、CJK Font or custom ( private ) Equal font type . Pass below C# Program code to show how to use the above types of fonts to draw text .
introduce dll
What is introduced in this procedure is Spire.Pdf.dll, The introduction method is as follows :
【 Method 1】 adopt NuGet install .
- Can be in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ management NuGet package ”, And then the search “Free Spire.PDF”, Click on “ install ”.
- You can also copy the following to PM Console installation :
Install-Package FreeSpire.PDF -Version 7.8.9
【 Method 2】 Manual installation .
It can be downloaded manually Free Spire.PDF for .NET package , Then decompress , find BIN Under folder Spire.Pdf.dll. stay Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ Add reference ”, The local path BIN Under folder dll Add a reference to the program .
Apply font
C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace ApplyFonts
{
class Program
{
static void Main(string[] args)
{
// establish PdfDocument object
PdfDocument pdf = new PdfDocument();
// Add a page
PdfPageBase page = pdf.Pages.Add();
// initialization y coordinate
float y = 30;
// Use standard Fonts draw text
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));
// Use true type Fonts draw text
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Arial", 12f), true);
page.Canvas.DrawString("TrueType Font - Arial", trueTypeFont, PdfBrushes.Blue, 0, (y = y + 30f));
/*// Draw text using private Fonts
string fontFileName = "C:\\Users\\Administrator\\Desktop\\fontfile.ttf";
trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f);
page.Canvas.DrawString("Private Font: Private font ", trueTypeFont, PdfBrushes.DarkGreen, 0, (y = y + 30f));
*/
// Use cjk Fonts draw text
PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f);
page.Canvas.DrawString(" you good ", 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));
// Save the document
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())
' establish PdfDocument object
Dim pdf As New PdfDocument()
' Add a page
Dim page As PdfPageBase = pdf.Pages.Add()
' initialization y coordinate
Dim y As Single = 30
' Use standard Fonts draw text
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)))
' Use true type Fonts draw text
Dim trueTypeFont As New PdfTrueTypeFont(New Font("Arial", 12F), True)
page.Canvas.DrawString("TrueType Font - Arial", trueTypeFont, PdfBrushes.Blue, 0, (InlineAssignHelper(y, y + 30F)))
'// Draw text using private Fonts
' string fontFileName = "C:\\Users\\Administrator\\Desktop\\fontfile.ttf";
' trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f);
' page.Canvas.DrawString("Private Font: Private font ", trueTypeFont, PdfBrushes.DarkGreen, 0, (y = y + 30f));
'
' Use cjk Fonts draw text
Dim cjkFont As New PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14F)
page.Canvas.DrawString(" you good ", 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)))
' Save the document
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
Font rendering effect :

—End—
边栏推荐
- golang利用异或^交换两个变量以及加解密
- 导师转我800块,让我仿真一个电路(电源设计)
- [issue 31] 360 background development practice experience - two rounds of technical aspects
- C# 在PDF文档中应用多种不同字体
- WordPress regenerate featured image plugin: regenerate thumbnails
- The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)
- 灵动边栏(Widget)插件:MO Widgets
- 在WordPress媒体库中创建文件夹
- Cap theory sounds very big, but it's actually very simple
- Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
猜你喜欢

Qt中radioButton使用

Elk - x-pack set user password

解决swagger文档接口404的问题

2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!

Intl.numberformat set number format

It will be too late if you don't brush the questions. The most complete bat interview questions

Adapter mode -- can you talk well?

Gerber文件在PCB制造中的作用

web开发选型,web开发毕业谁

C# 将OFD转为PDF
随机推荐
导师转我800块,让我仿真一个电路(电源设计)
CVPR 2022 𞓜 text guided entity level image manipulation manitrans
Fast build elk7.3
异或的妙用(C语言)
快速搭建ELK7.3
Use compiler option ‘--downlevelIteration‘ to allow iterating of iterators 报错解决
Elk - x-pack set user password
17.5 concept, usage, deadlock demonstration and solution of mutex
msf cs openssl流量加密
2019年书单
202年最新热门收益较高的年金险产品是什么?
[issue 30] shopee golang development experience
MSF CS OpenSSL traffic encryption
发布WordPress数据库缓存插件:DB Cache Reloaded 3.1
Gerber文件在PCB制造中的作用
Mongodb usage
WordPress regenerate featured image plugin: regenerate thumbnails
JVM class loading process
The complete manual of the strongest Flink operator is a good choice for the interview~
C# 在PDF文档中应用多种不同字体