Free PDF generator library for C#
iTextSharp
Download the iTextSharp library
http://sourceforge.net/projects/itextsharp/
Documentation
http://itextsharp.sourceforge.net/tutorial/index.html
Example from SinhalaGospel.com
private void GeneratePdf(int contID)
{
string path = "";
Document document = new Document();
try
{
cGospel gospel = new cGospel();
tbl_Gospel dtCont = gospel.GetContent(contID);
string contentID = dtCont.ContentID.ToString();
string gospelTitle = dtCont.Gospel;
string chapterID = dtCont.ChapterID.Value.ToString();
string titleID = dtCont.TitleID.ToString();
string title = dtCont.Title;
string content = dtCont.Content;
path = "../Contents/"+gospelTitle+"_"+chapterID+"_"+titleID+".pdf";
PdfWriter.GetInstance(document, new FileStream(Server.MapPath(path), FileMode.Create));
document.Open();
BaseFont bfBindu = BaseFont.CreateFont(Server.MapPath("../fm_bindu.ttf"), BaseFont.WINANSI, BaseFont.EMBEDDED);
iTextSharp.text.Font fontTitle1 = new iTextSharp.text.Font(bfBindu, 20,iTextSharp.text.Font.BOLD);
iTextSharp.text.Font fontTitle2 = new iTextSharp.text.Font(bfBindu, 18,iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font fontTitle3 = new iTextSharp.text.Font(bfBindu, 16,iTextSharp.text.Font.BOLD);
iTextSharp.text.Font fontBody = new iTextSharp.text.Font(bfBindu, 12);
Paragraph p1=new Paragraph();
p1.Add(new Chunk(gospelTitle+"\t",fontTitle1)); p1.Add(new Chunk(prefix+" "+chapterID+" - "+titleID,fontTitle2));
document.Add(p1);
document.Add(new Paragraph(title,fontTitle3));
document.Add(new Paragraph(content,fontBody));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
catch(Exception ex){throw ex;}
document.Close();
//frmPdf.Attributes.Add("src",path);
//Page.RegisterStartupScript("changesource","document.getElementById('"+frmPdf.ClientID+"').src = '"+path+"';");
//frmPdf.Attributes.Add("height","400px");
Response.Redirect(path);
}
Comments
Ling