標籤:java pdf itext
1.文檔內容的基本格式設定
範例程式碼:
public class Pdf05C {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("C:\\005.pdf"));
document.open();
BaseFont font = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
BaseFont font2 = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
BaseFont font3 = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//BaseFont.createFont(name, encoding, embedded)
Font chinese = new Font(font3,15,Font.NORMAL);
Font content1 = new Font(font2, 12,Font.BOLD);
Font content = new Font(font, 16);
document.add(new Paragraph("This document is created by iText!",content));
document.add(new Paragraph("www.samsung.com", content1));
document.add(new Paragraph("www.samsung.com"));
document.add(new Paragraph("there are some words",FontFactory.getFont(FontFactory.HELVETICA,15,Font.UNDERLINE)));
document.add(new Paragraph("These content will be deperated!",
FontFactory.getFont(FontFactory.COURIER,15,Font.NORMAL|Font.STRIKETHRU)));
document.add(new Paragraph("中文內容也可以正常顯示",chinese));
Chunk uk1 = new Chunk("Text Chunk1",FontFactory.getFont(FontFactory.COURIER_BOLD,12,Font.ITALIC));
Chunk uk2 = new Chunk("Text Chunk2",FontFactory.getFont(FontFactory.COURIER_BOLD,15,Font.BOLD));
uk2.setBackground(new Color(30, 50, 120));
document.add(uk1);
document.add(uk2);
JOptionPane.showMessageDialog(null, "文檔已建立!");
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
要點:
1)設定字型,使用BaseFont建立基準字型,通過Font類來格式化具體顯示內容的字型。常見格式包括:加粗、斜體、底線、刪除線等。
2)文檔塊Chunk,使用Chunk類建立一個文檔塊用於顯示在PDF文檔中,這些文檔塊是可以在同一行內並列顯示的。
3)當PDF文檔中顯示非英文字元時,需要使用BaseFont類的靜態方法createFont(String name,String encoding,boolean embedded)來對文字進行重新編碼指定。處理中文時name為具體的中文字型名稱,encoding值為UniGB-UCS2-H,embedded值使用字型常量NOT_EMBEDDED。
2.對文檔添加頁碼
範例程式碼:
public class Pdf06C {
/**
* @date 2015年7月7日 下午3:19:34
*/
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("C:\\006.pdf"));
document.open();
for (int i = 1; i < 5; i++) {
document.add(new Paragraph("No."+i+"page"));
document.newPage();
}
document.close();
PdfReader reader = new PdfReader("C:\\006.pdf");
int pages = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("C:\\006_pre.pdf"));
BaseFont ch = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
PdfContentByte pager = null;
for (int i = 1; i <= pages; i++) {
pager = stamp.getUnderContent(i);
pager.beginText();
pager.setFontAndSize(ch, 10);
pager.setTextMatrix(280, 15);
pager.showText("第"+i+"頁,共"+pages+"頁");
pager.endText();
}
stamp.close();
JOptionPane.showMessageDialog(null, "操作已完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上程式先建立了一個4頁的pdf文檔006.pdf,位於C盤根路徑下。添加頁碼時,通過PdfReader類來讀取這個文檔,並通過PdfStamper類來完成頁碼添加,輸出處理後的文檔為006_pre.pdf。
關鍵API:
1)public int com.lowagie.text.pdf.PdfReader.getNumberOfPages(),該方法返回reader讀取文檔的總頁數。
2)使用PdfStamper擷取當前頁面底部內容,進行頁碼添加,並使用setTextMatrix(float x,float y)方法設定頁碼的顯示位置。
本文出自 “notDebug” 部落格,轉載請與作者聯絡!
Java操作PDF文檔