使用ItextSharp创建和下载PDF时出错 [英] Error while creating and downloading PDF using ItextSharp

查看:69
本文介绍了使用ItextSharp创建和下载PDF时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,
我想创建PDF并在用户单击按钮时下载它.来自DB的值将连接到字符串"EmailHtml"(带有行内HTML标记);

当执行到document.Close();行时,将引发错误.

我的代码:

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:

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.

推荐答案

我在以下链接中使用google("itextsharp htmlparser")找到了它.我们必须告诉人们多少次Google应该是您用来查找问题答案的第一件事?

http://forums.asp.net/t/1199774.aspx [
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...:

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>


这篇关于使用ItextSharp创建和下载PDF时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆