【问题标题】:Justify text inside PdfPTable cell using iText XMLWorker使用 iText XMLWorker 在 PdfPTable 单元格内对齐文本
【发布时间】:2017-03-24 07:21:28
【问题描述】:

我正在尝试使用以下代码证明 PdfPTable 单元格内的文本:

                    PdfPTable pTable = new PdfPTable(1);
                    pTable.setWidthPercentage(98f);
                    pTable.setSpacingBefore(10f);
                    pTable.setHorizontalAlignment(Element.ALIGN_JUSTIFIED_ALL);

                    String content=getContent();

                    list =XMLWorkerHelper.parseToElementList(content);
                    for (Element element : list) {
                        cell = new PdfPCell();
                        cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED_ALL);
                        cell.setVerticalAlignment(PdfPCell.ALIGN_JUSTIFIED_ALL);
                        cell.setNoWrap(false);
                        cell.setBorderWidth(0);
                        cell.addElement(element);
                        pTable.addCell(cell);
                    }
                    pTable.setSplitLate(false);
                    paragraph.add(pTable);

但是文本是左对齐的。我成功地使用了Paragraph 对象而不是表格,但我需要在表格中显示文本。

【问题讨论】:

    标签: java alignment itext xmlworker text-justify


    【解决方案1】:

    .NET,但很容易转换为 Java...

    由于您正在解析 [X]HTML 并使用 XMLWorkerHelper,因此证明文本对齐的简单方法是使用 overloaded parseXHtml method 并自己设置 CSS 样式。示例 HTML:

    <html><head>
    <title>Test HTML</title>
    <style type='text/css'>
    td { 
        border:1px solid #eaeaea; 
        padding: 4px;
        text-align: right; 
        font-size: 1.4em; 
    }
    </style>
    </head><body>
    <table width='50%'><tr>
    <td>
    but I can not see justification of text. 
    I tried using only paragraph without using table, 
    and it does justification but I need to display 
    things in table
    </td></tr></table>
    </body></html>
    

    解析代码:

    // CSS specificity selector: apply style below without changing existing styles
    string css = "tr td { text-align: justify; }";
    
    using (var memoryStream = new MemoryStream())
    {
        using (var document = new Document())
        {
            PdfWriter writer = PdfWriter.GetInstance(
                document, memoryStream
            );
            document.Open();
            using (var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
            {
                using (var cssStream = new MemoryStream(Encoding.UTF8.GetBytes(css)))
                {
                    XMLWorkerHelper.GetInstance().ParseXHtml(
                        writer, document, htmlStream, cssStream
                    );
                }
            }
        }
        File.WriteAllBytes(OUTPUT, memoryStream.ToArray());
    }
    

    注意XMLWorkerHelper 足够聪明,可以在调用ParseXHtml() 时正确应用CSS Specificity 样式:

    1. 维护&lt;style&gt;中的指定样式。
    2. 将文本对齐方式从 right 更改为 justify

    输出PDF:

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 2014-08-07
      • 2014-11-08
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多