sharpPDF

sharpPDF is an open source DLL that will write a PDF stream. You can download the latest source at http://sourceforge.net/projects/sharppdf/.

I've had a difficult time finding any sample code that actually runs, but managed to piece together a working page that will import a graphic write a line of text and display a styled table.

Some things to note:

  • The pdf is represented by a document.
  • New pages can be added to the document.
  • The page is drawn in the first quadrant instead of the standard fourth.
  • Each item created is absolutely positioned on the page.

Below is a listing of the code for my working example. This library is not well documented but I think this is the best way to do what I’m doing.

View SharpPDF Example

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

// PDF Library
using sharpPDF;
using sharpPDF.PDFControls;
using sharpPDF.Enumerators;
using sharpPDF.Fonts;
using sharpPDF.Tables;
using sharpPDF.Collections;

public partial class sc_applications_enr_Default : System.Web.UI.Page
{
  // Used to translate first quadrent coordinate to fourth for layout in pdf file.
  protected int getTop(pdfPage myPage, int nTop)
  {
    return myPage.height - nTop;
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    // Define Font and Styles
    String strFont = "Helvetica";
    int leftMargin = 30;
    int topMargin = 30;
    int bottomMargin = 30;
    int lineHeight = 20;
    int linesPerPage;

    try
    {
      // Create document contents
      pdfDocument myDoc = new pdfDocument("PDF Sample Report", "planetWhat.com");
      
      // Add a page
      pdfPage myPage = myDoc.addPage(predefinedPageSize.csSharpPDFFormat);

      // Load an image (I was able to load JPEG, GIF, TIFF, and BMP formats)
      myDoc.addImageReference(Server.MapPath("~/Assets/Images/Sample.gif"), "ex");
      myPage.addImage(myDoc.getImageReference("ex"), leftMargin, getTop(myPage, 120));

      // Header Text
      myPage.addText("Verification of Enrollment", 140, getTop(myPage, 110),
                      myDoc.getFontReference(strFont), 20, pdfColor.Black);
     
      // Body Text
      myPage.addText("Employee Name: Donald Duck", leftMargin, getTop(myPage, 150),
                     myDoc.getFontReference(strFont), 10, pdfColor.Black);
      myPage.addText("Enrolled: November 14, 2007", leftMargin, getTop(myPage, 170),
                     myDoc.getFontReference(strFont), 10, pdfColor.Black);

      // Table Definition
      myPage.addText("Selections:", leftMargin + 50, getTop(myPage, 255),
                      myDoc.getFontReference(strFont), 10, pdfColor.Blue);
      pdfTable myTable = new pdfTable(myDoc, 1, new pdfColor("000000"), 5, 
                                      new pdfTableStyle(myDoc.getFontReference(strFont),
                                      12, pdfColor.Black, new pdfColor("d9d1b3")),
                                      new pdfTableStyle(myDoc.getFontReference(strFont),
                                      10, pdfColor.Black, pdfColor.White),
                                      new pdfTableStyle(myDoc.getFontReference(strFont),
                                      10, pdfColor.Black, pdfColor.White));
      myTable.coordX = leftMargin + 50;
      myTable.coordY = getTop(myPage, 260);

      // Create table's header
      myTable.tableHeader.rowHeight = 25;
      myTable.tableHeader.addColumn(175, predefinedAlignment.csCenter);
      myTable.tableHeader[0].addText("Individuals");
      myTable.tableHeader.addColumn(150, predefinedAlignment.csCenter);
      myTable.tableHeader[1].addText("Amount");

      // Create Table Row
      pdfTableRow myRow = myTable.createRow();
      myRow.rowHeight = 15;
      myRow[0].addText("Mickey Mouse");
      myRow[1].addText((7 * 1000).ToString("C0"));
      myTable.addRow(myRow);
      myPage.addTable(myTable);

      // Set response to the new PDF
      Response.Clear();
      Response.Buffer = true;
      Response.ContentType = "application/pdf";
      myDoc.createPDF(Response.OutputStream);
      Response.End();
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
        Response.End();
    }
  }
}