Introduction
This article describes how to use the Gios Pdf.NET open source library, a complete object-oriented .NET library for generating impressive reports with few lines of code.
Background
I started writing this library because I really couldn’t find a good C# library for exporting my reports. When you open a PDF document with Notepad to find out if it’s possible to edit it… well… you change your mind!
...But if you download the PDF format specifications from Adobe’s official site… you can demystify everything!
In this article, it’s not my intention to teach “how to write a clone of my library” but “how to use it” … By the way, if you want to try to write your own library… try it: it’s not impossible and it's a good (and difficult!) exercise!
Using the code
Start instantiating the document with a new PdfDocument
object. Remember to set the document format. In this case we specify width and height in centimeters. Document format is provided by the PdfDocumentFormat
class:
PdfDocument myPdfDocument =
new PdfDocument(PdfDocumentFormat.InCentimeters(21,29.7));
Now we create a table of 100 lines, 6 columns and 4 points of padding:
PdfTable myPdfTable =
myPdfDocument.NewTable(new Font("Verdana",12),200,6,4);
Importing data from the datatables... (also column names for the headers!):
myPdfTable.ImportDataTable(Table);
Set the format for correct date-time representation:
myPdfTable.Columns[2].SetContentFormat("{0:dd/MM/yyyy}");
Now we set our Graphic Design: Colors and Borders...
myPdfTable.HeadersRow.SetColors(Color.White,Color.Navy);
myPdfTable.SetColors(Color.Black,Color.White,Color.Gainsboro);
myPdfTable.SetBorders(Color.Black,1,BorderType.CompleteGrid);
With just one method, we can set the proportional width of the columns. It's a "percentage like" assignment, but the sum can be different from 100.
myPdfTable.SetColumnsWidth(new int[]{5,25,16,20,20,15});
You can also set colors for a range of cells, in this case, a row:
myPdfTable.Rows[7].SetColors(Color.Black,Color.LightGreen);
Now we set some alignment... for the whole table and then, for a column:
myPdfTable.SetContentAlignment(ContentAlignment.MiddleCenter);
myPdfTable.Columns[1].SetContentAlignment(ContentAlignment.MiddleLeft);
This is the most important feature of the library: each page generated by the table can be managed in all the details:
while (!myPdfTable.AllTablePagesCreated)
{
PdfPage newPdfPage=myPdfDocument.NewPage();
PdfTablePage newPdfTablePage =
myPdfTable.CreateTablePage(new PdfArea(myPdfDocument,48,120,500,670));
PdfTextArea pta=new PdfTextArea(new Font("Verdana",26, FontStyle.Bold),
Color.Red, new PdfArea(myPdfDocument,0,20,595,120),
ContentAlignment.MiddleCenter,"Contact List");
newPdfPage.Add(newPdfTablePage);
newPdfPage.Add(pta);
for (int index=newPdfTablePage.FirstRow;index<=newPdfTablePage.LastRow;index++)
if (((DateTime)myPdfTable.Rows[index][2].Content).Year==1968)
{
PdfCircle pc=newPdfTablePage.CellArea(index,2).InnerCircle(Color.Blue,2);
pc.StrokeWidth=3.5;
newPdfPage.Add(pc);
}
newPdfPage.SaveToDocument();
}
At last... remember to save the document!
myPdfDocument.SaveToFile("Example1.pdf");
And this is the result:
Remember, you can also output the PDF to a generic Stream. These are the lines for a Web Response:
Response.ClearHeaders();
Response.AppendHeader("Content-disposition",
string.Format("attachment;filename={0}","Report.pdf"));
Response.ContentType="application/pdf";
myPdfDocument.SaveToStream(Response.OutputStream);
Response.End();
History
- April 18, 2005 - Initial release.