Draw Text with Different Styles in PDF – C#

Usually, in order to catch readers’ eyes, there will be many font styles are used in electronic books or magazines. These font styles not only include font type, but text direction, alignment and some other formats. In this post, I will show the method about how to draw text with styles in a PDF document by using C#.

In my example, I will draw two text styles. One is to draw text with alignment, including right, left and center. The other is to transform text and make text to be formed with a circle. The following steps will show details.

Because I use Spire.PDF in this method, so I add its dll file as reference.

Steps:

  1. Create PDF document and add page.
  2. Draw text styles on this page. In this step, I declare three methods, AlignText, RotateText and BackgroundImage. Firstly, define AlignText. a. Set font type and color for text I will draw. b. Set text alignment, including left, right and center alignment. Secondly, draw text transformation. a. Set font type and color for text. b. Define text alignment and then set text location. c. Set transform angle and draw text with format. Thirdly, add background image.
  3. Save and launch file.

Main Coding:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

 

namespace DrawText

{

    class Text

    {

        static void Main(string[] args)

        {

            //Create PDF

            PdfDocument doc = new PdfDocument();

 

            //Add Page

            PdfPageBase page = doc.Pages.Add();

 

            //Draw Text and Add Background Image

            AlignText(page);

            RotateText(page);

            BackgroundImage(page);

 

            //Save

            doc.SaveToFile(“DrawText.pdf”);

            doc.Close();

 

            //Launch

            System.Diagnostics.Process.Start(“DrawText.pdf”);

        }

 

        private static void RotateText(PdfPageBase page)

        {

            PdfGraphicsState state = page.Canvas.Save();

 

            //Draw Text – Transform       

            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f);

            PdfSolidBrush brush = new PdfSolidBrush(Color.DarkCyan);

 

            PdfStringFormat centerAlignment

                = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

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

            float y = 250;

 

            page.Canvas.TranslateTransform(x, y);

            for (int i = 0; i < 12; i++)

            {

                page.Canvas.RotateTransform(30);

                page.Canvas.DrawString(“Rolling in The Deep – Adele”, font, brush, 20, 0, centerAlignment);

            }

 

            //Restore

            page.Canvas.Restore(state);

        }

 

        private static void AlignText(PdfPageBase page)

        {

            //Draw Text – Alignment

            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f);

            PdfSolidBrush brush = new PdfSolidBrush(Color.LimeGreen);

 

            PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            page.Canvas.DrawString(“Adele”,font, brush, 0, 40, leftAlignment);

 

            PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            page.Canvas.DrawString(“Rolling in The Deep”,font, brush, page.Canvas.ClientSize.Width / 2, 40, centerAlignment);

 

            PdfStringFormat rightAlignment

                = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);

            page.Canvas.DrawString(“Adele”,font, brush, page.Canvas.ClientSize.Width, 40, rightAlignment);

        }

 

        private static void BackgroundImage(PdfPageBase page)

        {

            Image img = Image.FromFile(@”D:\work\My Documents\bi.png”);

            page.BackgroundImage = img;

        }

    }

}

Result Shown by Following:

_______________________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

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