java - 使用iText 7和Java生成pdf无法换行长英文单词

标签 java itext itext7

使用iText 7和Java生成PDF无法换行长英文单词。

screenshot of 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();
    }
}

最佳答案

默认分割策略是查找空格字符和文本通常分割的其他字符(例如连字符 -)。在你的情况下,单词没有这样的字符。通过定义 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());

视觉效果:

result

关于java - 使用iText 7和Java生成pdf无法换行长英文单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61431370/

相关文章:

java - Jackson:无法使用嵌套 DTO 处理托管/反向引用 'defaultReference'

java - 使用 iText7 将对齐的文本环绕表格

java - 如何使用 iText List 将具有不同权重的文本添加到单个 ListItem

java - 打开附件面板的操作

java - Canvas 错误地绘制表 itext7 中的高度单元格

java - 如何在ubuntu中从java程序运行c可执行文件

java - 如何排序和保留 web 服务的结果?

java - Web 服务中每个用户的全局变量

java - 如何使用 iText 保存 .pdf 文件?

c# - 使用 iTextSharp 为文本部分加下划线