C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Tools and Utilities » Miscellanous »

DLL iTextsharp- Used to Create PDF in ASP.NET


Posted Date: 13 May 2009    Resource Type: Tools and Utilities    Category: Miscellanous
Author: Lakhan PalMember Level: Diamond    
Rating: 1 out of 5Points: 10



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)


  • Responses

    Author: guru prasad    22 May 2009Member 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 Pal    22 May 2009Member Level: Diamond   Points : 1
    Hi-
    Please find the attached DLL File.
    otherwise get ot from gogle name of the dll is iTextSharp.dll


    Author: guru prasad    22 May 2009Member 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 Pal    22 May 2009Member Level: Diamond   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: BKL    27 Aug 2009Member 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: BKL    01 Sep 2009Member 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 :)


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    ITextsharp  .  Create PDF  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Stickies for desktop
    Previous Resource: Google Book Downloader
    Return to Discussion Resource Index
    Post New Resource
    Category: Miscellanous


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use