[英]adding cell of a table in itext
為什么包含段落p的單元格未添加到表中?
package iText;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.*;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class NewMain1 {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("C:\\Users\\om\\Desktop\\pdf\\2.pdf"));
document.open();
PdfPTable table = new PdfPTable(3); // 3 columns.
Font font = new Font(FontFamily.HELVETICA, 22, Font.BOLD, BaseColor.WHITE);
Paragraph p = new Paragraph("xyz", font);
table.addCell("abc");
table.addCell(p);
table.addCell("cef");
table.addCell("ghi");
// table.completeRow();
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
document.add(table);
document.close();
} catch(Exception e){
}
}
}
ps我是這個itext的新手。 如果有人能解釋我如何通過itext生成pdf表格,那就太好了。 提前致謝 :)
您將創建一種白色字體並將其繪制在通常顯示為白色的畫布上。 它在那里,您只是看不到它。 嘗試將顏色更改為BaseColor.BLACK
其他顏色。
編輯
為了回答您評論中的問題, Document
構造函數的重載之一采用一個矩形,該矩形定義頁面的默認大小。 iText有一個名為PageSize
的幫助程序類,該類定義了許多常見的頁面,但是您可以自由使用3 x 3
到14,400 x 14,400
任何尺寸。 例如,您可以說new Document(PageSize.LETTER)
或new Document(new RectangleReadOnly(612, 702))
。
知道這一點后,您便擁有了x
和y
的最大值,並且除非您確實做了一些奇怪的事情,否則最小值x
和y
均為零。 對於PDF,左下角是原點,因此是0 x 0
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.