我使用以下代码使用.iTextSharp版本5.4.3生成PDF。
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
string data = @"<table cellspacing="0" border="0" style="border-collapse:collapse;margin-top:30px;;">
<tr>
<td></td><td>Visit</td><td></td><td></td><td></td>
</tr><tr>
<th>Datw</th><td>07/01/2013</td><td>07/18/2013</td><td>07/17/2013</td><td>07/09/2013</td>
</tr><tr>
<th>Score</th><td>3.00</td><td>6.33</td><td>1.00</td><td>8.00</td>
</tr><tr>
<th>Heading</th><td>7.0</td><td>8.0</td><td>2.0</td><td>3.0</td>
</tr><tr>
<th>Minutes</th><td>88</td><td>n/a</td><td>22</td><td>n/a</td>
</tr><tr>
<th>Test Data</th><td>5.0</td><td>8.0</td><td>4.0</td><td>3.0</td>
</tr><tr>
<th>Status</th><td>8.0</td><td>8.0</td><td>3.0</td><td>3.0</td>
</tr><tr>
<th>Data </th><td>3.96</td><td>6.88</td><td>5.83</td><td>6.67</td>
</tr><tr>
<th>Assessment (0-10)</th><td>5.0</td><td>3.0</td><td>2.0</td><td>2.0</td>
</tr><tr>
<th class="seperator">With heading</th><td>n/a</td><td>n/a</td><td>n/a</td><td>n/a</td>
</tr>
</table>";
IElement ele;
PdfPTable t;
var stringWriter = new StringWriter();
StyleSheet styles = new StyleSheet();
styles.LoadStyle("seperator", "border-top", "#a9a9a9 2px solid");
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(data), styles);
for (int k = 0; k < htmlarraylist.Count; k++)
{
ele = htmlarraylist[k];
document.Add(ele);
//document.Add((IElement)htmlarraylist[k]);
}
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf");
Response.BinaryWrite(output.ToArray());
虽然它生成PDF,但它没有应用样式。它也没有以粗体显示标签。
我错过了什么吗?谢谢
答案 0 :(得分:6)
您正在使用HTMLWorker
,这是一个不再受支持的过时类。它不支持CSS文件,这也解释了为什么有关iText,HTML和CSS的所有问题仍未得到答复。您应该使用 XML Worker 而不是HTMLWorker
类。请参阅http://sourceforge.net/projects/itextsharp/files/xmlworker和http://demo.itextsupport.com/xmlworker
答案 1 :(得分:1)
请尝试以下代码。我已经测试并应用了所有样式:
class Program
{
private static void createPDF(string html)
{
FileStream fs = new FileStream("test.pdf", FileMode.Create);
TextReader reader = new StringReader(html);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
fs.Close();
}
static void Main(string[] args)
{
string data = @"<table cellspacing='0' border='0' style='border-collapse:collapse;margin-top:30px;color:red;'>
<tr>
<td></td><td>Visit</td><td></td><td></td><td></td>
</tr><tr>
<th>Datw</th><td>07/01/2013</td><td>07/18/2013</td><td>07/17/2013</td><td>07/09/2013</td>
</tr><tr>
<th>Score</th><td>3.00</td><td>6.33</td><td>1.00</td><td>8.00</td>
</tr><tr>
<th>Heading</th><td>7.0</td><td>8.0</td><td>2.0</td><td>3.0</td>
</tr><tr>
<th>Minutes</th><td>88</td><td>n/a</td><td>22</td><td>n/a</td>
</tr><tr>
<th>Test Data</th><td>5.0</td><td>8.0</td><td>4.0</td><td>3.0</td>
</tr><tr>
<th>Status</th><td>8.0</td><td>8.0</td><td>3.0</td><td>3.0</td>
</tr><tr>
<th>Data </th><td>3.96</td><td>6.88</td><td>5.83</td><td>6.67</td>
</tr><tr>
<th>Assessment (0-10)</th><td>5.0</td><td>3.0</td><td>2.0</td><td>2.0</td>
</tr><tr>
<th class='seperator'>With heading</th><td>n/a</td><td>n/a</td><td>n/a</td><td>n/a</td>
</tr>
</table>";
createPDF(data);
Console.ReadKey();
}
}