td border,border-bottom不使用itext库以pdf格式打印

时间:2016-12-27 19:40:52

标签: java html itext

<tr><td style='border-bottom:1px solid #333;'><p style='font-family:arial; font-size:8px; margin:0px 0px 10px 0px;  line-height:10px;'><strong>Customer Details </strong><br>Lawakush Kurmi<br>8285998390<br>lkurmi@craterzone.com<br> </p></td><td valign='top' style='font-family:arial; font-size:8px;  text-align:right; border-bottom:1px solid #333;'>&nbsp;</td></tr>

我试图通过使用i-text库转换这些HTML格式的HTML代码。但是border-bottom:1px没有在pdf中绘制任何边框。请建议使用html在pdf中绘制水平线边框的最佳选项是什么 注意:我正在使用HTMLWorker将HTML转换为PDF页面。

1 个答案:

答案 0 :(得分:1)

请允许我重复使用iTextSharp add ( css style or a css file) and download pdf file中的代码并稍微更改一些值:

public static final String CSS = "th { border-top: 5px solid green; } "
    + "td { font-size: 10pt; border-color: gray; border: 3px}";
public static final String HTML = "<html><body><table  class='table-bordered'>"
    + "<thead><tr><th>Customer Name</th><th>Customer's Address</th> </tr></thead>"
    + "<tbody><tr><td> XYZ </td><td> Bhubaneswar </td></tr>"
    + "<tr><td> MNP </td><td> Cuttack </td></tr></tbody>"
    + "</table></body></html>";

/**
 * @param file
 * @throws IOException
 * @throws DocumentException
 */
public void createPdf(String file) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    document.open();


    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    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 ByteArrayInputStream(HTML.getBytes()));
    document.close();
}

生成的PDF如下所示:

enter image description here

如果您查看CSS,就会发现我们已将<th>代码的边框定义为border-top: 5px solid green;,将<td>代码的边框定义为{{1} }}。这证明了iText支持表格边框的CSS。