將table導出成PDF是常見的需求,使用itextpdf導出會非常簡單
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class DrawPdf {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("Employee.pdf"));
document.open();
Paragraph para = new Paragraph("adidas originals");
para.setSpacingAfter(20);
document.add(para);
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("First Name"));
table.addCell(cell);
table.addCell("Last Name");
table.addCell("Gender");
table.addCell("Ram");
table.addCell("Kumar");
table.addCell("Male");
table.addCell("Lakshmi");
table.addCell("Devi");
table.addCell("Female");
document.add(table);
document.close();
}
}