iText操作PDF基礎-2 – JAVA編程語言程序開發技術文章

UML 元素類圖:

 clip_image001

Chunk & Phase
Chunk
最小的元素(有同一種類型的字體,顏色,樣式組成的字符串)。字體屬性都是由Font對象來定義。

一般不直接添加chunk元素,而是使用chunk組成其他大元素後,添加到文檔中。

不過Chunk.NEWLINE是直接添加到文檔中。

LEADING
文字之間的間距,Chunk對象不包含文字間距設置。

1
PdfWriter.getInstance(document, new FileOutputStream(RESULT)).setInitialLeading(16);
PHRASE
由一系列帶有leading屬性的chunk對象組成。

基本樣例:

01
public static void main(String[] args) throws DocumentException,
02
 
03
IOException {
04
 
05
    Document document = new Document(); // 創建一個文檔
06
 
07
    PdfWriter.getInstance(document, new FileOutputStream("D:/demo.pdf")); // 獲取PdfWriter實例
08
 
09
    document.open(); // 打開文檔
10
 
11
    Phrase phase = new Phrase();
12
 
13
    phase.add(new Chunk("first line"));
14
 
15
    phase.add(Chunk.NEWLINE); //換行
16
 
17
    phase.add(new Chunk("second Line"));
18
 
19
    document.add(phase);
20
 
21
    document.close(); // 關閉文檔
22
 
23
}
結果:

 clip_image001[7]

Paragraph
有一系列phrase和一些額外的屬性帶有換行標識組成。

代碼:

01
public static void main(String[] args) throws FileNotFoundException,
02
 
03
DocumentException {
04
 
05
    Document document = new Document(); // 創建一個文檔
06
 
07
    PdfWriter.getInstance(document, new FileOutputStream("D:/demo.pdf")); // 獲取PdfWriter實例
08
 
09
    document.open(); // 打開文檔
10
 
11
    Paragraph para = new Paragraph();
12
 
13
    para.add(new Chunk("first chunk"));
14
 
15
    para.add(new Chunk(",second chunk"));
16
 
17
    para.setAlignment(Element.ALIGN_RIGHT); // 右對齊
18
 
19
    document.add(para);
20
 
21
    // Pharagraph帶有換行標識符
22
 
23
    Paragraph paraL = new Paragraph();
24
 
25
    paraL.add(new Chunk("2 : first chunk"));
26
 
27
    paraL.add(new Chunk(",second chunk"));
28
 
29
    paraL.setAlignment(Element.ALIGN_LEFT);// 左對齊
30
 
31
    document.add(paraL);
32
 
33
    Paragraph paraR = new Paragraph();
34
 
35
    paraR.add(new Chunk("3 : first chunk"));
36
 
37
    paraR.add(new Chunk(",second chunk"));
38
 
39
    paraR.setAlignment(Element.ALIGN_LEFT);// 左對齊
40
 
41
    paraR.setLeading(30f);// 設置和上面一段paragraph間距
42
 
43
    document.add(paraR);
44
 
45
    document.close(); // 關閉文檔
46
 
47
}

List
一系列Paragraph組成

代碼:

01
public static void main(String[] args) throws FileNotFoundException,
02
DocumentException {
03
    Document document = new Document(); // 創建一個文檔
04
    PdfWriter.getInstance(document, new FileOutputStream("D:/demo.pdf")); // 獲取PdfWriter實例
05
    document.open(); // 打開文檔
06
    List pList = new List();
07
    ListItem li = new ListItem();
08
    for (int i = 0; i < 3; i++) {
09
        Paragraph p = new Paragraph();
10
        p.add(new Chunk("item : " + i));
11
        li.add(p);
12
    }
13
    pList.setNumbered(true);
14
    pList.add(li);
15
    document.add(pList);
16
    document.add(Chunk.NEWLINE);
17
    List pList2 = new List();
18
    ListItem li2 = new ListItem();
19
    for (int i = 0; i < 3; i++) {
20
        li2.add(new Chunk("mainItem : " + i));
21
            for (int j = 0; j < 3; j++) {
22
                List pList3 = new List();
23
                ListItem li3 = new ListItem();
24
                Paragraph p = new Paragraph();
25
                p.add(new Chunk("subItem : " + j));
26
                li3.add(p);
27
                pList3.add(li3);
28
                pList3.setIndentationLeft(10f);
29
                li2.add(pList3);
30
            }
31
        }
32
        pList2.add(li2);
33
        document.add(pList2);
34
        document.close(); // 關閉文檔
35
}
clip_image003

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *