java使用iText產生pdf表格

來源:互聯網
上載者:User

標籤:poi   產生   ddt   get   code   java.awt   ica   ase   out   

首先匯入如下jar包:

  

package poi.zr.com.pojo;import java.awt.Color;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import com.lowagie.text.BadElementException;import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.Table;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfWriter;public class POI {    public static void main(String[] args) throws DocumentException, IOException {        // 建立Document對象(頁面的大小為A4,左、右、上、下的頁面邊界為10)        Document document = new Document(PageSize.A4, 10, 10, 10, 10);        // 建立書寫器        PdfWriter.getInstance(document, new FileOutputStream("/Users/apple/Desktop/poi.PDF"));        // 設定相關的參數        POI.setParameters(document, "開發人員測試", "測試", "測試 開發人員 調試", "甘雨路", "甘雨路");        // 開啟文檔        document.open();        // 使用iTextAsian.jar中的字型        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);        Font font = new Font(baseFont);                List<User> users = new ArrayList<User>();        // 迴圈添加對象        for (int i = 0; i < 5; i++) {            User user = new User();            user.setId(""+i);            user.setInfo("開發人員測試"+i);            user.setName("測試"+i);            users.add(user);        }                Table table = POI.setTable(users);        document.add(new Paragraph("使用者資訊如下:",POI.setFont()));        document.add(table);                // 關閉文檔        document.close();    }        public static Table setTable(List<User> users) throws BadElementException{        //建立一個有3列的表格        Table table = new Table(3);        table.setBorderWidth(1);        table.setBorderColor(new Color(0, 0, 255));        table.setPadding(5);        table.setSpacing(5);        // 建立表頭        Cell cell1 = POI.setTableHeader("編號ID");        Cell cell2 = POI.setTableHeader("基本資料");        Cell cell3 = POI.setTableHeader("姓名");        table.addCell(cell1);        table.addCell(cell2);        table.addCell(cell3);        // 添加此代碼後每頁都會顯示表頭        table.endHeaders();                        for (int i = 0; i < users.size(); i++) {            Cell celli1 = POI.setTableHeader(users.get(i).getId());            Cell celli2 = POI.setTableHeader(users.get(i).getInfo());            Cell celli3 = POI.setTableHeader(users.get(i).getName());            table.addCell(celli1);            table.addCell(celli2);            table.addCell(celli3);        }                return table;            }    /**     * 設定字型編碼格式     * @return     */    public static Font setFont(){        BaseFont baseFont = null;        try {            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);        } catch (DocumentException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        Font font = new Font(baseFont, 8, Font.NORMAL,Color.BLUE);        return font;    }    /**     * 設定cell     * @param name     * @return     * @throws BadElementException     */    public static Cell setTableHeader(String name) throws BadElementException{                Cell cell = new Cell(new Phrase(name,POI.setFont()));        //儲存格水平對齊        cell.setHorizontalAlignment(Element.ALIGN_CENTER);        //儲存格垂直對齊        cell.setVerticalAlignment(Element.ALIGN_CENTER);//        cell.setHeader(true);        cell.setBackgroundColor(Color.RED);        return cell;    }        /**     * 設定相關參數     * @param document     * @return     */    public static Document setParameters(Document document,String title,String subject,String keywords,String author,            String creator){        // 設定標題        document.addTitle(title);        // 設定主題        document.addSubject(subject);        // 設定作者        document.addKeywords(keywords);        // 設定作者        document.addAuthor(author);        // 設定建立者        document.addCreator(creator);        // 設定生產者        document.addProducer();        // 設定建立日期        document.addCreationDate();                return document;    }    }

代碼運行後產生的效果如下:

 

java使用iText產生pdf表格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.