我正在使用ITextSharp生成PDF,我在pdf文档中生成HTML表格,但它在PDF文件中无法正确显示。我复制了生成的HTML并签入了HTML文件,它在HTML文件中正确显示
这是HTML文件中的HTML
这是我的PDF文件
这是我的代码
var data = MyClass.GetHistory();//It returns a Data Table
var stream = new MemoryStream();
Document document = new Document(PageSize.A4, 50, 50, 25, 25);
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
StringBuilder sb = new StringBuilder();
sb.Append("<table style=\"border-collapse: collapse; border-spacing: 0; width: 100%; margin-bottom: 15px; border: 1px solid #dddddd; font-family: Arial, Helvetica, sans-serif; font-size:14px\">");
sb.Append("<thead>");
sb.Append("<tr>" + title + "</tr");
sb.Append("<tr>");
sb.Append(" <th style=\"padding: 8px; line-height: 1.428571429; vertical-align: top; border-top: 1px solid #dddddd; border: 1px solid #dddddd; border-bottom: 2px solid #dddddd; background:#DCEBFD; text-align:left;\">Date</th>");
sb.Append(" <th style=\"padding: 8px; line-height: 1.428571429; vertical-align: top; border-top: 1px solid #dddddd; border: 1px solid #dddddd; border-bottom: 2px solid #dddddd; background:#DCEBFD; text-align:left;\">Description</th>");
sb.Append(" <th style=\"padding: 8px; line-height: 1.428571429; vertical-align: top; border-top: 1px solid #dddddd; border: 1px solid #dddddd; border-bottom: 2px solid #dddddd; background:#DCEBFD; text-align:left;\">City</th>");
sb.Append(" <th style=\"padding: 8px; line-height: 1.428571429; vertical-align: top; border-top: 1px solid #dddddd; border: 1px solid #dddddd; border-bottom: 2px solid #dddddd; background:#DCEBFD; text-align:left;\">State</th>");
sb.Append(" </tr>");
sb.Append(" </thead>");
sb.Append(" <tbody>");
foreach (var rows in data.Rows)
{
sb.Append("<tr>");
foreach (var item in ((System.Data.DataRow)(rows)).ItemArray)
{
sb.Append("<td style=\"padding: 8px; line-height: 1.428571429; vertical-align: top; border-top: 1px solid #dddddd; border: 1px solid #dddddd; width: 125px\">" + item + "</td>");
}
sb.Append("</tr>");
}
//sb.Append("<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>");
//sb.Append(" <tr><td>This is a test</td><td>2/td><td>3</td><td>4</td><td>5</td><tr/>");
//sb.Append(" <tr><td>This is a test</td><td>This is a test</td><td>This is a test</td><td>This is a test</td><td>This is a test</td><tr/>");
sb.Append(" </tbody>");
sb.Append(" </table>");
其次我想从日期只显示年份和月份,如果当前项目是日期时间,我如何检查foreach循环内部?
答案 0 :(得分:0)
我建议你在这种情况下使用iTextSharp.text.pdf.PdfPTable
对象。 html表似乎更简单,您可以使用iTextSharp.text.pdf.PdfPTable
更快地完成您的需求。