Wrapping justified text around a table using iText7

时间:2018-02-03 08:41:37

标签: java itext7

I try to wrap a text around a table. If the text alignment is LEFT than everything is ok but when I use the text alignment JUSTIFIED the output looks ugly.

try(PdfWriter writer = new PdfWriter("Justified.pdf"))
{
    try(PdfDocument pdfDocument = new PdfDocument(writer))
    {
        pdfDocument.setDefaultPageSize(PageSize.A5);
        try(Document document = new Document(pdfDocument))
        {
            String text = "Lorem ipsum dolor sit amet, ...";
            String text2 = "Lorem ipsum dolor sit amet, ...";

            Table table2 = new Table(1)
                    .setWidth(150f)
                    .setBorder(new SolidBorder(1f))
                    .setMargin(5f)
                    .setHorizontalAlignment(HorizontalAlignment.LEFT)
                    .addCell(new Cell()
                            .add(new Paragraph(text2)));
            table2.setProperty(Property.FLOAT, FloatPropertyValue.LEFT);
            document.add(table2);
            document.add(new Paragraph(text)
                    .setTextAlignment(TextAlignment.JUSTIFIED));
            Table table3 = new Table(1)
                    .setWidth(150f)
                    .setBorder(new SolidBorder(1f))
                    .setMargin(5f)
                    .setHorizontalAlignment(HorizontalAlignment.RIGHT)
                    .addCell(new Cell()
                            .add(new Paragraph(text2)));
            table3.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
            document.add(table3);
            document.add(new Paragraph(text)
                    .setTextAlignment(TextAlignment.JUSTIFIED));
        }
    }
}
catch(Exception ex){}

Text alignment JUSTIFIED:

enter image description here

Text Alignment LEFT:

enter image description here

1 个答案:

答案 0 :(得分:2)

自7.1.1版以来,此特定错误已在iText中修复。在iText 7.1.2中将引入一些用于对齐浮动元素周围文本的附加修复(如内联浮动块和第一行文本缩进)。