How to Create PDF List with C#
If one document includes many parts, the creator may create a category or list to show the title and page number of each part.
Generally speaking, list is created with an order which is formed according to the contents in documents. The list shows a brief structure of the whole document. Therefore, it can be taken as simplified outline. There are two functions of list. One is to retrieve and the other is to guide how to read. Also, because of list, readers can learn more clearly about the documents.
List is often used in PDF documents for creating an e-book. It is a little complicated to add PDF list. The most frequently used method is to use bookmark function provided by Adobe Acrobat. There are lots of materials which introduce how to create list with Acrobat. Follow the guide and then you can get the list.
Then, I also want to introduce one method to create PDF list by using C#. And I hope this method can be useful for developers.
The list I create with this method is a simple list. It doesn’t include any subtitles. Also, the method is based on a development component: Spire.PDF.
Code:
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 a pdf document.
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;
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
float y = 10;
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), 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;
RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
PdfLinearGradientBrush brush
= new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical);
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
String formatted
= "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";
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 = 8;
sortedList.TextIndent = 5;
sortedList.Brush = brush;
sortedList.Draw(page, 0, y);
//Save pdf file.
doc.SaveToFile("SimpleList.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("SimpleList.pdf");
}
}
}
|