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

使用iText 7和Java生成pdf不能包装长英文单词,java,itext,itext7,Java,Itext,Itext7,使用iText 7和Java生成PDF不能包装长的英语单词 当单元格中有一个长单词时,该单词不会在单元格中包装,而是在增长,并且PDF中缺少表格内容。不知道如何在单元格中包装长单词 我正在使用iText 7生成PDF 这是我的Java文件: package com.sid.pdf; 导入java.io.File; 导入com.itextpdf.kernel.pdf.PdfDocument; 导入com.itextpdf.kernel.pdf.PdfWriter; 导入com.itextpdf.

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

当单元格中有一个长单词时,该单词不会在单元格中包装,而是在增长,并且PDF中缺少表格内容。不知道如何在单元格中包装长单词

我正在使用iText 7生成PDF

这是我的Java文件:

package com.sid.pdf;
导入java.io.File;
导入com.itextpdf.kernel.pdf.PdfDocument;
导入com.itextpdf.kernel.pdf.PdfWriter;
导入com.itextpdf.layout.Document;
导入com.itextpdf.layout.element.Cell;
导入com.itextpdf.layout.element.paragration;
导入com.itextpdf.layout.element.Table;
导入com.itextpdf.layout.property.property;
导入com.itextpdf.layout.spliting.DefaultSplitCharacters;
公共类测试表{
公共静态void main(字符串[]args)引发异常
{
公共静态最终字符串DEST=“C:\\test.pdf”;
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
新的TestTable().操纵EPDF(DEST);
System.out.println(“生成的PDF…”);
}
受保护的无效操作EPDF(字符串目标)引发异常{
PdfDocument pdfDoc=新PdfDocument(新PdfWriter(dest));
单据单据=新单据(pdfDoc);
表=新表(3);
float tableWidth=doc.getPdfDocument().getDefaultPageSize().getWidth()
-(doc.getLeftMargin()+doc.getRightMargin());
table.setWidth(tableWidth);
Cell cell1=新的Cell();
第p段=新的第(1)段;
p、 setProperty(Property.SPLIT_CHARACTERS,新的DefaultSplitCharacters());
单元格1.添加(p);
表1.addCell(cell1);
Cell cell2=新的Cell();
p2段=新段(“CamLane_Disp_Warn_Rq_Pr2_e0h2tjvjx5d9y5cbvxqsnhwa7”);
p2.setProperty(Property.SPLIT_字符,新的DefaultSplitCharacters());
单元格2.添加(p2);
表1.addCell(第2单元);
Cell cell3=新的Cell();
p3段=新段(“CamLane_Disp_Warn_Rq_AR2”);
p3.setProperty(Property.SPLIT_字符,新的DefaultSplitCharacters());
第3单元添加(p3);
表1.addCell(第3单元);
Cell cell4=新的Cell();
第p4段=新的段落(“SQC/CRC”);
p4.setProperty(Property.SPLIT_CHARACTERS,新的DefaultSplitCharacters());
第4单元添加(p4);
表1.addCell(第4单元);
单元格5=新单元格();
第p5段=新的一段(“特殊目的公司工程Q1_VAN_Pr2_vx0c4n6d46wgrav5gmco6bvc”);
p5.setProperty(Property.SPLIT_字符,新的DefaultSplitCharacters());
第5单元添加(p5);
表1.addCell(第5单元);
单元格6=新单元格();
p6段=新段(“Bckl_Sw_Ft_Stat_Pr2_b14xqvpzjykdbhltdyma53upe”);
p6.setProperty(Property.SPLIT_CHARACTERS,新的DefaultSplitCharacters());
第6单元添加(p6);
表1.addCell(第6单元);
单据新增(表);
doc.close();
}
}

默认拆分策略是查找空格字符和文本通常拆分的其他字符(例如连字符
-
)。在你的情况下,单词没有这样的字符。通过定义
split_characters
属性,您已经朝着为文本自定义分割字符迈出了半步,但缺少的部分是使自定义
ISplitCharacters
实现。还允许下划线(
\uu
)作为拆分字符的示例实现:

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\u CHARACTERS
属性:

p6.setProperty(Property.SPLIT_CHARACTERS, new CustomSplitCharacters());
视觉效果:


默认拆分策略是查找空格字符和文本通常拆分的其他字符(例如连字符
-
)。在你的情况下,单词没有这样的字符。通过定义
split_characters
属性,您已经朝着为文本自定义分割字符迈出了半步,但缺少的部分是使自定义
ISplitCharacters
实现。还允许下划线(
\uu
)作为拆分字符的示例实现:

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\u CHARACTERS
属性:

p6.setProperty(Property.SPLIT_CHARACTERS, new CustomSplitCharacters());
视觉效果:


我尝试了以下代码,并为我工作。我已经创建了一个自定义拆分 角色类

公共类SpecificSplitCharacters扩展了DefaultSplitCharacters{
@凌驾
公共布尔值isSplitCharacter(GlyphLine文本,int-glyphPos){
如果(!text.get(glyphPos).hasValidUnicode()){
返回false;
}
Glyph Glyph=text.get(glyphPos);
if(glyph.getUnicode()==''\u'){
返回true;
}否则{
返回super.isSplitCharacter(文本,glyphPos);
}
}
}
最后,我们可以将自定义属性设置为document对象,如下所示:

doc.setProperty(Property.SPLIT_CHARACTERS, new SpecificSplitCharacters ());
(我们也可以为每个段落设置属性)


我正在尝试下面的代码,并为我工作。我已经创建了一个自定义拆分 角色类

公共类SpecificSplitCharacters扩展了DefaultSplitCharacters{
@凌驾
公共布尔值isSplitCharacter(GlyphLine文本,int-glyphPos){
如果(!text.get(glyphPos).hasValidUnicode()){
返回false;
}
Glyph Glyph=text.get(glyphPos);
if(glyph.getUnicode()==''\u'){
返回true;
}否则{
返回super.isSplitCharacter(文本,glyphPos);
}
}
}
最后,我们可以将自定义属性设置为document对象,如下所示:

doc.setProperty(Property.SPLIT_CHARACTERS, new SpecificSplitCharacters ());
(我们也可以为每个段落设置属性)


我是winform c#上的build and pdf viewer,在单元构建函数中添加以下代码:SetMaxWidth

    Paragraph p = new Paragraph(val != null ? val : "");
    ...
    ...
    p.SetFontSize(fontsize);
                p.SetMaxWidth(height - 1000);
                res.Add(p);
    ...
    ...
得到这个结果


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

    Paragraph p = new Paragraph(val != null ? val : "");
    ...
    ...
    p.SetFontSize(fontsize);
                p.SetMaxWidth(height - 1000);
                res.Add(p);
    ...
    ...
得到这个结果


那些不是很长的英语单词,是吗?那些不是很长的英语单词