【itext學習之路】-------(第二篇)設定pdf的一些常用屬性
阿新 • • 發佈:2018-11-11
在上一篇文章中,我們已經成功的建立了一個簡單的pdf,下面我將學習設定該pdf的常用屬性,其中包括:作者,建立時間,pdf建立者,pdf生產者,關鍵字,標題,主題
- 下面是我們的程式碼,非常簡單。
package cn.tomtocc.pdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com .itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class pdfTest {
public static void main(String[] args) throws FileNotFoundException,
DocumentException {
//實現A4紙頁面 並且橫向顯示(不設定則為縱向)
Document document = new Document(PageSize.A 4.rotate());
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream("d:/demo.pdf"));
// 開啟文件
document.open();
// 生成第一頁
document.newPage();
//設定作者
document.addAuthor("tomaotcc");
//設定建立日期
document.addCreationDate();
// 設定建立者
document.addCreator ("tomaotcc.com");
// 設定生產者
document.addProducer();
// 設定關鍵字
document.addKeywords("my");
//設定標題
document.addTitle("Set Attribute Example");
//設定主題
document.addSubject("An example to show how attributes can be added to pdf files.");
// 加入文件內容
document.add(new Paragraph("my first pdf demo"));
// 關閉文件
document.close();
pdfWriter.close();
}
}
- 然後我們開啟生成的pdf,然後滑鼠右鍵—–>文件屬性
然後我們就可以看到我們剛才設定的pdf文件屬性了
到這裡為止,我們設定pdf的屬性就完成了,接下來,我們將要學習pdf文件加密。
【itext學習之路】系列教程
【itext學習之路】—–(第一篇)建立一個簡單的pdf文件
【itext學習之路】—–(第二篇)設定pdf的一些常用屬性
【itext學習之路】—–(第三篇)對pdf文件進行加密和許可權設定
【itext學習之路】—–(第四篇)給pdf增加文字水印和圖片水印
【itext學習之路】—–(第五篇)對pdf進行蓋章/簽章/數字簽名