利用itext產生pdf

來源:互聯網
上載者:User

標籤:

1、引入jar包 :

2、demo1

public static void main(String[] args) throws FileNotFoundException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        String filename="D:/111.pdf" ;
        
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }

 

 

demo2

public class PDFTest2 {
    private Font FontChinese;
    public void simplePDF() {
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            FontChinese = new Font(bfChinese, 12, Font.NORMAL);
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("F:\\Hello simplePDF.pdf"));
            document.open();

            PdfPTable table = new PdfPTable(4);
            table.addCell(getCell("姓名", 1, 1));
            table.addCell(getCell("董亞轉", 1, 1));
            table.addCell(getCell("編號", 1, 1));
            table.addCell(getCell("", 1, 1));

            table.addCell(getCell("部門", 1, 1));
            table.addCell(getCell("", 1, 1));
            table.addCell(getCell("崗位名稱", 1, 1));
            table.addCell(getCell("", 1, 1));

            table.addCell(getCell("到職日期", 1, 1));
            table.addCell(getCell("", 1, 1));
            table.addCell(getCell("預定離職日期", 1, 1));
            table.addCell(getCell("", 1, 1));

            table.addCell(getCell("事由", 1, 3));
            table.addCell(getCell("", 3, 3));

            table.addCell(getCell("部門意見", 1, 3));
            table.addCell(getCell("", 3, 3));
            document.add(table);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
        PdfPCell cell = new PdfPCell();
        try {
            cell = new PdfPCell(new Phrase(cellValue, FontChinese));
            cell.setRowspan(rowSpan);
            cell.setColspan(colspan);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cell;
    }
    public static void main(String[] args) {
        PDFTest2 pt = new PDFTest2();
        pt.simplePDF();
    }

}

 

demo3

 

 

 public static void main(String[] args) throws Exception  
            {  
                Document pdfDoc = new Document();  
                // 將要產生的 pdf 檔案的路徑輸出資料流  
                FileOutputStream pdfFile =   
                    new FileOutputStream(new File("D:/222.pdf"));  
        
                // pdf 檔案中的一個文欄位落  
                Paragraph paragraph = new Paragraph("My first PDF file with an image ...");  
                Image image = Image.getInstance("d:/Desert.jpg");  
                  
                // 用 Document 對象、File 對象獲得 PdfWriter 輸出資料流對象  
                PdfWriter.getInstance(pdfDoc, pdfFile);  
                pdfDoc.open();  // 開啟 Document 文檔  
                  
                // 添加一個文欄位落、一張圖片  
                pdfDoc.add(paragraph);  
                pdfDoc.add(image);  
              
                pdfDoc.close();  
    }

百度雲網盤

連結: http://pan.baidu.com/s/1o8IjIlW 密碼: ezms

利用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.