Java如何在iText中设置表格的宽度?
我们可以使用setWidthPercentage()com.itextpdf.text.pdf.PdfPTable类的方法设置表格的宽度。此方法接受浮点值作为参数。此方法以表格在页面中占据的宽度的百分比设置宽度。默认表的宽度为80%。
让我们看下面的代码示例:
package org.nhooo.example.itextpdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class TableWidthDemo {
public static void main(String[] args) {
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream("TableWidthDemo.pdf"));
doc.open();
//创建一个具有5列的表格
PdfPTable table = new PdfPTable(5);
table.addCell(new PdfPCell(new Phrase("Cell 1")));
table.addCell(new PdfPCell(new Phrase("Cell 2")));
table.addCell(new PdfPCell(new Phrase("Cell 3")));
table.addCell(new PdfPCell(new Phrase("Cell 4")));
table.addCell(new PdfPCell(new Phrase("Cell 5")));
//设置表格将占用的宽度百分比
//在页面中。
table.setWidthPercentage(50);
doc.add(table);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
doc.close();
}
}
}Maven依赖
<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
热门推荐
10 宝宝新生入学祝福语简短
11 年前幸福祝福语大全简短
12 整形机构开业祝福语简短
13 早日回国祝福语简短英语
14 虎年开新车祝福语简短
15 炸鸡拼盘生日祝福语简短
16 嫂子生日英语祝福语简短
17 国庆简短幽默祝福语
18 空军大学开学祝福语简短