獨家:使用iText JAR在Java中建立PDF

來源:互聯網
上載者:User

【Csdn.net 4月3日編譯】在如今的企業級應用程式中產生PDF的功能很常見。但是使用Java來實現這個功能卻不那麼容易,因為Java沒有預設提供處理PDF檔案所需的API。不過現在有了iText JAR,實現這個功能就不難了。

 

iText是一個免費的Java-PDF庫,通過它可以實現on the fly(動態)建立PDF。iText是那些需要動態PDF文檔產生或操作功能來改進應用程式的開發人員的理想選擇。iText不是一個使用者終端工具,也就是說你不用像使用Acrobat或其它PDF工具那樣,只需要把iText內建到自己的程式中,它就可以自動的完成PDF產生和操作。

 

iText具有如下功能:

將PDF傳輸到瀏覽器

通過XML檔案或資料庫來產生動態文檔

支援眾多的PDF互動功能

添加書籤、頁碼和浮水印等等

分割、串連PDF頁面,或對他們進行平移縮放等操作

自動填PDF表格

為PDF檔案添加數字浮水印

 

系統要求

JDK 1.4或更高版本

 

獲得方法

可以再這裡下載:http://www.lowagie.com/iText/download.html

核心檔案是:iText-2.1.5.jar

 

使用iText產生PDF的簡單例子

將itext.jar加入你的class path。並將下邊的代碼複製到一個叫GeneratePDF.java的檔案中,編譯並執行,它會在C盤建立一個叫Test.pdf的檔案。

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.Date;

 

import com.lowagie.text.Document;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

 

public class GeneratePDF {

 

public static void main(String[] args) {

try {

OutputStream file = new FileOutputStream(new File("C://Test.pdf"));

 

Document document = new Document();

PdfWriter.getInstance(document, file);

document.open();

document.add(new Paragraph("Hello Kiran"));

document.add(new Paragraph(new Date().toString()));

 

document.close();

file.close();

 

} catch (Exception e) {

 

e.printStackTrace();

}

}

}

 

在上面的代碼中我們建立了一個Document類來表示我們的PDF文檔,同時,我們將OutputStream對象通過getInstance()方法把輸出資料流給了OutputStream。因此,在上面的例子中,我們建立了一個用於輸出的檔案,並把輸出匯入到這個檔案。

 

使用OutputStreamHTTP請求中產生PDF

有時我們希望為網路應用程式加入PDF產生功能,使用者可以通過點擊一個儲存成PDF檔案按鈕就可以得到PDF檔案,因此需要動態建立PDF並發送到用戶端瀏覽器。

 

下面的代碼使用了Struts的Action class,其中實現了動態建立PDF並將其發送到瀏覽器的功能

 

package net.viralpatel.struts.helloworld.action;

 

import java.util.Date;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

 

import com.lowagie.text.Document;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

 

/**

* @author KiranRavi_Hegde

*

*/

public class PdfHelloWorldAction extends Action{

 

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

 

Document document = new Document();

try{

response.setContentType("application/pdf");

PdfWriter.getInstance(document, response.getOutputStream());

document.open();

document.add(new Paragraph("Hello Kiran"));

document.add(new Paragraph(new Date().toString()));

}catch(Exception e){

e.printStackTrace();

}

document.close();

return null;

 

}

}

 

自習分析上面的代碼,就會發現我們將response.getOutputStream()對象傳遞給了getInstance()方法。從而iText產生的輸出文檔就會直接由response輸出。同時不要忘記將content type設為application/pdf

設定PDF的屬性

產生PDF的同時,也可以設定不同的屬性,比如作者名,題目以及檔案描述等,下面的代碼就產生了一些屬性:

 

document.addAuthor("Kiran Hegde");

document.addCreationDate();

document.addCreator("iText library");

document.addTitle("Hello World 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.