Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use many different fonts in PDF documents with C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how C# applies a variety of different fonts in PDF documents. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Introduction of dll

Spire.Pdf.dll is introduced in this program, and the introduction method is as follows:

[method 1] install through NuGet.

You can open solution Explorer in Visual Studio, right-click "references", "manage NuGet packages", then search for "Free Spire.PDF" and click "install".

You can also copy the following to the PM console installation:

Install-Package FreeSpire.PDF-Version 7.8.9

[method 2] manually install.

You can download the Free Spire.PDF for .NET package manually, and then extract it to find the Spire.Pdf.dll under the BIN folder. Open solution Explorer in Visual Studio, right-click "reference" and "add reference" to add a reference to the dll file under the local path BIN folder.

Apply font C#using Spire.Pdf;using Spire.Pdf.Graphics;using System.Drawing;namespace ApplyFonts {class Program {static void Main (string [] args) {/ / create PdfDocument object PdfDocument pdf = new PdfDocument (); / / add a page PdfPageBase page = pdf.Pages.Add (); / / initialize y coordinates float y = 30 / use standard font to 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)); / / draw the text PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont (new Font ("Arial", 12f), true) in true type font Page.Canvas.DrawString ("TrueTypeFont-Arial", trueTypeFont, PdfBrushes.Blue, 0, (y = y + 30f)); / * / draw text in private font string fontFileName = "C:\\ Users\\ Administrator\\ Desktop\\ fontfile.ttf"; trueTypeFont = new PdfTrueTypeFont (fontFileName, 14f) Page.Canvas.DrawString ("Private Font: private fonts", trueTypeFont, PdfBrushes.DarkGreen, 0, (y = y + 30f)); * / / use cjk font to draw text PdfCjkStandardFont cjkFont = new PdfCjkStandardFont (PdfCjkFontFamily.MonotypeHeiMedium, 14f); page.Canvas.DrawString ("Hello", 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 document pdf.SaveToFile ("ApplyFonts.pdf", FileFormat.PDF); System.Diagnostics.Process.Start ("ApplyFonts.pdf") } VB.NETImports Spire.PdfImports Spire.Pdf.GraphicsImports System.DrawingNamespace ApplyFonts Class Program Private Shared Sub Main (args As String ()) 'create PdfDocument object Dim pdf As New PdfDocument ()' add a page Dim page As PdfPageBase = pdf.Pages.Add () 'initialize y coordinates Dim y As Single = 30 'draw text Dim standardFont As New PdfFont (PdfFontFamily.Helvetica) using standard fonts 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)) 'uses true type font to draw text Dim trueTypeFont As New PdfTrueTypeFont (New Font ("Arial", 12F), True) page.Canvas.DrawString ("TrueTypeFont-Arial", trueTypeFont, PdfBrushes.Blue, 0, (InlineAssignHelper (y)) Y + 30F)'/ / draw the text in private font 'string fontFileName = "C:\\ Users\\ Administrator\\ Desktop\\ fontfile.ttf" 'trueTypeFont = new PdfTrueTypeFont (fontFileName, 14f);' page.Canvas.DrawString ("Private Font: private fonts", trueTypeFont, PdfBrushes.DarkGreen, 0, (y = y + 30f)) '' use cjk font to draw text Dim cjkFont As New PdfCjkStandardFont (PdfCjkFontFamily.MonotypeHeiMedium, 14F) page.Canvas.DrawString ("Hello", cjkFont, PdfBrushes.DeepPink, 0, (InlineAssignHelper (y, y + 30F)) cjkFont = New PdfCjkStandardFont (PdfCjkFontFamily.HanyangSystemsGothicMedium, 14F) page.Canvas.DrawString ("Hello", cjkFont, PdfBrushes.OrangeRed, 0, (InlineAssignHelper (y)) Y + 16F)) cjkFont = New PdfCjkStandardFont (PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14F) page.Canvas.DrawString ("saved documents", cjkFont, PdfBrushes.Purple, 0, (InlineAssignHelper (y, y + 16F) 'Save 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 ClassEnd Namespace drawing effect

These are all the contents of the article "how to use many different fonts in PDF documents". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report