Warm tip: This article is reproduced from serverfault.com, please click

其他-使用iText 7和Java生成pdf不能包装长英文单词

(其他 - Using iText 7 and Java generating pdf can not wrap long English words)

发布于 2020-04-25 19:27:41

使用iText 7和Java生成PDF不能包装长英语单词。

pdf的屏幕截图

当一个单元格中有一个长字时,该字并没有在单元格中换行,而是在增长,并且PDF中的表内容丢失。不知道如何将长字包装在单元格中。

我正在使用iText 7生成PDF。

这是我的Java文件:

package com.sid.pdf;

import java.io.File;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.splitting.DefaultSplitCharacters;

public class TestTable {

    public static void main(String[] args) throws Exception

    {
        public static final String DEST = "C:\\test.pdf";
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TestTable().manipulatePdf(DEST);
        System.out.println("PDF generated....");
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Table table = new Table(3);
        float tableWidth = doc.getPdfDocument().getDefaultPageSize().getWidth()
                - (doc.getLeftMargin() + doc.getRightMargin());
        table.setWidth(tableWidth);

        Cell cell1 = new Cell();
        Paragraph p = new Paragraph("1");
        p.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
        cell1.add(p);
        table.addCell(cell1);

        Cell cell2 = new Cell();
        Paragraph p2 = new Paragraph("CamLane_Disp_Warn_Rq_Pr2_e0h2tjvjx5d9y5cbvxqsnhwa7");
        p2.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
        cell2.add(p2);
        table.addCell(cell2);

        Cell cell3 = new Cell();
        Paragraph p3 = new Paragraph("CamLane_Disp_Warn_Rq_AR2");
        p3.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
        cell3.add(p3);
        table.addCell(cell3);

        Cell cell4 = new Cell();
        Paragraph p4 = new Paragraph("SQC/CRC");
        p4.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
        cell4.add(p4);
        table.addCell(cell4);

        Cell cell5 = new Cell();
        Paragraph p5 = new Paragraph("SPV_EngRq1_VAN_Pr2_vx0c4n6d46wgrav5gmco6bvc");
        p5.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
        cell5.add(p5);
        table.addCell(cell5);

        Cell cell6 = new Cell();
        Paragraph p6 = new Paragraph("Bckl_Sw_Ft_Stat_Pr2_b14xqvpzjykdbhltdyma53upe");
        p6.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
        cell6.add(p6);
        table.addCell(cell6);
        doc.add(table);
        doc.close();
    }
}
Questioner
Srikant
Viewed
0
Alexey Subach 2020-04-27 06:14:26

默认的拆分策略是查找空格字符和文本通常在其中拆分的其他字符(例如,连字符-)。在你的情况下,单词没有此类字符。通过定义SPLIT_CHARACTERS属性,你已经为定制文本的拆分字符迈出了半步,但是缺少的部分是进行自定义ISplitCharacters实现。还允许下划线(_)作为分割字符的示例实现

private static class CustomSplitCharacters extends DefaultSplitCharacters {
    @Override
    public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
        if (!text.get(glyphPos).hasValidUnicode()) {
            return false;
        }
        boolean baseResult = super.isSplitCharacter(text, glyphPos);
        boolean myResult = false;
        Glyph glyph = text.get(glyphPos);
        if (glyph.getUnicode() == '_') {
            myResult = true;
        }
        return myResult || baseResult;
    }
}

要启用它,只需将新实例而不是默认实例设置为该SPLIT_CHARACTERS属性:

p6.setProperty(Property.SPLIT_CHARACTERS, new CustomSplitCharacters());

视觉效果:

结果