Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends,
I want to create the PDF and download it while user clicks on the button. Values from DB will be concatenated to string "EmailHtml" (with in line HTML Tags);

When the execution comes to the document.Close(); line, it throws error.

My code:

C#
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;

string PDF_Filename = company_name_en;

Document document = new Document(PageSize.A4, 80, 50, 30,65);

PdfWriter.GetInstance(document, new FileStream(Server.MapPath("pdf/" + PDF_Filename.ToString() + ".pdf"), FileMode.Create));

document.Open();

System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(new StringReader(EmailHtml));

HtmlParser.Parse(document, reader);

if (document.IsOpen())
{
    document.Close();
}

Response.Redirect("Pdf/" + PDF_Filename.ToString() + ".pdf");


The document has no pages. <br />
<br />
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.<br />
<br />
Exception Details: System.IO.IOException: The document has no pages.
Posted
Updated 19-Jul-10 4:01am
v4

1 solution

I found this using google ("itextsharp htmlparser") at the following link. How many times do we have to tell people that google should be ther FIRST thing you should use to find an answer to your questions?

http://forums.asp.net/t/1199774.aspx[^]

---------------------------------

I got itextsharp version 4.1.6.0 and got it to work like this for parsing html not in a file...:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections;
using System.Text;
using iTextSharp.text.xml;
using iTextSharp.text.html;

    public partial class itexttest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //create document
          Document document = new Document();
            try {
                //writer - have our own path!!! and see you have write permissions...
       PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/") + "WordDoc/" + "parsetest.pdf", FileMode.Create));
           document.Open();
                //html -text - kan be from database or editor too
 String htmlText="<font hold=" />" color="\"#0000FF\"">Title One</font><font hold=" />" color="\"black\""><br><br>Some text here<br><br><br><font hold=" />" color="\"#0000FF\"">Another title here   " +
" </font><font hold=" />" color="\"black\""><br><br>Text1<br>Text2<br><ol><li>hi</li><li>how are u</li></ol>";

 //make an arraylist ....with STRINGREADER since its no IO reading file...
ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
                //add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
    document.Add((IElement)htmlarraylist[k]);
}

                document.Add(new Paragraph("And the same with indentation...."));

// or add the collection to an paragraph 
                // if you add it to an existing non emtpy paragraph it will insert it from
                //the point youwrite -
Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
mypara.IndentationLeft = 36;
mypara.InsertRange(0, htmlarraylist);
document.Add(mypara);
document.Close();        
  }
  catch (Exception exx) {
   Console.Error.WriteLine(exx.StackTrace);
   Console.Error.WriteLine(exx.Message);
  }
 }

}</br></br></br></br></font></br></br></br></br></br></font>
 
Share this answer
 
Comments
Sathish Kumar. K 20-Jul-10 2:14am    
Hi John,
thanks for posting the code. but i look for the reason.before posting i googled to find the reason why its occur. your posted code is using lateset version .

i am using older version. this same older version is working fine with another project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900