【问题标题】:how to create PDF from HTML stored in a string from database using itextsharp如何使用 itextsharp 从存储在数据库字符串中的 HTML 创建 PDF
【发布时间】:2010-08-02 07:02:21
【问题描述】:

我需要从编辑器存储的数据库提供的 HTML 创建一个 pdf 文件...

我必须用pdf中的所有样式显示相应的HTML...请帮助

我使用了 itextsharp 方法,但是当我将其转换为 pdf 时,我没有得到我在编辑器中提供的正确内容,

我使用的代码

    string content = "<the html content from database>";

    StringReader reader = new StringReader(content);
    MemoryStream ms = new MemoryStream();
    Document doc = new Document(PageSize.A4,50,50,30,30);
    HTMLWorker parser = new HTMLWorker(doc);
    PdfWriter.GetInstance(doc, ms);
    doc.Open();
    try
    {
        parser.Parse(reader);
    }
    catch(Exception ex)
    {
        Paragraph paragraph = new Paragraph("Error! " + ex.Message);
        paragraph.SetAlignment("center");
        Chunk text = paragraph.Chunks[0] as Chunk;
        doc.Add(paragraph);
    }
    finally
    {
        doc.Close();
    }
    Byte[] buffer = ms.GetBuffer();
    if (buffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }

这有什么问题吗,请帮助代码从 html 创建 pdf

【问题讨论】:

    标签: .net pdf


    【解决方案1】:

    这是对我有用的代码。

         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 Default2 : System.Web.UI.Page
         {
               protected void Page_Load(object sender, EventArgs e)
               {
                //create document
                Response.Write(Server.MapPath("."));
                Document document = new Document();
                try
                {
                  //writer - have our own path!!!
                   PdfWriter.GetInstance(document, new FileStream(Server.MapPath(".") +         
                      "parsetest.pdf", FileMode.Create));
            document.Open();
            //html -text - kan be from database or editor too
            String htmlText = "<font  " +
            " color=\"#0000FF\"><b><i>Title One</i></b></font><font   " +
            " color=\"black\"><br><br>Some text here<br><br><br><font   " +
            " color=\"#0000FF\"><b><i>Another title here   " +
            " </i></b></font><font   " +
            " 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...
            List<IElement> 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);
        }
     }
    }
    

    【讨论】:

    • 我在这一行遇到异常...列表 非泛型类型 'iTextSharp.text.List' 不能与类型参数 List htmlarraylist = iTextSharp.text.html.simpleparser 一起使用.HTMLWorker.ParseToList(new StringReader(htmlText), null);
    • 当我使用数组而不是列表时出现异常无法将类型'system.collection,generic.list 隐式转换为 system.collections.arraylist
    • 我更正了这些问题,但如果 html 中存在任何样式,则 itz 不会进入 pdf
    • 您只需到这里,您将了解如何在从 html today.java.net/pub/a/today/2007/06/26/… 生成 pdf 的同时包含 css
    • 这都是关于java的,是itextsharp的问题..内联样式永远不会出现..?
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多