【问题标题】:Using iText 7 and Java generating pdf can not wrap long English words使用 iText 7 和 Java 生成 pdf 不能换行长英文单词
【发布时间】:2020-08-09 09:13:15
【问题描述】:

使用 iText 7 和 Java 生成 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();
    }
}

【问题讨论】:

  • 那些不是很长的英文单词吧?

标签: java itext itext7


【解决方案1】:

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

视觉效果:

【讨论】:

  • 非常感谢您的解决方案。我之前也得到了相同的解决方案,并且即将在答案部分发布。
  • @Srikant 如果您得到“相同的解决方案”,则无需将其发布在另一个答案中,您可以简单地接受 Alexey 的答案。
【解决方案2】:

我正在尝试使用以下代码并为我工作。我创建了一个自定义拆分 字符类。

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

最后我们可以为文档对象设置自定义属性,如下所示:

doc.setProperty(Property.SPLIT_CHARACTERS, new SpecificSplitCharacters ());

(我们也可以为每个段落设置属性)

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

【讨论】:

    【解决方案3】:

    我在winform c#上构建和pdf查看器,在单元格构建函数中添加以下代码:SetMaxWidth

        Paragraph p = new Paragraph(val != null ? val : "");
        ...
        ...
        p.SetFontSize(fontsize);
                    p.SetMaxWidth(height - 1000);
                    res.Add(p);
        ...
        ...
    

    得到这个结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2018-07-09
      • 2020-12-11
      • 2022-07-29
      • 1970-01-01
      • 2016-09-02
      • 2015-10-13
      相关资源
      最近更新 更多