java 畫pdf用itext調(diào)整表格寬度、自定義各個列寬的方法
ps:我用的版本是7.0.5
場景:
左側第一列寬度不夠,導致數(shù)據(jù)換行。

Table table = new Table(new float[2]);
new 一個Table之后,setWidthPercent()這個參數(shù)是這是所有列寬,并不能試用個別列。
需要在寫入數(shù)據(jù)的時候?qū)Ω鱾€列進行自定義列寬:
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上設置寬度。
即可解決個別列寬問題。
調(diào)整后的效果:

補充:java通過itext生成PDF,設置單元格cell的最大高度 以及 itext7初嘗
網(wǎng)上百度java生成pdf都是很老的代碼,使用的是itext5,找遍了大江南北都找不到設置表格或單元格最大高度,或者絕對定位表格的實現(xiàn),最后對table和cell的方法一個一個找,找到了滿足要求的方法:
cell.setMaxLines(int numberOfLines)
由于字體確定,每行字體的高度已確定,設定最大行數(shù)也就設定了最大高度,且避免了設置的高度不是每行高度的整數(shù)倍的麻煩,itext的這個操作也挺6,只是不符合一般認知,無法輕易找到這個方法。
雖然cell最大高度解決了,但是表格的絕對定位依然沒有解決,itext5只能通過百分比的方式設置表格寬度,然后居中或靠左靠右顯示,非常不靈活。
經(jīng)查詢,itext7是目前最新版,試用了一下,非常靈活,該解決的問題都解決了。用法與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);//有傳頁數(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();
}
}
}
所以以后做功能,百度大法雖好,也要有自己的靈活性,遇到問題多角度解決,技術在更新,思路也要更新
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- Java 使用openoffice進行word轉換為pdf的方法步驟
- Java使用iTextPDF生成PDF文件的實現(xiàn)方法
- Java 實現(xiàn)word模板轉為pdf
- java 后端生成pdf模板合并單元格表格的案例
- java 用itext設置pdf紙張大小操作
- Java生成pdf文件或jpg圖片的案例講解
- 用Java驗證pdf文件的電子章簽名
- 基于Java SWFTools實現(xiàn)把pdf轉成swf
- Java實現(xiàn)Word/Pdf/TXT轉html的示例
- Java pdf和jpg互轉案例
- Java實現(xiàn)圖片轉換PDF文件的示例代碼
- Java 基于Spire.Cloud.SDK for Java在PDF中繪制形狀
- Java 在PDF中添加騎縫章示例解析
- Java 在PDF中繪制形狀的兩種方法
- 教你怎么用Java通過關鍵字修改pdf
相關文章
Java語言之LinkedList和鏈表的實現(xiàn)方法
LinkedList是由傳統(tǒng)的鏈表數(shù)據(jù)結構演變而來的,鏈表是一種基本的數(shù)據(jù)結構,它可以動態(tài)地增加或刪除元素,下面這篇文章主要給大家介紹了關于Java語言之LinkedList和鏈表的實現(xiàn)方法,需要的朋友可以參考下2023-05-05
一文詳解SpringBoot中CommandLineRunner接口
Spring Boot的CommandLineRunner接口是一個函數(shù)式接口,用于在Spring Boot應用程序啟動后執(zhí)行一些初始化操作,它提供了一個run方法,該方法在應用程序啟動后被調(diào)用,本文給大家詳細介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下2023-10-10
spring boot創(chuàng)建項目包依賴問題的解決
本篇文章主要介紹了spring boot創(chuàng)建項目包依賴問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Java API方式調(diào)用Kafka各種協(xié)議的方法
本篇文章主要介紹了Java API方式調(diào)用Kafka各種協(xié)議的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

