如何将html内容添加到文档中

时间:2015-09-21 09:02:30

标签: c# asp.net

我正在尝试将html转换为pdf。我有一个HTML代码。 例如:

var example_html = @"<p>This
                        <em>is</em>
                        <span class=""
                              headline""
                              style=""text-decoration: underline;"">some</span>
                        <strong>sample
                            <em>text</em>
                        </strong>
                        <span style=""color: red;"">!!!</span>
                    </p>";

我已经添加了下面的代码来生成这个html内容为pdf,但它抛出错误说“documnet没有页面”

Response.Clear();
Response.ContentType = "application/pdf";
using (Document doc = new Document())
{
    PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
    doc.Open();
    //doc.Add(new Paragraph(example_html));
    using (TextReader reader = File.OpenText(Server.MapPath("~/GST/HTMLtoPDF.aspx")))
    {
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, reader);
    }
    doc.Close();
}
Response.End();

在doc中我需要传递或添加我的html内容吗?但我不知道如何添加我的HTML内容,以便它可以解析为pdf.Kindly help.Thank you。

2 个答案:

答案 0 :(得分:1)

你可以试试这个

 //HTMLString = Pass your Html , fileLocation = File Store Location
        public void converttopdf(string HTMLString, string fileLocation)
        {
            Document document = new Document();

            PdfWriter.GetInstance(document, new FileStream(fileLocation, FileMode.Create));
            document.Open();

            List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
            for (int k = 0; k < htmlarraylist.Count; k++)
            {
                document.Add((IElement)htmlarraylist[k]);
            }

            document.Close();
        }

http://sourceforge.net/projects/itextsharp/files/itextsharp/iTextSharp-5.4.1/

答案 1 :(得分:0)

由于错误声明您没有在文档中添加页面。以下代码对我有用,它返回一个PDF标题MyDoc.pdf。

在您提出的问题中,您提到要将 example_html 转换为PDF,但代码示例正在读取名为 HTMLtoPDF.aspx 的文件。确保该文件具有该HTML。我尝试直接从文件和变量中读取它,它在两种情况下都有效。

要添加更多页面,请将 NewPage 方法和HTML调用到该页面,如下所示:

public ActionResult Index()
{
    HttpContext.Response.Clear();
    HttpContext.Response.ContentType = "application/PDF";
    HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "MyDoc.pdf");

    var page1 = @"<p>PAGE 1 This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
    var page2 = @"<p>PAGE 2 This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
    var page3 = @"<p>PAGE 3 This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";

    MemoryStream outputStream = new MemoryStream();
    using (Document doc = new Document())
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();

        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, new StringReader(page1));

        // To add more pages you can call NewPage and add other HTML snippets
        doc.NewPage();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, new StringReader(page2));
        doc.NewPage();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, new StringReader(page3));
    }

    return View();
}

此示例在每页上创建一个3页PDF,其中包含不同的HTML