Showing posts with label iTextSharp. Show all posts
Showing posts with label iTextSharp. Show all posts

Wednesday, June 8, 2011

Convert Html to Pdf using iTextSharp


This is iTestSharp 4.1.2 version and in Asp.net(C#)
Add the iTestSharp dll by adding it as Add Reference into your project and then you create a aspx,ascx page and write the below code into a function and call it from any event or add this function name into page_load event.

using System;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using iTextSharp.text;
using System.Data;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using System.Web;
private void GeneratePdf()
{
Console.WriteLine("Report");
MemoryStream m = new MemoryStream();
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=VisualROOF Inspection.pdf");
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
// step 1: creation of a document-object
Document document = new Document();

try
{
// step 2
// we create a writer that listens to the document
// and directs a PDF-stream to a file
//PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream); PdfWriter writer = PdfWriter.GetInstance(document, m);
writer.CloseStream = false;
// step 3:
// we Add a Footer that will show up on PAGE 1
Paragraph para = new Paragraph("©2009 NRCIA",FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.NORMAL,new Color(102,45,25)));
para.Add(Environment.NewLine);
para.Font =FontFactory.GetFont(FontFactory.HELVETICA, 8, iTextSharp.text.Font.NORMAL,new Color(237,28,36));
para.Add("This document is not a certification");
HeaderFooter footer = new HeaderFooter(para, false);
footer.Alignment = Element.ALIGN_CENTER;
document.Footer = footer;
// we open the document
document.Open();
// we rotate the page and add background image, starting from PAGE 2 document.SetPageSize(PageSize.A4.Rotate());
document.SetMargins(10f,10f,10f,10f);
// step 4: we Add content to the document
// PAGE 1
//add image into pdf
Image pdfImage = Image.GetInstance(Server.MapPath("/images/reports/inspection/InspectionReportImage.jpg"));
pdfImage.ScaleToFit(3500, 845);
pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(2, -5);

document.Add(pdfImage);
// Empty table for spacing
PdfPTable emptytable = new PdfPTable(3);
emptytable.TotalWidth = 560f;
emptytable.HorizontalAlignment = Element.ALIGN_CENTER;
emptytable.LockedWidth = true;
PdfPCell cell1 = new PdfPCell();
cell1.Padding = 60;
cell1.Colspan = 3;
cell1.Border = 0;
emptytable.AddCell(cell1);
document.Add(emptytable);

///actual table for data
/// PdfPTable table = new PdfPTable(3);
table.TotalWidth = 400f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.LockedWidth = true;
float[] widths = new float[] { 30f, 50f,50f };
//set the column widths
table.SetWidths(widths);
// Report No header name for cell
PdfPCell cell = new PdfPCell(new Phrase("Report No:", new Font(fBoldHeading, 10, 0, new Color(102, 45, 25))));
cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);
// header Prepared Exclusively for cell
cell = new PdfPCell(new Phrase("Prepared Exclusively for:", new Font(fBoldHeading, 10, 0, new Color(102, 45, 25))));
cell.HorizontalAlignment =1; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);
cell = new PdfPCell(new Phrase(ReportNo, new Font(fBoldHeading, 10, 0, new Color(0, 0, 0))));
cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);
// Prepared Exclusively Value for cell
cell = new PdfPCell(new Phrase(objInspMgt.PreparedFor, new Font(fBoldHeading, 10, 0, new Color(0, 0, 0))));
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);
//Add table to Document for First Page
document.Add(table);
//we remove the footer and new footer for page 2
document.ResetFooter();
// we reset the page numbering
//document.ResetPageCount();
// we Add a Footer that will show up on PAGE 2
Phrase para1 = new Phrase("©2009 This document is not a certification.", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 8, iTextSharp.text.Font.NORMAL, new Color(237, 28, 36)));
string spce = " ";
footer = new HeaderFooter(para1, false);
footer.Alignment = Element.ALIGN_CENTER;
footer.Border = Rectangle.TOP_BORDER;
footer.BorderWidthTop = 2;
footer.BorderColorTop = new Color(58, 38, 5);
footer.Bottom = 20f;
document.Footer = footer;
// set page size for all pages
document.SetPageSize(PageSize.A4);
document.SetMargins(10f, 10f, 0f, 10f);
// we trigger a page break Table OF CONTENT
document.NewPage();
// we Add some more content

}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
// step 6: Write pdf bytes to outputstream
//Response.Write(document);
//Response.End();
Response.ContentType = "application/pdf";
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length); Response.OutputStream.Flush();
Response.OutputStream.Close();
m.Close();
}

Get Address image from Google Map and Add it into iTextSharp Document PDF

//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?center=" + objInspMgt.PropertyInspectedAddress + "&zoom=14&size=612x612&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|41.726247,-97.002853&sensor=false");

//Map style roadMap
iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=roadmap&markers=size:mid|color:red|"+objInspMgt.PropertyInspectedAddress+"&sensor=false");

//Map style terrain
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=terrain&markers=size:mid|color:red|" + objInspMgt.PropertyInspectedAddress + "&sensor=false");

//Map style satellite
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=satellite&markers=size:mid|color:red|"+objInspMgt.PropertyInspectedAddress+"&sensor=false");

//Map style hybrid
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=hybrid&markers=size:mid|color:red|" + objInspMgt.PropertyInspectedAddress + "&sensor=false");


LocationImage.ScaleToFit(650, 650);
LocationImage.Alignment = iTextSharp.text.Image.UNDERLYING;
LocationImage.SetAbsolutePosition(0, 0);
locationcell = new PdfPCell(LocationImage);
locationcell.PaddingBottom = 10;
locationcell.PaddingTop = 10;
locationcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
locationcell.Colspan = 3;
locationcell.Border = 0;

Locationtable.AddCell(locationcell);
Document.Add(Locationtable);