ExcelTechnology

How to Set PDF Font Using C#

In order to have a wonderful layout, people set formatting when they post articles to their blogs or submit their electronic document to article websites.

Font is one part of format settings, including font style and color. Generally speaking, the most frequently used font styles are Times New Roman and Arial. Also, there are many other font styles, including some special font, for example, Chinese font, Japanese font and so on.

How should we do to set font in PDF? Actually, people can set formatting well in Word and then convert it to PDF. However, it is possible that the format will lost during conversion. Therefore, some professional editors are developed for editing PDF documents. Of course, the most famous is Adobe Acrobat, which specializes in operating PDF documents.

In this post, I want to introduce one method to set PDF font by using C#. The following code can show you how to set various font styles, including special styles. And I hope that this method can be helpful for programmers who want to edit PDF font programmatically.

Before using the code, you need to install one component: Spire.PDF firstly. And add the DLL file as reference to your project.

Using the Code:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

namespace Font

{

class Program

{

static void Main(string[] args)

{

//Create a pdf document.

PdfDocument doc = new PdfDocument();

// Create one page

PdfPageBase page = doc.Pages.Add();

//Draw the text

float l = page.Canvas.ClientSize.Width / 2;

PointF center = new PointF(l, l);

float r = (float)Math.Sqrt(2 * l * l);

PdfRadialGradientBrush brush

= new PdfRadialGradientBrush(center, 0f, center, r, Color.Blue, Color.Red);

PdfFontFamily[] fontFamilies

= (PdfFontFamily[])Enum.GetValues(typeof(PdfFontFamily));

float y = 10;

for (int i = 0; i < fontFamilies.Length; i++)

{

String text = String.Format("Font Family: {0}", fontFamilies[i]);

float x1 = 0;

y = 10 + i * 16;

PdfFont font1 = new PdfFont(PdfFontFamily.Courier, 14f);

PdfFont font2 = new PdfFont(fontFamilies[i], 14f);

float x2 = x1 + 10 + font1.MeasureString(text).Width;

page.Canvas.DrawString(text, font1, brush, x1, y);

page.Canvas.DrawString(text, font2, brush, x2, y);

}

//true type font - embedded.

System.Drawing.Font font = new System.Drawing.Font("Arial", 14f, FontStyle.Bold);

PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);

page.Canvas.DrawString("Font Family: Arial - Embedded", trueTypeFont, brush, 0, (y = y + 16f));

//right to left

String arabicText

= "u0627u0644u0630u0647u0627u0628u0021u0020"

+ "u0628u062Fu0648u0631u0647u0020u062Du0648u0644u0647u0627u0021u0020"

+ "u0627u0644u0630u0647u0627u0628u0021u0020"

+ "u0627u0644u0630u0647u0627u0628u0021u0020"

+ "u0627u0644u0630u0647u0627u0628u0021";

trueTypeFont = new PdfTrueTypeFont(font, true);

RectangleF rctg = new RectangleF(new PointF(0, (y = y + 16f)), page.Canvas.ClientSize);

PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

format.RightToLeft = true;

page.Canvas.DrawString(arabicText, trueTypeFont, brush, rctg, format);

//true type font - not embedded

font = new System.Drawing.Font("Batang", 14f, FontStyle.Italic);

trueTypeFont = new PdfTrueTypeFont(font);

page.Canvas.DrawString("Font Family: Batang - Not Embedded", trueTypeFont, brush, 0, (y = y + 16f));

//font file

String fontFileName = "Hawaii_Killer.ttf";

trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f);

page.Canvas.DrawString("Hawaii Killer Font", trueTypeFont, brush, 0, (y = y + 16f));

page.Canvas.DrawString("Hawaii Killer Font, from http://www.1001freefonts.com",

new PdfFont(PdfFontFamily.Helvetica, 8f), brush, 10, (y = y + 20f));

//cjk font

PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f);

page.Canvas.DrawString("How to say 'Font' in Chinese? u5B57u4F53",

cjkFont, brush, 0, (y = y + 16f));

cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14f);

page.Canvas.DrawString("How to say 'Font' in Japanese? u30D5u30A9u30F3u30C8",

cjkFont, brush, 0, (y = y + 16f));

cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14f);

page.Canvas.DrawString("How to say 'Font' in Korean? uAE00uAF34",

cjkFont, brush, 0, (y = y + 16f));

//Save pdf file.

doc.SaveToFile("Font.pdf");

doc.Close();

//Launching the Pdf file.

System.Diagnostics.Process.Start("Font.pdf");

}

}

}

08.07.2011. u 07:52 | 1 Komentara | Print | # | ^

<< Arhiva >>

Creative Commons License
Ovaj blog je ustupljen pod Creative Commons licencom Imenovanje-Dijeli pod istim uvjetima.

< srpanj, 2011 >
P U S Č P S N
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Kolovoz 2011 (2)
Srpanj 2011 (2)
Lipanj 2011 (2)
Svibanj 2011 (2)
Travanj 2011 (3)
Ožujak 2011 (2)
Veljača 2011 (2)
Siječanj 2011 (5)
Prosinac 2010 (5)

Dnevnik.hr
Gol.hr
Zadovoljna.hr
Novaplus.hr
NovaTV.hr
DomaTV.hr
Mojamini.tv

Opis bloga

something about Excel Technology in VB.Net and C#

Linkovi

Dnevnik.hr
Video news portal Nove TV

Blog.hr
Blog servis

Igre.hr
Najbolje igre i igrice

Forum.hr
Monitor.hr