13

我正在使用 ItextSharp 模块将下面列出的 html 转换为 pdf 页面。

        <div style="font-size: 18pt; font-weight: bold;">
    mma<br>mmar</div><br> <br>
    <div style="font-size: 14pt;">Click to View Pricing
    </div>
    <br>
    <div>
    <table>
    <tr><td> <a href="http://www.mma.com/fci" style="color: Blue; font-size: 10pt; text-decoration: underline;"> FCI</a>:</td> 
<td><a href="http://www.mma.com/access/?pn=78211-014" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 78211-014</a></td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-009" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-009</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-006" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-007" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-003" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-005" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-005</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-008"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-008</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-004" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-004</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-012" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-012</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-007LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-015LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-015LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-003LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-009LF" 
style="color: Blue; font-size: 10pt; text-decoration:
underline;">78211-009LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-005LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-005LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-010LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-010LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-006LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-014LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-014LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-004LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-004LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-012LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-012LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-008LF"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-008LF</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-011LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-011LF</td></tr><tr><td></td> <td><a href="http://www.mma.com/access/?pn=78211-013LF" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-013LF</td></tr><tr><td></td> <td><a href="http://www.mma.com/access/?pn=78211-010" style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-010</td></tr><tr><td></td>
<td><a href="http://www.mma.com/access/?pn=78211-015"
 style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-015</td></tr><tr><td> 
<a href="http://www.mma.com/souriau" 
style="color: Blue; font-size: 10pt; text-decoration: underline;"> Souriau</a>:</td>
 <td><a href="http://www.mma.com/access/?pn=24JR124-3" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 24JR124-3</a></td></tr></table></div>

生成上述 html 的 C# 代码:

 var html = new StringBuilder(@"<div style=""font-size: 18pt; font-weight: bold;"">Authorized Distributor</div><br> <br><div style=""font-size: 14pt;"">Click to View Pricing, Inventory, Delivery & Lifecycle Information:</div><br>");
            List<MfrBrandView> mfrBrands = MfrBrandView.Load(fileId);
            var uniquesuppliers = mfrBrands.GroupBy(t => new {t.Manufacturer,t.SupplierVirtualDirectory}).Select(g => g.First());
            html.Append("<div><table>");
            foreach (var uniquesupplier in uniquesuppliers)
            {
                var mfrPartNums =
                    mfrBrands.Where(t => t.ManufacturerId == uniquesupplier.ManufacturerId).Select(
                        t => t.ManufacturerPartNumber).ToList();
                html.AppendFormat(@"<tr><td> <a href=""http://www.mma.com/{0}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;""> {1}</a>:</td> <td><a href=""http://www.mma.com/access/?pn={2}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;""> {2}</a></td></tr>", uniquesupplier.SupplierVirtualDirectory, uniquesupplier.MouserizationLabel, mfrPartNums[0]);

                for (int i = 1; i < mfrPartNums.Count(); i++)
                {
                    html.AppendFormat(
                        @"<tr><td></td> <td><a href=""http://www.mma.com/access/?pn={0}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;"">{0}</td></tr>",
                        mfrPartNums[i]);
                }
            }
            html.Append("</table></div>");
            return html.ToString();

html形成后生成页面的代码:

PdfWriter writer = null;
            PdfDocument doc = null;
            //PdfReader reader = new PdfReader(content);
           // reader.Info["Title"]

            try
            {
                doc = new PdfDocument();
                doc.SetPageSize(PageSize.LETTER);
                writer = PdfWriter.GetInstance(doc, content);
                writer.CloseStream = false;
                doc.Open();
                doc.NewPage();

                foreach (IElement element in iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null))
                {
                    doc.Add(element);
                }
            }

下面是我得到一个异常的地方,说“无法将 'iTextSharp.text.html.simpleparser.CellWrapper'类型的对象转换为'iTextSharp.text.Paragraph'。” iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null)

我要解析的 html 有问题吗?有没有更好的方法来做到这一点?非常感谢您的帮助。非常感谢。

4

3 回答 3

24

`HTMLWorker'已被弃用,取而代之的是XMLWorker。这是一个使用上面使用的 HTML 片段测试的工作示例:

StringReader html = new StringReader(@"
<div style='font-size: 18pt; font-weight: bold;'>
Mouser Electronics <br />Authorized Distributor</div><br /> <br />
<div style='font-size: 14pt;'>Click to View Pricing, Inventory, Delivery & Lifecycle Information:
</div>
<br />
<div>
<table>
<tr><td></td><td>
<a href='http://www.mouser.com/access/?pn=78211-009' 
style='color: Blue; font-size: 10pt; text-decoration: underline;'>78211-009</a></td></tr>
</table></div>    
");      
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  XMLWorkerHelper.GetInstance().ParseXHtml(
    writer, document, html
  );
}

使用时,XMLWorker您需要使用格式良好的 HTML——毕竟它是一个 XML 解析器。您上面问题的示例HTML没有结束<a><br>标签。像HtmlAgilityPack这样的HTML解析器 将解决这些问题,并将其转为:

<div><img src='a.gif'><br><hr></div>

进入这个:

<div><img src='a.gif' /><br /><hr /></div>

只有几行代码:

var hDocument = new HtmlDocument()
{
    OptionWriteEmptyNodes = true,
    OptionAutoCloseOnEnd = true
};
hDocument.LoadHtml("<div><img src='a.gif'><br><hr></div>");
var closedTags  = hDocument.DocumentNode.WriteTo();

XMLWorker作为 nuget 包提供,或在 sourceforge 单独下载

有关XMLWorker. _ _

于 2012-08-29T15:59:46.880 回答
0

试试下面的代码

    &lt;a href="http://www.abcd.com"&gt;&lt;/a&gt;

在这里,我们将 < 替换为&lt; 和 > 与&gt; 所以我的变得&lt;a&gt;和变得 &lt;/a&gt;

于 2012-09-01T18:10:41.770 回答
0

这是您需要做的步骤

1- 从 nuget 的 Itextsharp 安装 itext sharp 和 XMLWorker

2-然后使用内联 CSS 将您的设计放入 html(此处为 Invoice.html)

3-确保所有标签都像断点或 td 一样正确结束(这里我之前遇到了错误)

4-这里的图片不会在本地显示,所以我不浪费时间在服务器上上传图片并提供服务器路径来访问 Image.U 可以研究更多以在本地运行它。

Document doc = new Document();

PdfPTable tableLayout = new PdfPTable(4);

PdfWriter writer= PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~/Admin/T13.pdf"), FileMode.Create));                 

doc.Open();

string contents = File.ReadAllText(Server.MapPath("~/Admin/invoice.html"));

StringReader sr = new StringReader(contents);


XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr);  

doc.Close();
于 2017-06-14T12:53:29.623 回答