Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用itextsharp方法从数据库中存储在字符串中的HTML创建PDF_Itext_Itext7 - Fatal编程技术网

如何使用itextsharp方法从数据库中存储在字符串中的HTML创建PDF

如何使用itextsharp方法从数据库中存储在字符串中的HTML创建PDF,itext,itext7,Itext,Itext7,我需要从一个编辑器中存储的数据库中创建一个pdf文件 我使用了该方法,但当我将其转换为pdf时,没有得到编辑器中提供的正确内容 返回到pdf仅包含数据库表内容的第一行 请帮忙 注意:我尝试过使用这种方法,但没有成功,错误是 无法加载文件或程序集“BouncyCastle.Crypto,版本=1.8.1.0, 区域性=中性,PublicKeyToken=null'或其依赖项之一 我用的代码 using System; using System.Collections.Generic; using

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

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

返回到
pdf
仅包含数据库表
内容的第一行

请帮忙

注意:我尝试过使用这种方法,但没有成功,错误是

无法加载文件或程序集“BouncyCastle.Crypto,版本=1.8.1.0, 区域性=中性,PublicKeyToken=null'或其依赖项之一

我用的代码

using System;
using System.Collections.Generic;
using System.Web.UI;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html.simpleparser;
using MySql.Data.MySqlClient;
using System.Configuration;

public partial class Default5 : Page
{
    string connString;
    string sql;
    string htmlText;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //create document
            Response.Write(Server.MapPath("public/"));
            Document document = new Document();

            try
            {
                PdfWriter.GetInstance(document, new FileStream(Server.MapPath("public/") +
                   "parsetest.pdf", FileMode.Create));
                document.Open();

                connString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;

                sql = @String.Format(" SELECT * FROM `contents`; ");

                using (MySqlConnection con =
                    new MySqlConnection(connString))
                {
                    using (MySqlCommand cmd =
                        new MySqlCommand(sql, con))
                    {
                        cmd.Connection.Open();

                        MySqlDataReader reader = cmd.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                htmlText = reader["contents"].ToString() + "<br />";
                                lbcontents.Text += htmlText.ToString() + "<br />";

                                List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);

                                for (int k = 0; k < htmlarraylist.Count; k++)
                                {
                                    document.Add((IElement)htmlarraylist[k]);
                                }

                                document.Add(new Paragraph("And the same with indentation...."));
                                Paragraph mypara = new Paragraph();
                                mypara.IndentationLeft = 36;
                                mypara.InsertRange(0, htmlarraylist);
                                document.Add(mypara);
                                document.Close();
                            }
                        }
                        else
                        {
                            Console.WriteLine("No rows found.");
                        }

                        reader.Close();
                        cmd.Connection.Close();
                    }
                }

            }
            catch (Exception exx)
            {
                Console.Error.WriteLine(exx.StackTrace);
                Console.Error.WriteLine(exx.Message);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Web.UI;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用System.IO;
使用iTextSharp.text.html.simpleparser;
使用MySql.Data.MySqlClient;
使用系统配置;
公共部分类Default5:第页
{
字符串连接字符串;
字符串sql;
字符串htmlText;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
//创建文档
Write(Server.MapPath(“public/”));
文档=新文档();
尝试
{
GetInstance(文档,新文件流(Server.MapPath(“public/”)+
“parsetest.pdf”,FileMode.Create);
document.Open();
connString=ConfigurationManager.ConnectionString[“cn”].ConnectionString;
sql=@String.Format(“从`contents`;中选择*”;
使用(MySqlConnection-con)=
新的MySqlConnection(connString))
{
使用(mysqlcmd)命令=
新的MySqlCommand(sql,con))
{
cmd.Connection.Open();
MySqlDataReader=cmd.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
{
htmlText=reader[“contents”].ToString()+“
”; lbcontents.Text+=htmlText.ToString()+“
”; List htmlaraylist=HTMLWorker.parsetList(新的StringReader(htmlText),null); for(int k=0;k