Archive

Archive for the ‘PDF’ Category

PDF Generation on Fly Using iTextSharp

April 13, 2010 Leave a comment

U can generate PDF on the fly by the following function before that you have tyo ensure that you have include iTextShat dll in your project.

public static void GeneratePDFTemplate(CInvoiceList oInvoiceList, Page oPage)
{
oPage.Response.ContentType = “application/pdf”;
oPage.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
PdfPTable datatable = new PdfPTable(7);

datatable.DefaultCell.Padding = 0;
float[] headerwidths = { 15, 1, 25, 2, 12, 1, 26 }; // percentage
datatable.SetWidths(headerwidths);
datatable.WidthPercentage = 100; // percentage

datatable.DefaultCell.BorderWidth = 0;

Paragraph oParagraph = new Paragraph(“Name of Company”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
Paragraph oColon = new Paragraph(“:”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));

datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.CompanyName, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“Customer ID”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(“munirul_islam@cyberjahan.com”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.CompleteRow();

oParagraph = new Paragraph(“Name of Contact Person”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.ContactPersonName, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“OR”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
datatable.AddCell(“”);
datatable.CompleteRow();

oParagraph = new Paragraph(“Address”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.Address, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“CJ Staff ID”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(“upol@cyberjahan.com”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.CompleteRow();

oParagraph = new Paragraph(“Phone”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.Phone, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“CJ Staff Name”, new Font(Font.TIMES_ROMAN, 8, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(“upol Uddin(092324)”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.CompleteRow();

oParagraph = new Paragraph(“Mobile”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.Mobile, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“OR”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(“”);
datatable.AddCell(“”);
datatable.AddCell(“”);
datatable.CompleteRow();

oParagraph = new Paragraph(“Fax”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.Fax, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“CJ Agent ID”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(“Kaml@cyberjahan.com”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.CompleteRow();

oParagraph = new Paragraph(“Email”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.Email, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
oParagraph = new Paragraph(“CJ Agent Name”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(“Kamal Hassan(2314234)”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.CompleteRow();

oParagraph = new Paragraph(“Url”, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(oColon);
oParagraph = new Paragraph(oInvoiceList.ClientInformation.Url, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
datatable.AddCell(oParagraph);
datatable.AddCell(“”);
datatable.AddCell(“”);
datatable.AddCell(“”);
datatable.AddCell(“”);
datatable.CompleteRow();

hw.WriteLine(“<table >”);
hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’4′ align=’center’><u><b>Invoice</b></u></td>”);
hw.WriteLine(“</tr>”);
hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’4′ align=’center’>&nbsp;</td>”);
hw.WriteLine(“</tr>”);
hw.WriteLine(“</table>”);

hw.WriteLine(“<table>”);
hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’2′ align=’left’><b>Invoice No: ReV-0000191</b></td>”);
hw.WriteLine(“<td colspan=’2′ align=’right’><b>Date:” + DateTime.Now.ToString() + “</b></td>”);
hw.WriteLine(“</tr>”);
hw.WriteLine(“</table>”);
hw.WriteLine(“<table border=’1′>”);
hw.WriteLine(“<tr>”);
hw.WriteLine(“<td><b>Portal Type</b></td>”);
hw.WriteLine(“<td><b>Service Type</b></td>”);
hw.WriteLine(“<td><b>Duration</b></td>”);
hw.WriteLine(“<td><b>Amount in BDT</b></td>”);
hw.WriteLine(“</tr>”);

string sPrevPortalType = string.Empty;
foreach (CInvoice oInvoice in oInvoiceList.InvoiceList)
{

if (sPrevPortalType != oInvoice.PortalType)
{
sPrevPortalType = oInvoice.PortalType;

hw.WriteLine(“<tr>”);
hw.WriteLine(“<td rowspan=’2′>” + oInvoice.PortalType + “</td>”);
hw.WriteLine(“<td>” + oInvoice.ServiceType + “</td>”);
hw.WriteLine(“<td>” + oInvoice.Duration + “</td>”);
hw.WriteLine(“<td align=’center’>” + oInvoice.Amount + “</td>”);
hw.WriteLine(“</tr>”);
}
else
{
hw.WriteLine(“<tr>”);
hw.WriteLine(“<td align=’center’>,,</td>”);
hw.WriteLine(“<td>” + oInvoice.ServiceType + “</td>”);
hw.WriteLine(“<td>” + oInvoice.Duration + “</td>”);
hw.WriteLine(“<td align=’center’>” + oInvoice.Amount + “</td>”);
hw.WriteLine(“</tr>”);
}
}

hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’3′ align=’right’>Total</td>”);
hw.WriteLine(“<td align=’center’>” + oInvoiceList.Total + “</td>”);
hw.WriteLine(“</tr>”);

hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’3′ align=’right’>Discount</td>”);
hw.WriteLine(“<td align=’center’>” + oInvoiceList.Total + “</td>”);
hw.WriteLine(“</tr>”);

hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’3′ align=’right’>Vat(15%)</td>”);
hw.WriteLine(“<td align=’center’>” + oInvoiceList.Total + “</td>”);
hw.WriteLine(“</tr>”);

hw.WriteLine(“<tr>”);
hw.WriteLine(“<td colspan=’3′ align=’right’><b>Net Payable</b></td>”);
hw.WriteLine(“<td align=’center’><b>” + oInvoiceList.Total + “</b></td>”);
hw.WriteLine(“</tr>”);

hw.WriteLine(“<tr>”);
hw.WriteLine(“<td align=’left’>Amount in words:</td>”);
hw.WriteLine(“<td colspan=’3′ align=’left’>” + oInvoiceList.AmountInWords + “</td>”);

hw.WriteLine(“</tr>”);

hw.WriteLine(“</table>”);

//string sPath = oPage.Server.MapPath(“”);
string sPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + “App_Themes\\Black\\images\\logo.jpg”;
//Image gif = Image.getInstance(“../../App_Themes/Black/images/logo.gif”);

//System.Drawing.Image gif = System.Drawing.Image.FromFile(“../../App_Themes/Black/images/logo.gif”);

iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(sPath);
gif.Alignment = iTextSharp.text.Image.ALIGN_CENTER;

//iTextSharp.text.Phrase gif1 = new iTextSharp.text.Phrase(new Chunk(“Cyber Jahan Ltd. (www.cyberjahan.com)”));

StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, oPage.Response.OutputStream);

pdfDoc.Open();
pdfDoc.Add(gif);
// pdfDoc.Add(gif1);

pdfDoc.Add(datatable);
htmlparser.Parse(sr);
pdfDoc.Close();

oPage.Response.Write(pdfDoc);
oPage.Response.End();
}

Categories: PDF

working with pdf

December 13, 2009 Leave a comment

http://www.ujihara.jp/iTextdotNET/en/examples.html

CS Code:

http://www.ujihara.jp/iTextdotNET/examples/AcroForm.cs

Sample PDF form:

http://www.ujihara.jp/iTextdotNET/examples/AcroForm-Printable.pdf

Categories: PDF Tags:
Follow

Get every new post delivered to your Inbox.