The
com.itextpdf.text.Paragraph
class in IText represents a "paragraph" of text. In a paragraph you can set the paragraph alignment, indentation and spacing before and after the paragraph.
Here is a simple code example:
import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; public class DocumentExample { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("Paragraph.pdf")); document.open(); Paragraph paragraph = new Paragraph(); for(int i=0; i<10; i++){ Chunk chunk = new Chunk( "This is a sentence which is long " + i + ". "); paragraph.add(chunk); } document.add(paragraph); document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Here is what the generated document looks like:
An IText Paragraph example |
Notice how sentence 6 is nicely located on its own line.
Line Spacing
Paragraph objects knows how to add line spacing if the added text exceeds the right edge of the document. Line spacing is measured in user units. There are 72 units per inch. The default spacing is 1.5 times the font height. You can change the line spacing by passing spacing as a parameter to the
Paragraph
constructor, like this:Paragraph paragraph = new Paragraph(50);
Spacing Before and After Paragraph
You can also set the spacing before and after a paragraph. Here is how you do that in code:
paragraph.setSpacingAfter(50); paragraph.setSpacingBefore(50);
Alignment
You can set the alignment of the paragraph using the
setAlignment()
method. This sets what side of the page the text is aligned to. Here is an example:paragraph.setAlignment(Element.ALIGN_LEFT); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.setAlignment(Element.ALIGN_RIGHT);
Indentation
You can set the left and right indentation of the paragraph. This moves the paragraph content further away from the edges of the page. Here is the code to do that:
paragraph.setIndentationLeft(50); paragraph.setIndentationRight(50);
A Larger Paragraph Example
Here is a somewhat larger paragraph example which adds two parapraphs to a document. The first paragraph uses the default left alignment, and indentation. The second paragraph is center-aligned, and uses 50 user units as both left and right indentation.
import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; /** */ public class Paragraph2Example { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("Paragraph2.pdf")); document.open(); Paragraph paragraph1 = new Paragraph(); Paragraph paragraph2 = new Paragraph(); paragraph2.setSpacingAfter(25); paragraph2.setSpacingBefore(25); paragraph2.setAlignment(Element.ALIGN_CENTER); paragraph2.setIndentationLeft(50); paragraph2.setIndentationRight(50); for(int i=0; i<10; i++){ Chunk chunk = new Chunk( "This is a sentence which is long " + i + ". "); paragraph1.add(chunk); paragraph2.add(chunk); } document.add(paragraph1); document.add(paragraph2); document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Here is how the resulting document looks:
A larger IText Paragraph example |
沒有留言:
張貼留言