Java使用iText设置表格边框

时间:2017-06-26 14:17:02

标签: java html itext

我正在从html构建pdf,我想设置表格边框。 这是我的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Digital </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="font-size:12.0pt; font-family:Arial">

    <table style="width:100%" border=\"2\">
        <tr>
            <th>depo date</th>
            <th>MMR</th>
            <th>SUM</th>
            <th>NUM</th>
            <th>ACCOUNT</th>
            <th>TNUM</th>
        </tr>
        <tr>
            <td dir="rtl">$data.depo</td>
            <td dir="rtl">$data.MR</td>
            <td dir="rtl">$data.Sum</td>
            <td dir="rtl">$data.Num</td>
            <td dir="rtl">$data.accoun</td>
            <td dir="rtl">$data.branch</td> 
            <td dir="rtl">$data.TNum</td>
        </tr>

    </table>
</body>
</html>

关注iText-add-table-to-pdf,唯一的不同之处是将border=\"2\"添加到表中,但它仍然不起作用,我需要添加更多更改吗?

创建pdf函数(与教程中的相同):

public void createPdf(String file, String htmlString, String pathToTTF) 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
        // Styles
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        XMLWorkerFontProvider fontProvider =
            new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); //
        fontProvider.register(pathToTTF);
        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(htmlString), Charset.forName("UTF-8"));
        // step 5
        document.close();
    }

1 个答案:

答案 0 :(得分:1)

将html转换为pdf时请考虑使用pdfHtml pdfHtml是我们的插件部分中执行此任务的最新工具,并且在CSS和HTML构造方面具有更大的范围。