Create PDF with iTextSharp
iTextSharp is an open source PDF creator- download/ documentation
This is a simplified generic version to print a Dataset.
using System.Data;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
/// <summary>
/// Create Pdf using iTextSharp.
/// </summary>
/// <remarks>
/// Uses a dataset, and creates a table per DataTable. The DataTable name and column names are used as page header and table headers.
/// </remarks>
public class CreatePdf
{
/// <summary>
/// Initializes a new instance of the <see cref="CreatePdf"/> class.
/// </summary>
/// <param name="ds">The dataset containing one or more datatables.</param>
/// <param name="name">The filename and pdf title.</param>
public CreatePdf(DataSet ds, string name)
{
this.ds = ds;
this.name = name;
}
private readonly DataSet ds;
private readonly string name;
public void Execute()
{
HttpResponse Response = HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + name + ".pdf");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 10, 10, 90, 10);
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
//set some header stuff
document.AddTitle(name);
document.AddSubject("Table of " + name);
document.AddCreator("This Application");
document.AddAuthor("Me");
// we Add a Header that will show up on PAGE 1
Phrase phr = new Phrase(""); //empty phrase for page numbering
HeaderFooter footer = new HeaderFooter(phr, true);
document.Footer = footer;
// step 3: we open the document
document.Open();
// step 4: we add content to the document
CreatePages(document);
// step 5: we close the document
document.Close();
}
public void CreatePages(Document document)
{
bool first = true;
foreach (DataTable table in ds.Tables)
{
if (first)
first = false;
else
document.NewPage();
document.Add(FormatHeaderPhrase(table.TableName));
PdfPTable pdfTable = new PdfPTable(table.Columns.Count);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100; // percentage
pdfTable.DefaultCell.BorderWidth = 2;
pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
foreach (DataColumn column in table.Columns)
{
pdfTable.AddCell(column.ColumnName);
}
pdfTable.HeaderRows = 1; // this is the end of the table header
pdfTable.DefaultCell.BorderWidth = 1;
Color altRow = new Color(242, 242, 242);
int i = 0;
foreach (DataRow row in table.Rows)
{
i++;
if (i % 2 == 1)
pdfTable.DefaultCell.BackgroundColor = altRow;
foreach (object cell in row.ItemArray)
{
//assume toString produces valid output
pdfTable.AddCell(FormatPhrase(cell.ToString()));
}
if (i % 2 == 1)
pdfTable.DefaultCell.BackgroundColor = Color.WHITE;
}
document.Add(pdfTable);
}
}
/// <summary>
/// Formats the phrase. Apply your own font and size here.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
private static Phrase FormatPhrase(string value)
{
return new Phrase(value, FontFactory.GetFont(FontFactory.HELVETICA, 8));
}
private static Phrase FormatHeaderPhrase(string value)
{
return new Phrase(value, FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(255, 0, 0)));
}
}