java通过itext生成PDF,设置单元格cell的最大高度 以及 itext7初尝

网上百度java生成pdf都是很老的代码,使用的是itext5,找遍了大江南北都找不到设置表格或单元格最大高度,或者绝对定位表格的实现,最后对table和cell的方法一个一个找,找到了满足要求的方法:

cell.setMaxLines(int numberOfLines)

由于字体确定,每行字体的高度已确定,设定最大行数也就设定了最大高度,且避免了设置的高度不是每行高度的整数倍的麻烦,itext的这个操作也挺6,只是不符合一般认知,无法轻易找到这个方法。


虽然cell最大高度解决了,但是表格的绝对定位依然没有解决,itext5只能通过百分比的方式设置表格宽度,然后居中或靠左靠右显示,非常不灵活。

经查询,itext7是目前最新版,试用了一下,非常灵活,该解决的问题都解决了。用法与5有稍许区别。

iText7示例

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
i
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `PdfPCellEvent` 接口来实现斜线单元格的效果。这个接口提供了一个方法 `cellLayout()`,可以在单元格绘制时添加自定义内容。 下面是一个示例代码,实现了斜线单元格的效果: ``` import com.itextpdf.awt.geom.AffineTransform; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.FileOutputStream; public class SlantedCell implements PdfPCellEvent { private String text; public SlantedCell(String text) { this.text = text; } @Override public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; float x1 = position.getLeft() + cell.getPaddingLeft(); float y1 = position.getTop() - cell.getPaddingTop(); float x2 = position.getRight() - cell.getPaddingRight(); float y2 = position.getBottom() + cell.getPaddingBottom(); canvas.saveState(); canvas.beginText(); canvas.setColorFill(BaseColor.BLACK); canvas.setFontAndSize(BaseFont.createFont(), 12); canvas.setTextMatrix(new AffineTransform(1, 0, 0.2f, 1, 0, 0)); canvas.showTextAligned(Element.ALIGN_LEFT, text, x1, y1, 0); canvas.endText(); canvas.restoreState(); canvas.saveState(); canvas.beginText(); canvas.setColorFill(BaseColor.BLACK); canvas.setFontAndSize(BaseFont.createFont(), 12); canvas.setTextMatrix(new AffineTransform(1, 0, -0.2f, 1, 0, 0)); canvas.showTextAligned(Element.ALIGN_LEFT, text, x2, y2, 0); canvas.endText(); canvas.restoreState(); } public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("slanted-cell.pdf")); document.open(); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); table.getDefaultCell().setPadding(5); PdfPCell cell1 = new PdfPCell(new Phrase("斜线单元格")); cell1.setCellEvent(new SlantedCell("斜线单元格")); table.addCell(cell1); table.addCell(new PdfPCell(new Phrase("普通单元格"))); document.add(table); document.close(); } } ``` 这个示例代码创建了一个 `PdfPCellEvent` 接口的实现类 `SlantedCell`,并在 `cellLayout()` 方法中绘制了两条斜线。然后将这个实现类的实例设置给需要添加斜线的单元格的 `cellEvent` 属性即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值