使用带有htmls表的itextsharp

时间:2015-03-25 04:31:50

标签: c# html itextsharp

我使用itextsharp编写HTML以使用以下代码创建PDF,但是我无法通过使用宽度添加emtpy行或更改td的宽度。这是我使用的代码

string customer = "客";
        string address = "地";
        int page = 0;
        DateTime today = DateTime.Now;
        double lastMonthLeft = 0.0;
        double twoMonthsLeft = 0.0;
        double threeMonthsLeft = 0.0;
        double thisMonthLeft = 0.0;
        double fourAboveLeft = 0.0;
        string handleMan = "";
        DateTime startPick = StartTimePicker.SelectionRange.Start;
        DateTime endPick = EndTimePicker.SelectionRange.Start;
        DateTime beginning = new DateTime(startPick.Year, startPick.Month, startPick.Day, 00, 00, 01);
        DateTime ending = new DateTime(endPick.Year, endPick.Month, endPick.Day, 23, 59, 59);
        string path = "C:\\Users\\Benjiw\\Desktop\\test.pdf";
        StringBuilder sb = new StringBuilder();
        sb.Append("<html><body><table border=\"1\" width=\"100%\" cellspacing=\"0\"> <tr><td></td><td colspan=\"3\" align=\"center\" font size=\"5\">ccc</td><td></td></tr>");
        sb.Append("<tr><td></td><td colspan=\"3\" align=\"center\" >bbb</td><td></td></tr>");
        sb.Append("<tr><td></td><td colspan=\"3\" align=\"center\" >aaa</td><td></td></tr>");
        sb.Append("<tr><td></td><td colspan=\"3\" align=\"center\" >Rddd</td><td></td></tr>");
        sb.Append("<tr><td></td><td>TEL: 123123</td><td></td><td>FAX: 32321</td></tr><tr><td></td><td></td><td></td>  <td>   </td><td></td></tr>");
        sb.Append("</table>");

        sb.Append("<table border=\"1\" width=\"100%\" cellspacing=\"0\">");
        sb.Append("<tr><td>致   :</td><td>" + customer + "</td><td></td><td>日期         :</td><td>" + today.ToString("MMMM dd yyyy") + "</td></tr>");
        sb.Append("<tr><td>地址   :</td><td colspan=\"2\">" + address + "</td><td>發票號碼         :</td><td>" + today.ToString("MMMM dd yyyy") + "</td></tr>");
        sb.Append("<tr><td></td><td align=\"center\" colspan=\"3\">INVOICE</td><td></td></tr>");
        sb.Append("<tr><td></td><td align=\"center\" colspan=\"3\">發票</td><td></td></tr>");


        sb.Append("</table>");
        sb.Append("<table border=\"1\" width=\"100%\" cellspacing=\"0\">");
        sb.Append("<tr><td colspan=\"7\">======================================================================================</td></tr>");
        sb.Append("<tr><td style=\"width:5px;\">單號</td><td width=\"5%\">日期</td><td width=\"5%\">貨品</td><td width=\"5%\" colspan=\"2\">數量</td><td width=\"5%\">單價(HK$)</td><td width=\"75%\">金額(HK$)</td></tr>");
        sb.Append("<tr><td colspan=\"7\">======================================================================================</td></tr>");



        sb.Append("<tr><td colspan=\"7\"><br><br><br>1<\td><\tr>");  <---------- this is where i wanna insert some white space
        sb.Append("</table>");

        sb.Append("</body></html>");
        iTextSharp.text.FontFactory.Register(Environment.GetEnvironmentVariable("windir") + "\\Fonts\\KAIU.TTF", "KAIU");
        iTextSharp.text.html.simpleparser.StyleSheet style = new iTextSharp.text.html.simpleparser.StyleSheet();

        style.LoadTagStyle("body", "face", "KAIU");
        style.LoadTagStyle("body", "encoding", "Identity-H");
        style.LoadTagStyle("body", "leading", "12,0");


        style.LoadTagStyle("td", "face", "KAIU");
        style.LoadTagStyle("td", "encoding", "Identity-H");
        style.LoadTagStyle("td", "leading", "18,0");

        iTextSharp.text.Document document = new iTextSharp.text.Document();
        document.SetPageSize(iTextSharp.text.PageSize.A4);

        iTextSharp.text.pdf.PdfWriter.GetInstance(document, (new FileStream(path, FileMode.Create)));
        document.Open();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document, null, style);
        hw.Parse(new StringReader(sb.ToString()));

        //document.NewPage();
        //hw.Parse(new StringReader(htmlText));

        document.Close();

我尝试添加
但它与一个数据行的高度不同,请让我知道如何完成这项操作并设置列的宽度 谢谢

1 个答案:

答案 0 :(得分:0)

请查看ParseHtmlTable8示例。对于这个例子,我稍微调整了你的HTML(因为它不完全正确):table5_css.html(注意它仍然是一个尴尬的文档)。

当我使用我在评论中引用您的示例将其转换为PDF时,我得到此PDF:html_table_8.pdf

enter image description here

如您所见,宽度,高度和字体看起来还不错。

这是我的代码:

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
    // CSS
    CSSResolver cssResolver = new StyleAttrCSSResolver();

    // HTML
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontProvider.register("resources/fonts/NotoSansCJKsc-Regular.otf");
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    // Pipelines
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));;
    // step 5
    document.close();
}

请注意,我使用NotoSansCJKsc-Regular.otf作为字体。这是Google根据Apache软件许可证2.0分发的字体。

显然,我表示我想在我的HTML文件中使用这种字体:

<body style="font-family: NotoSansCJKsc-Regular">