itextpdf中PdfPCell内中文后的数字或字母自动换行问题处理

	又是好久没写博客了,今天来记录一下心得。话不多说,上才艺。
	使用场景:导出的pdf中表格内的单元格数据出现数字自动换行。
	![在这里插入图片描述](https://img-blog.csdnimg.cn/fe4efe534cab47c8862ebbba7a95d2d4.png)

处理方案:原先的PdfPCell赋值改成包装一层Chunk赋值,使用Chunk的setSplitCharacter方法。这里是否进行语句的分割方法,直接返回false。

public static PdfPCell productCellTwo(String value, Font font){
        Chunk chunk = new Chunk(value,font);
        chunk.setSplitCharacter(new OwnerSplitCharacter());
        PdfPCell cell = new PdfPCell();
        cell.addElement(chunk);
        // 水平居中
        // cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        // 垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        // 动态高度
        if(StringUtils.isNotEmpty(value)){
            if(value.length() <= 72){
                cell.setFixedHeight(20);
            } else {
                int height = (value.length()/36 + 1) * 10;
                cell.setFixedHeight(height);
            }
        }
        return cell;
    }

    private static class OwnerSplitCharacter implements SplitCharacter {
        @Override
        public boolean isSplitCharacter(int i, int i1, int i2, char[] chars, PdfChunk[] pdfChunks) {
            return false;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用iTextPDF的`PdfPCell`类来添加图片。下面是一个示例代码,演示如何在`PdfPCell`添加图片: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); PdfPTable table = new PdfPTable(1); // 创建一个 PdfPCell PdfPCell cell = new PdfPCell(); // 读取图片 Image image = Image.getInstance("path/to/image.jpg"); // 设置图片宽度和高度 image.scaleAbsolute(200, 200); // 将图片添加到 PdfPCell cell.addElement(image); // 将 PdfPCell 添加到表格 table.addCell(cell); // 将表格添加到文档 document.add(table); document.close(); } catch (DocumentException | IOException e) { e.printStackTrace(); } } } ``` 在上面的示例代码,我们首先创建了一个`PdfPTable`对象,然后创建一个`PdfPCell`对象。然后,我们使用`Image.getInstance()`方法读取图片文件,并使用`scaleAbsolute()`方法设置图片的宽度和高度。 最后,我们将图片添加到`PdfPCell`,再将`PdfPCell`添加到表格,最终将表格添加到文档。请确保将`"path/to/image.jpg"`替换为您实际的图片路径。运行代码后,将会生成一个名为`output.pdf`的PDF文件,其包含了添加了图片的单元格。 请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值