C# – Create List in PDF Document

After opening a book, we can find a category which shows chapter title and page. This category can help readers find which they want to read quickly. Also, it can show readers a brief list about what the book talks about. In this post, I would like to show something about how to create a simple list in PDF documents with C#.

As is known, PDF is one of the most frequently used formats to create e-books. And category is one necessary part in PDF documents. Although sometimes the PDF document just is one short story, list may be used in it. Therefore, we need to know how to create list or category in PDF.

Steps:

  1. Create a PDF document and set margin to make sure that the layout will be wonderful after printing.
  2. Add a new page for drawing list.
  3. Set page title and format it.
  4. Add list text and format text, including font style, size and color.
  5. Format list. There are two different list formats.
  6. In order to make the appearance be more beautiful, add background image.
  7. Save and Launch file.

Note: this method is based on a component: Spire.PDF. Therefore, before staring above steps, add Spire.PDF dll file as reference.

Coding:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using Spire.Pdf.Lists;

namespace SimpleList

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create PDF

            PdfDocument doc = new PdfDocument();

            //Margin

            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            PdfMargins margin = new PdfMargins();

            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Bottom = margin.Top;

            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Right = margin.Left;

            //Add Page

            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            float y = 10;

            //Title

            PdfBrush brush1 = PdfBrushes.Black;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(“Times New Roman”, 14f, FontStyle.Regular), true);

            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.DrawString(“Categories List”, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);

            y = y + font1.MeasureString(“Categories List”, format1).Height;

            y = y + 5;

            //List Text and Text Format

            RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);

            PdfLinearGradientBrush brush

                = new PdfLinearGradientBrush(rctg, Color.DarkCyan, Color.DarkOliveGreen, PdfLinearGradientMode.Vertical);

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

            String formatted

                = “Chap 1-Listening\nChap 2-Speaking\nChap 3-Reading\nChap 4-Writing\nChap 5(Test 1)\nChap 6(Test 2)\nChap 7-Test Answers”;

            //Two Different List Formats

            PdfList list = new PdfList(formatted);

            list.Font = font;

            list.Indent = 8;

            list.TextIndent = 5;

            list.Brush = brush;

            PdfLayoutResult result = list.Draw(page, 0, y);

            y = result.Bounds.Bottom;

            PdfSortedList sortedList = new PdfSortedList(formatted);

            sortedList.Font = font;

            sortedList.Indent = 18;

            sortedList.TextIndent = 5;

            sortedList.Brush = brush;

            sortedList.Draw(page, 0, y);

            //Add Background Image

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

            page.BackgroundImage = img;

            //Save and Launch File

            doc.SaveToFile(“List.pdf”);

            doc.Close();

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

        }

    }

}

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