C# – Draw PDF Font Style

At present, PDF has become one of the most frequently used electronic document format. It is very convenient for delivering and printing. Also, e-books or e-magazines which are created by PDF can give viewers a great visual effect.

Of course, we can create our writings as PDF documents and submit to internet as well. In order to have a wonderful appearance, we need to pay attention to format settings. In this post, I will show my method to set text font style in PDF with C#.

In my example, I will show how to set different font styles, including PDF embedded font, not embedded font and CJK font. Firstly, I add Spire.PDF as reference in my project, which I use in my method. Then, add some other necessary reference, System.Drawing.

The following shows steps about how to set font style.

  1. Create a new PDF document and add a blank page in it.
  2. Set position and format of text which I will draw. Then, get font styles of PdfFontFamily, display all the font styles in it on PDF.
  3. Draw font style which is embedded in PDF.
  4. Draw Arabic text font style and set text from right to left.
  5. Draw font style which is not embedded in PDF.
  6. Draw CJK Font.
  7. Save and launch file.

Coding:

using System;

using  System.Drawing;

usingSpire.Pdf;

using Spire.Pdf.Graphics;

namespace PDFFont

{

    class Font

    {

        static void Main(string[] args)

        {

            //Create PDF Document and Add Page

            PdfDocument doc = new PdfDocument();

            PdfPageBase page = doc.Pages.Add();

            //Draw 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, 13f);

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

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

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

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

            }

            //Font Type – Embedded

            System.Drawing.Font font = new System.Drawing.Font("Arial", 13f, 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

                = "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020"

                + "\u0628\u062F\u0648\u0631\u0647\u0020\u062D\u0648\u0644\u0647\u0627\u0021\u0020"

                + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020"

                + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020"

                + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021";

            trueTypeFont = new PdfTrueTypeFont(font, true);

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

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

            format.RightToLeft = true;

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

            //Font Type – Not Embedded

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

            trueTypeFont = new PdfTrueTypeFont(font);

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

            //CJK Font

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

            page.Canvas.DrawString("How to say 'Font' in Chinese? \u5B57\u4F53",

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

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

            page.Canvas.DrawString("How to say 'Font' in Japanese? \u30D5\u30A9\u30F3\u30C8",

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

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

            page.Canvas.DrawString("How to say 'Font' in Korean? \uAE00\uAF34",

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

            //Save and Launch File

            doc.SaveToFile("Font.pdf");

            doc.Close();

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

        }

    }

}

Result:

___________________________________________________________________________________

Click Here to learn more about Spire.PDF

Click Here to download Spire.PDF

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s