iTextSharp - 将一段HTML输出为PDF(放入表格单元格) [英] iTextSharp - Outputting a segment of HTML into PDF (into a Table Cell)

查看:127
本文介绍了iTextSharp - 将一段HTML输出为PDF(放入表格单元格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp为ASP.NET应用程序生成PDF,PDF生成似乎工作正常,但我发现iTextSharp使用起来有点不直观,但这是一个不同的故事。

I'm using iTextSharp to generate PDFs for an ASP.NET application, the PDF generation seems to work fine, though I'm finding iTextSharp a little unintuitive to use, but that's a different story.

我将数据放入PDF中的表格中,现在我想将HTML内容放入表格单元格并保持格式/样式。我已经看到很多例子展示了如何使用iTextSharp将HTML解析为PDF并保持格式化,但是这些示例都将内容直接吐出到文档对象中,例如。 doc.addElement()我试图调整代码以将解析的HTML内容吐出到表格单元格中,例如。而不是......

I'm putting data into a table inside my PDF, now I want to place HTML content into a table cell and have it maintain the formatting/styling. I've seen plenty of examples show how to parse HTML into a PDF using iTextSharp and maintain formatting, but the examples all spit the content out directly into the document object eg. doc.addElement() I've tried to adapt the code to spit the parsed HTML content into a table cell eg. instead of...

objects = HTMLWorker.ParseToList(new StringReader(htmlString), styles);
for (int k = 0; k < objects.Count; ++k)
{
    document.Add((IElement) objects[k]);
}

我正在使用...

Cell cell = new Cell();

objects = HTMLWorker.ParseToList(new StringReader(htmlString), styles);
for (int k = 0; k < objects.Count; ++k)
{
    cell.Add((IElement) objects[k]);
}

table.AddCell(cell);
document.Add(table);

然而,它将数据放入表格单元格中,所有格式正确,但所有内容都重叠并位于顶部彼此而不是间隔开。有什么我做错了。

However, it puts the data into the table cell all properly formatting, but with everything overlapping and on top of each other rather than spaced out. Is there anything I'm doing wrong.

上面的代码示例来自这个网站 http://blog.rubypdf.com/2007/10/10/using-htmlworker -to-parse-html-snippets-and-convert-to-pdf /

The above code sample came from this website http://blog.rubypdf.com/2007/10/10/using-htmlworker-to-parse-html-snippets-and-convert-to-pdf/

注意:我不打算维护CSS样式,只是那样一个< h1> 大致看起来像< h1> 通常看起来像等。

Note: I'm not looking to maintain CSS styles, only that an <h1> roughly looks like what an <h1> normally looks like etc.

推荐答案

使用 PdfPTable& PdfPCell 类而不是表&单元格类。

(我尝试使用Table / Cell,我得到了你描述的相同行为,但是使用PdfPtable / PdfPCell就可以了。)

Use PdfPTable & PdfPCell classes instead of Table & Cell classes.
(I tried with Table/Cell, I got the same behavior you describe, but with PdfPtable/PdfPCell it was ok.)

这篇关于iTextSharp - 将一段HTML输出为PDF(放入表格单元格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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