java 畫pdf用itext調(diào)整表格寬度、自定義各個(gè)列寬的方法
ps:我用的版本是7.0.5
場(chǎng)景:左側(cè)第一列寬度不夠,導(dǎo)致數(shù)據(jù)換行。
Table table = new Table(new float[2]);
new 一個(gè)Table之后,setWidthPercent()這個(gè)參數(shù)是這是所有列寬,并不能試用個(gè)別列。
需要在寫入數(shù)據(jù)的時(shí)候?qū)Ω鱾€(gè)列進(jìn)行自定義列寬:
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中設(shè)置寬度,不要再table上設(shè)置寬度。
即可解決個(gè)別列寬問(wèn)題。
調(diào)整后的效果:補(bǔ)充:java通過(guò)itext生成PDF,設(shè)置單元格cell的最大高度 以及 itext7初嘗
網(wǎng)上百度java生成pdf都是很老的代碼,使用的是itext5,找遍了大江南北都找不到設(shè)置表格或單元格最大高度,或者絕對(duì)定位表格的實(shí)現(xiàn),最后對(duì)table和cell的方法一個(gè)一個(gè)找,找到了滿足要求的方法:
cell.setMaxLines(int numberOfLines)
由于字體確定,每行字體的高度已確定,設(shè)定最大行數(shù)也就設(shè)定了最大高度,且避免了設(shè)置的高度不是每行高度的整數(shù)倍的麻煩,itext的這個(gè)操作也挺6,只是不符合一般認(rèn)知,無(wú)法輕易找到這個(gè)方法。
雖然cell最大高度解決了,但是表格的絕對(duì)定位依然沒有解決,itext5只能通過(guò)百分比的方式設(shè)置表格寬度,然后居中或靠左靠右顯示,非常不靈活。
經(jīng)查詢,itext7是目前最新版,試用了一下,非常靈活,該解決的問(wèn)題都解決了。用法與5有稍許區(qū)別。
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); // 支持系統(tǒng)字體(支持中文) 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);//有傳頁(yè)數(shù)參數(shù)的方法 // 表格 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(); } } }
所以以后做功能,百度大法雖好,也要有自己的靈活性,遇到問(wèn)題多角度解決,技術(shù)在更新,思路也要更新
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享2. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器3. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. 如何在jsp界面中插入圖片6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. 詳解瀏覽器的緩存機(jī)制8. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令9. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程10. phpstudy apache開啟ssi使用詳解
