Phrase objects knows how to add line spacing if the added phrase exceeds the right edge of the document. It does not, however, add extra space between paragraphs. For this, you need to use a
Paragraph
object.
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
Phrase
constructor, like this: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("Phrase2.pdf")); document.open(); Chunk chunk = new Chunk("This is a sentence "); Phrase phrase = new Phrase(50); phrase.add(chunk); phrase.add(chunk); phrase.add(chunk); phrase.add(chunk); phrase.add(chunk); phrase.add(chunk); document.add(phrase); document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Below is a screenshot of what the generated document looks like. Notice the extra line spacing between the two lines.
An IText Phrase example with custom line spacing |
沒有留言:
張貼留言