Showing posts with label Convert Html to Pdf. Show all posts
Showing posts with label Convert Html to Pdf. 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();
}

Merge PDF Files using iTextSharp


iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.The code of the class I've written uses iText# and is based on the example code (Console Application) that can be found on http://itextsharp.sourceforge.net/examples/Concat.cs . However, this code seems to target an out of date version of iText# and can't be compiled without fixing some lines...This C#-class will allow you to merge multiple PDF's to one big PDF-file:// Based on : http://itextsharp.sourceforge.net/examples/Concat.csusing System;using System.IO;using iTextSharp.text;
using iTextSharp.text.pdf;
public class PdfMerge{public static void MergeFiles(string destinationFile, string[] sourceFiles){
try
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;int rotation;
// step 4: we add content
while (f < sourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the documentdocument.Close();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}