欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java使用itext生成Pdf文档插入表格实例,itextpdf,下面的例子演示如何使用i

来源: javaer 分享于  点击 41712 次 点评:186

java使用itext生成Pdf文档插入表格实例,itextpdf,下面的例子演示如何使用i


下面的例子演示如何使用itext向pdf文档中插入表格,并设置表格单元格边框颜色:

package cn.outofmemory.example.itextpdf;import com.itextpdf.text.BaseColor;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 TableCellBorderColor {    public static void main(String[] args) {        Document document = new Document();        try {            PdfWriter.getInstance(document,                    new FileOutputStream("TableCellBorder.pdf"));            document.open();            PdfPTable table = new PdfPTable(3);            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));            cell1.setUseBorderPadding(true);            //            // Setting cell's border width and color            //            cell1.setBorderWidth(5f);            cell1.setBorderColor(BaseColor.BLUE);            table.addCell(cell1);            PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));            cell2.setUseBorderPadding(true);            //            // Setting cell's background color            //            cell2.setBackgroundColor(BaseColor.GRAY);            //            // Setting cell's individual border color            //            cell2.setBorderWidthTop(1f);            cell2.setBorderColorTop(BaseColor.RED);            cell2.setBorderColorRight(BaseColor.GREEN);            cell2.setBorderColorBottom(BaseColor.BLUE);            cell2.setBorderColorLeft(BaseColor.BLACK);            table.addCell(cell2);            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));            cell3.setUseBorderPadding(true);            //            // Setting cell's individual border width            //            cell3.setBorderWidthTop(2f);            cell3.setBorderWidthRight(1f);            cell3.setBorderWidthBottom(2f);            cell3.setBorderWidthLeft(1f);            table.addCell(cell3);            table.completeRow();            document.add(table);        } catch (DocumentException | FileNotFoundException e) {            e.printStackTrace();        } finally {            document.close();        }    }}
相关栏目:

用户点评