您的位置:首页技术文章
文章详情页

java 画pdf用itext调整表格宽度、自定义各个列宽的方法

【字号: 日期:2022-08-17 14:39:32浏览:6作者:猪猪

ps:我用的版本是7.0.5

场景:

左侧第一列宽度不够,导致数据换行。

java 画pdf用itext调整表格宽度、自定义各个列宽的方法

Table table = new Table(new float[2]);

new 一个Table之后,setWidthPercent()这个参数是这是所有列宽,并不能试用个别列。

需要在写入数据的时候对各个列进行自定义列宽:

Cell cell=new Cell().setWidth(70).setBorder(Border.NO_BORDER).setHorizontalAlignment(HorizontalAlignment.RIGHT).add(new Paragraph(entry.getKey()).setFont(sysFont).setFontSize(10));Cell cell1=new Cell().setBorder(Border.NO_BORDER).setHorizontalAlignment(HorizontalAlignment.LEFT).add(new Paragraph(entry.getValue()).setFont(sysFont).setFontSize(10));

cell为第一列,cell1为第二列,在cell中设置宽度,不要再table上设置宽度。

即可解决个别列宽问题。

调整后的效果:

java 画pdf用itext调整表格宽度、自定义各个列宽的方法

补充: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;import com.itextpdf.layout.Document;import com.itextpdf.layout.border.DashedBorder;import com.itextpdf.layout.element.Cell;import com.itextpdf.layout.element.Link;import com.itextpdf.layout.element.Paragraph;import com.itextpdf.layout.element.Table; /** * @author belle.wang * @version V1.0.0 * @Description * @date 2017/7/19 0019 上午 11:37 */public class Main { public static void main(String[] args) { try { PdfDocument pdfDoc = new PdfDocument(new PdfWriter('d:Helloworld.pdf')); Document document = new Document(pdfDoc, PageSize.A4); // 支持系统字体(支持中文) PdfFontFactory.registerSystemDirectories(); PdfFont chinese = PdfFontFactory.createRegisteredFont('microsoft yahei', PdfEncodings.IDENTITY_H); // 文字 Paragraph phrase = new Paragraph(); phrase.setFont(chinese); phrase.add('雷猴啊'); Link chunk = new Link('European Business Award!', PdfAction.createURI('http://www.baidu.com')); phrase.add(chunk); // 图片// Image img = new Image(ImageDataFactory.create('src/main/resources/img/magic.png'));// img.setFixedPosition(80, 560);//有传页数参数的方法 // 表格 Table table = new Table(new float[]{200f, 100f}); table.setWidth(300); table.setBorder(new DashedBorder(Color.BLUE, 2)); table.setFixedPosition(300f,300f,300f); table.addCell(phrase); // The complete cell is a link: Cell cell = new Cell().add('Help us win a European Business Award!'); table.addCell(cell); document.add(table); document.close(); } catch (Exception e) { e.printStackTrace(); } } }

所以以后做功能,百度大法虽好,也要有自己的灵活性,遇到问题多角度解决,技术在更新,思路也要更新

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Java
相关文章: