1. 程式人生 > >iText操作PDF學習(三)

iText操作PDF學習(三)

三、 用 iText 進行 PDF 操作

1) 經典的 hello word.(說明: 生成一個 PDF、內容為 hello word!)

新建java project iTextDemo;-------新建類HelloiText--------在main方法中編寫以下程式碼:

//新建一個文件
Document document = new Document();


try {
//建立一個書寫器與文件關聯
PdfWriter.getInstance(document, new FileOutputStream("D:\\helloiText.pdf"));
//開啟文件
document.open();
//向文件中寫入文字


document.add(new Paragraph("hello world!"));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}finally{
//關閉文件
document.close();
}

執行-輸出:
在D盤可看見生成了一個helloiText.pdf檔案,開啟檔案可見:

至此,最簡單的iText操作建立helloworld PDF檔案程式完成了