IText operation PDF basics-2

Source: Internet
Author: User

UML element class diagram:

 

Chunk & Phase
Chunk
The smallest element (a string consisting of fonts, colors, and styles of the same type ). Font attributes are defined by Font objects.

The chunk element is not directly added. Instead, the chunk is used to form other large elements and then added to the document.

However, Chunk. NEWLINE is directly added to the document.

LEADING
Text spacing. The Chunk object does not contain the text spacing settings.

1
Using writer. getInstance (document, new FileOutputStream (RESULT). setInitialLeading (16 );
PHRASE
It consists of a series of chunk objects with the leading attribute.

Basic example:

01
Public static void main (String [] args) throws disable entexception,
02
 
03
IOException {
04
 
05
Document document = new Document (); // create a Document
06
 
07
Response writer. getInstance (document, new FileOutputStream ("D:/demo.pdf"); // gets the response writer instance
08
 
09
Document. open (); // open the document
10
 
11
Phrase phase = new Phrase ();
12
 
13
Phase. add (new Chunk ("first line "));
14
 
15
Phase. add (Chunk. NEWLINE); // line feed
16
 
17
Phase. add (new Chunk ("second Line "));
18
 
19
Document. add (phase );
20
 
21
Document. close (); // close the document
22
 
23
}
Result:

 

Paragraph
There are a series of phrase and some additional attributes with a line feed identifier.

Code:

01
Public static void main (String [] args) throws FileNotFoundException,
02
 
03
DocumentException {
04
 
05
Document document = new Document (); // create a Document
06
 
07
Response writer. getInstance (document, new FileOutputStream ("D:/demo.pdf"); // gets the response writer instance
08
 
09
Document. open (); // open the document
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); // right alignment
18
 
19
Document. add (para );
20
 
21
// Pharagraph carries a line feed identifier
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); // left alignment
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); // left alignment
40
 
41
ParaR. setLeading (30f); // you can specify the distance from the preceding paragraph.
42
 
43
Document. add (paraR );
44
 
45
Document. close (); // close the document
46
 
47
}

List
A series of Paragraph Components

Code:

01
Public static void main (String [] args) throws FileNotFoundException,
02
DocumentException {
03
Document document = new Document (); // create a Document
04
Response writer. getInstance (document, new FileOutputStream ("D:/demo.pdf"); // gets the response writer instance
05
Document. open (); // open the document
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 (); // close the document
35
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.