当前位置:网站首页>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 .

  1. 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 ”.
  2. 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— 

原网站

版权声明
本文为[Eiceblue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111133421611.html