Phrase and Paragraph examples

This is a code example of iText PDF, discover more.

8th November 2015
admin-marketing

Switch code for this example

ParagraphSpacingBefore.java
  1. /*
  2.  * This question was answered by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/32945733/itext-spacingbefore-property-applied-to-paragraph-causes-new-page
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import sandbox.WrapToTest;
  15.  
  16. /**
  17.  *
  18.  * @author Bruno Lowagie (iText Software)
  19.  */
  20. @WrapToTest
  21. public class ParagraphSpacingBefore {
  22.    
  23.     public static final String DEST = "results/objects/paragraph_spacebefore.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ParagraphSpacingBefore().createPdf(DEST);
  30.     }
  31.     public void createPdf(String filename) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  34.         document.open();
  35.         Paragraph paragraph1 = new Paragraph("First paragraph");
  36.         Paragraph paragraph2 = new Paragraph("Second paragraph");
  37.         paragraph2.setSpacingBefore(380f);
  38.         document.add(paragraph1);
  39.         document.add(paragraph2);
  40.         document.close();
  41.     }
  42. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now