繁体   English   中英

使用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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM