使用 iText for .NET 从 HTML 输出图表
output a graph from HTML with iText for .NET
我正在使用 iText for .NET 从 html 源创建 pdf 文档,但我遇到了问题。我在本文档中有一个内置于 HTML 中的图表,但未显示在 PDF 输出中。
图表的 HTML 是:
<table style="height:100;" cellspacing="0" cellpadding="0">
<tbody>
<!-- BARS-->
<tr style="height: 100">
<td></td>
<td valign="bottom" style="padding: 0 1px">
<span>500</span>
<div style="height:60%; background-color: black"></div>
</td>
<td></td>
<td valign="bottom" style="padding: 0 1px">
<span>500</span>
<div style="height:20%; background-color: black"></div>
</td>
<td></td>
</tr>
<!-- AXIS-->
<tr style="background-color:red; padding: 0; height: 2px">
<td colspan="27">
<div style="height:0;width:100%;border:0;border-bottom:2px;border-style: solid;border-color: #000000"></div>
</td>
</tr>
<!-- MONTHS-->
<tr>
<td></td>
<td>Ene</td>
<td></td>
<td>Feb</td>
<td></td>
</tr>
</tbody>
</table>
以及从 html 创建 pdf 的 C# 代码(此代码工作正常):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace PruebaITextSharp
{
class Program
{
static void Main(string[] args)
{
try
{
MemoryStream ms = new MemoryStream();
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER);
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms);
document.Open();
iTextSharp.tool.xml.XMLWorkerHelper helper = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
string facturaHTML = File.ReadAllText("D:\route\to\source.html");
helper.ParseXHtml(writer, document, new StringReader(facturaHTML));
writer.Flush();
document.Close();
FileStream file = new FileStream("output.pdf", FileMode.Create, FileAccess.Write);
file.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
file.Flush();
file.Close();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
}
}
}
}
所以这终于对我有用了!
<!-- GRAPH -->
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<!--BARS-->
<tr>
<td width="100%">
<table width="100%" style="" cellspacing="0" cellpadding="0" align="left">
<tr style="font-size:5pt;height:130px;">
<!-- -->
<td style="font-size:5pt;width:4%"></td>
<!-- MONTH 1 -->
<td valign="bottom" style="font-size:5pt;width:5%">
<p valign="center" style="width:100%; height:8px;">label 1</p>
<div style="width:100%; height:20px; background-color:black">
</div>
</td>
<!-- -->
<td style="font-size:5pt;width:2%"></td>
<!-- MONTH 2 -->
<td valign="bottom" style="font-size:5pt;width:5%">
<p valign="center" style="width:100%; height:8px;">label 2</p>
<div style="width:100%; height:10px; background-color:gray">
</div>
</td>
<!-- -->
<td style="font-size:5pt;width:2%"></td>
<!-- MONTH 3 -->
<td valign="bottom" style="font-size:5pt;width:5%">
<p valign="center" style="width:100%; height:8px;">label 3</p>
<div style="width:100%; height:30px; background-color:gray">
</div>
</td>
<!-- -->
<td style="font-size:5pt;width:2%"></td>
</tr>
</table>
</td>
</tr>
<!-- AXIS -->
<tr style="background-color:black; padding: 0; height: 1px">
<td colspan="26" nowrap="" >
<div></div>
</td>
</tr>
<!-- MONTHS LABELS -->
<tr>
<td width="100%">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td style="font-size:5pt;" width="4%"></td>
<td style="font-size:5pt;" width="5%">month 1</td>
<td style="font-size:5pt;" width="2%"></td>
<td style="font-size:5pt;" width="5%">month 2</td>
<td style="font-size:5pt;" width="2%"></td>
<td style="font-size:5pt;" width="5%">month 3</td>
<td style="font-size:5pt;" width="2%"></td>
</tr>
</table>
</td>
</tr>
</table>
我正在使用 iText for .NET 从 html 源创建 pdf 文档,但我遇到了问题。我在本文档中有一个内置于 HTML 中的图表,但未显示在 PDF 输出中。
图表的 HTML 是:
<table style="height:100;" cellspacing="0" cellpadding="0">
<tbody>
<!-- BARS-->
<tr style="height: 100">
<td></td>
<td valign="bottom" style="padding: 0 1px">
<span>500</span>
<div style="height:60%; background-color: black"></div>
</td>
<td></td>
<td valign="bottom" style="padding: 0 1px">
<span>500</span>
<div style="height:20%; background-color: black"></div>
</td>
<td></td>
</tr>
<!-- AXIS-->
<tr style="background-color:red; padding: 0; height: 2px">
<td colspan="27">
<div style="height:0;width:100%;border:0;border-bottom:2px;border-style: solid;border-color: #000000"></div>
</td>
</tr>
<!-- MONTHS-->
<tr>
<td></td>
<td>Ene</td>
<td></td>
<td>Feb</td>
<td></td>
</tr>
</tbody>
</table>
以及从 html 创建 pdf 的 C# 代码(此代码工作正常):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace PruebaITextSharp
{
class Program
{
static void Main(string[] args)
{
try
{
MemoryStream ms = new MemoryStream();
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER);
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms);
document.Open();
iTextSharp.tool.xml.XMLWorkerHelper helper = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
string facturaHTML = File.ReadAllText("D:\route\to\source.html");
helper.ParseXHtml(writer, document, new StringReader(facturaHTML));
writer.Flush();
document.Close();
FileStream file = new FileStream("output.pdf", FileMode.Create, FileAccess.Write);
file.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
file.Flush();
file.Close();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
}
}
}
}
所以这终于对我有用了!
<!-- GRAPH -->
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<!--BARS-->
<tr>
<td width="100%">
<table width="100%" style="" cellspacing="0" cellpadding="0" align="left">
<tr style="font-size:5pt;height:130px;">
<!-- -->
<td style="font-size:5pt;width:4%"></td>
<!-- MONTH 1 -->
<td valign="bottom" style="font-size:5pt;width:5%">
<p valign="center" style="width:100%; height:8px;">label 1</p>
<div style="width:100%; height:20px; background-color:black">
</div>
</td>
<!-- -->
<td style="font-size:5pt;width:2%"></td>
<!-- MONTH 2 -->
<td valign="bottom" style="font-size:5pt;width:5%">
<p valign="center" style="width:100%; height:8px;">label 2</p>
<div style="width:100%; height:10px; background-color:gray">
</div>
</td>
<!-- -->
<td style="font-size:5pt;width:2%"></td>
<!-- MONTH 3 -->
<td valign="bottom" style="font-size:5pt;width:5%">
<p valign="center" style="width:100%; height:8px;">label 3</p>
<div style="width:100%; height:30px; background-color:gray">
</div>
</td>
<!-- -->
<td style="font-size:5pt;width:2%"></td>
</tr>
</table>
</td>
</tr>
<!-- AXIS -->
<tr style="background-color:black; padding: 0; height: 1px">
<td colspan="26" nowrap="" >
<div></div>
</td>
</tr>
<!-- MONTHS LABELS -->
<tr>
<td width="100%">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td style="font-size:5pt;" width="4%"></td>
<td style="font-size:5pt;" width="5%">month 1</td>
<td style="font-size:5pt;" width="2%"></td>
<td style="font-size:5pt;" width="5%">month 2</td>
<td style="font-size:5pt;" width="2%"></td>
<td style="font-size:5pt;" width="5%">month 3</td>
<td style="font-size:5pt;" width="2%"></td>
</tr>
</table>
</td>
</tr>
</table>