DLL iTextsharp- Used to Create PDF in ASP.NET

itextsharp.dll used to create pdf document in Dot Net.


private void CreateMyPDF()
{
Document doc = new Document(PageSize.A4);

PdfWriter writer = PdfWriter.GetInstance(doc, new
FileStream("C:\\lakhan.pdf", FileMode.Create));

doc.Open();

string strURL = "http://wiziq.com/index.aspx";
Uri uri = new Uri(strURL);

//Create the request object

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.UserAgent = "Get Content";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string html = sr.ReadToEnd();

ArrayList lt = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null);

ColumnText ct = new ColumnText(writer.DirectContent);

ct.SetSimpleColumn(50, 50, PageSize.A4.Width - 50, PageSize.A4.Height - 50);

for (int k = 0; k < lt.Count; ++k) {

ct.AddElement((IElement)lt[k]);

}

ct.Go();

doc.Close();

}


Attachments

  • ITextSharp.dll (28414-13823-itextsharp.dll)
  • Comments

    Author: guru prasad22 May 2009 Member Level: Gold   Points : 1

    Hi
    Thanks for ur code.

    But i'm unable to get "PdfWriter" object.Which reference/class shall i needs to import to get it.

    Is there any .dll needs to be downloaded to use this object.



    Thanks & Regards
    Guru Prasad

    Author: Lakhan Pal22 May 2009 Member Level: Gold   Points : 1

    Hi-
    Please find the attached DLL File.
    otherwise get ot from gogle name of the dll is iTextSharp.dll

    Author: guru prasad22 May 2009 Member Level: Gold   Points : 1

    Thanks for your code.
    This attached .dll not working.Any way i have downloaded from google.

    Code working properly, but how to get grdiview borders & gridview color schemes in to the .PDF file.


    Thanks & Regards
    Guru Prasad.

    Author: Lakhan Pal22 May 2009 Member Level: Gold   Points : 2

    Hi Guru-

    Check these function ..
    may be these will help you to resolve your issues:
    private void CreateMyPDF()
    {
    Document doc = new Document(PageSize.A4);

    PdfWriter writer = PdfWriter.GetInstance(doc, new
    FileStream("C:\\lakhan.pdf", FileMode.Create));

    doc.Open();

    string strURL = "http://wiziq.com/index.aspx";
    Uri uri = new Uri(strURL);

    //Create the request object

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.UserAgent = "Get Content";
    WebResponse resp = req.GetResponse();
    Stream stream = resp.GetResponseStream();
    StreamReader sr = new StreamReader(stream);
    string html = sr.ReadToEnd();

    ArrayList lt = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null);

    ColumnText ct = new ColumnText(writer.DirectContent);

    ct.SetSimpleColumn(50, 50, PageSize.A4.Width - 50, PageSize.A4.Height - 50);

    for (int k = 0; k < lt.Count; ++k) {

    ct.AddElement((IElement)lt[k]);

    }

    ct.Go();

    doc.Close();

    }

    private void CreateNewPDF()
    {
    string strURL="http://wiziq.com/index.aspx";
    Uri uri = new Uri(strURL);

    //Create the request object

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.UserAgent = "Get Content";
    WebResponse resp = req.GetResponse();
    Stream stream = resp.GetResponseStream();
    StreamReader sr = new StreamReader(stream);
    string html = sr.ReadToEnd();
    Document Doc = new Document();

    //PdfWriter.GetInstance
    //(Doc, new FileStream(Request.PhysicalApplicationPath
    //+ "\\AmitJain.pdf", FileMode.Create));

    PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\Lakhan.pdf", FileMode.Create));
    Doc.Open();

    Chunk c = new Chunk("Export HTML to PDF\n",FontFactory.GetFont("Verdana", 15));
    Paragraph p = new Paragraph();
    p.Alignment = Element.ALIGN_CENTER;
    p.Add(c);
    Chunk chunk1 = new Chunk("By Lakhan Pal Garg, \n",FontFactory.GetFont("Verdana", 8));
    Paragraph p1 = new Paragraph();
    p1.Alignment = Element.ALIGN_RIGHT;
    p1.Add(chunk1);

    Doc.Add(p);
    Doc.Add(p1);

    //System.Xml.XmlTextReader xmlReader =new System.Xml.XmlTextReader(new StringReader(html));
    //HtmlParser.Parse(Doc, xmlReader);
    HtmlParser.Parse(Doc, "E:\\Lakhan\\Projects\\Testweb\\1.html");

    Doc.Close();
    string Path = Environment.GetFolderPath
    (Environment.SpecialFolder.Desktop)
    + "\\Lakhan.pdf";


    ShowPdf(Path);


    }

    Author: BKL27 Aug 2009 Member Level: Bronze   Points : 1

    You rock my world! I got it to work with simple aspx pages that had some simple page behind code.

    Although it appears it has to be PERFECT xhtml. I've got a form with 1155 lines of html code I have to make sure is xhtml compliant. Yay!

    Author: BKL01 Sep 2009 Member Level: Bronze   Points : 2

    I found a few other articles about converting HTML to PDF this way and I noticed a lot of people having trouble with column widths.

    I have a lot of tables (regular HTML tables, not asp.net server tables) in the page I wanted to convert. The column width for each table was always the same no matter what WIDTH I had specified in the TD attribute or in the style.. even when I used itextsharp's STYLESHEET class to create a style.

    I found a workaround. You can adjust your column widths with the COLSPAN attribute. If you have four columns, all four columns will have the same width even if you want the first column to be wider. Just specify COLSPAN="2" in the first TD and that coloumn will be twice and wide as the remaining columns.

    Just thought I'd share :)

    Author: amit24 Dec 2009 Member Level: Bronze   Points : 2

    hi there ,

    I am using itextsharp.dll to convert my Html content to PDF.
    but the not able to implement the CSS. i have a invoice to convert it's a div section i need to convert. The html code is having all the Css working. but when i converted it to PDF the file is not showing propert fonts border lines and some other styles.

    is there any suggestion how to do this.

    Thanks

    Author: Manish Dave05 Jan 2010 Member Level: Bronze   Points : 1

    Dear All,
    Css,link and layout create a problem when it convert .......... plz give me any soulation if you have............ i have lots of html file that i want to convert what i do.........

    Guest Author: Gaurav Garg08 Jan 2013

    Dear Bloggers,

    I have the same issue adding Site.css file. I am using iTextSharp. Yes, I am able to genrate the PDF but without format or CSS. Please suggest if you have any clue to add colors, fonts etc into the generated PDF.

    Author: Lakhan Pal17 Jan 2013 Member Level: Gold   Points : 0

    Hi,

    Try writting CSS classes on teh same page instead of css file as when you will generate PDF it will not pick CSS classes from css file.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: