PDFSharp

29 Octubre 2008

//Create a document

PdfDocument pdf = new PdfDocument();

//Create a Page

PdfPage page = new PdfPage();

page.Size = PageSize.A4;

pdf.AddPage(page);

// Create a font

XGraphics gfx = XGraphics.FromPdfPage(pdf.Pages[0], XGraphicsPdfPageOptions.Append);

XFont font = new XFont(“Times New Roman”, 12, XFontStyle.Regular);

XTextFormatter tf = new XTextFormatter(gfx);

//Write header

x = 20;

y = 2;

tf.DrawString(“Name”, font, XBrushes.Black,new XRect(x, y, 40, 20),   XStringFormat.TopLeft);

x = x + 42;

tf.DrawString(“Surname”, font, XBrushes.Black, new XRect(x, y, 270, 20), XStringFormat.TopLeft);

x = x + 272;

//Write Data

y = 105;

foreach (DataRow row in data.Rows)

{

x = 20;

tf.DrawString(row["Name"].ToString(), font, XBrushes.Black, new XRect(x, y, 40, 20), XStringFormat.TopLeft);

x = x + 42;

tf.DrawString(row["Surname"].ToString(), font, XBrushes.Black, new XRect(x, y, 270, 20), XStringFormat.TopLeft);

x = x + 272;

recordCount = recordCount + 1;

y = y + 20;

}

//Save the document.

pdf.Save(toFilename);

//Send document to client.

Response.Clear();

Response.ContentType = “application/pdf”;

Response.AddHeader(“Content-Disposition”, “attachment; filename=” +   toFilename.Substring(toFilename.LastIndexOf(@”\”) + 1));

Response.WriteFile(toFilename);

}