Java操作应用—— 导出PDF文件
将表格导出成pdf也是一个比较常见的需求。通过itextpdf,导出pdf也不是什么难事。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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( "Employee Table" ); 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(); } } |
点击加载更多评论>>