66

I am creating a PDF using iText. I want to insert blank lines between paragraphs and tables.

How can I achieve this?

alexander
  • 1,191
  • 2
  • 20
  • 40
Thanuja
  • 661
  • 1
  • 5
  • 3

12 Answers12

111

You can trigger a newline by inserting Chunk.NEWLINE into your document. Here's an example.

public static void main(String args[]) {
    try {
        // create a new document
        Document document = new Document( PageSize.A4, 20, 20, 20, 20 );
        PdfWriter.getInstance( document, new FileOutputStream( "HelloWorld.pdf" ) );

        document.open();

        document.add( new Paragraph( "Hello, World!" ) );
        document.add( new Paragraph( "Hello, World!" ) );

        // add a couple of blank lines
        document.add( Chunk.NEWLINE );
        document.add( Chunk.NEWLINE );

        // add one more line with text
        document.add( new Paragraph( "Hello, World!" ) );

        document.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Below is a screen shot showing part of the PDF that the code above produces.

Hello PDF with blank lines inserted

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • `public static final Chunk NEWLINE = new Chunk("\n");`. Jes was right, as are you. BTW: add(new Chunk("\n\n\n") should produce three new lines, though I haven't tested it. – Mark Storer Nov 22 '10 at 22:28
29

And to insert blank line between tables you can use these both methods

table.setSpacingBefore();      

table.setSpacingAfter();       
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
rgksugan
  • 3,521
  • 12
  • 45
  • 53
  • At least in version 5.1.1 of iText#, these methods don't seem to exist; however there are properties: `SpacingBefore` and `SpacingAfter`. – JYelton Aug 23 '11 at 14:53
  • 3
    This is much more useful than `Chunk.NEWLINE` if you are using tables. – Jordan.J.D Apr 23 '14 at 15:09
  • iText7 Table neither has setSpacingBefore/After methods nor a such named constant in Property.class. – Heri Feb 14 '23 at 11:10
18

You can use "\n" in Paragraph

document.add(new Paragraph("\n\n"));
Atif Mahmood
  • 8,882
  • 2
  • 41
  • 44
8

You can try a blank phrase:

document.add(new Phrase("\n"));
umanganiello
  • 756
  • 4
  • 7
5

You can add Blank Line throw PdfContentByte class in itextPdf. As shown below:

package com.pdf.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class Ranvijay {

    public static final String RESULT = "d:/printReport.pdf";

    public void createPdf(String filename) throws Exception {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(filename));
        document.open();

        Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
        PdfPTable tabletmp = new PdfPTable(1);
        tabletmp.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        tabletmp.setWidthPercentage(100);
        PdfPTable table = new PdfPTable(2);
        float[] colWidths = { 45, 55 };
        table.setWidths(colWidths);
        String imageUrl = "http://ssl.gstatic.com/s2/oz/images/logo/2x/googleplus_color_33-99ce54a16a32f6edc61a3e709eb61d31.png";
        Image image2 = Image.getInstance(new URL(imageUrl));
        image2.setWidthPercentage(60);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);
        PdfPCell cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        cell.addElement(image2);
        table.addCell(cell);
        String email = "ranvijay9286@gmail.com";
        String collectionDate = "09/09/09";
        Chunk chunk1 = new Chunk("Date: ", normal);
        Phrase ph1 = new Phrase(chunk1);

        Chunk chunk2 = new Chunk(collectionDate, bold);
        Phrase ph2 = new Phrase(chunk2);

        Chunk chunk3 = new Chunk("\nEmail: ", normal);
        Phrase ph3 = new Phrase(chunk3);

        Chunk chunk4 = new Chunk(email, bold);
        Phrase ph4 = new Phrase(chunk4);

        Paragraph ph = new Paragraph();
        ph.add(ph1);
        ph.add(ph2);
        ph.add(ph3);
        ph.add(ph4);

        table.addCell(ph);
        tabletmp.addCell(table);
        PdfContentByte canvas = writer.getDirectContent();
        canvas.saveState();
        canvas.setLineWidth((float) 10 / 10);
        canvas.moveTo(40, 806 - (5 * 10));
        canvas.lineTo(555, 806 - (5 * 10));
        canvas.stroke();
        document.add(tabletmp);
        canvas.restoreState();
        PdfPTable tabletmp1 = new PdfPTable(1);
        tabletmp1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        tabletmp1.setWidthPercentage(100);

        document.add(tabletmp1);

        document.close();
    }

    /**
     * Main method.
     * 
     * @param args
     *            no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws Exception {
        new Ranvijay().createPdf(RESULT);
        System.out.println("Done Please check........");
    }

}
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
Ranvijay Sachan
  • 2,407
  • 3
  • 30
  • 49
3
document.add(new Paragraph("")) 

It is ineffective above,must add a blank string, like this:

document.add(new Paragraph(" "));
0m3r
  • 12,286
  • 15
  • 35
  • 71
hyman zhang
  • 101
  • 1
  • 3
2

You can add empty line ;

 Paragraph p = new Paragraph();
 // add one empty line
  addEmptyLine(p, 1);
 // add 3 empty line
  addEmptyLine(p, 3);


private static void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
      paragraph.add(new Paragraph(" "));
    }
  }
Adel
  • 5,341
  • 2
  • 21
  • 31
2

You can also use

document.add(new Paragraph());
document.add(new Paragraph());

before seperator if you are using either it is fine.

Sumit Kumar
  • 564
  • 6
  • 14
1

Instead of using:

document.add( Chunk.NEWLINE );

use this:

document.add(new Paragraph(""));

it makes small space

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
1

directly use

paragraph.add("\n");

to add an empty line.

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

I had to add blank lines after a table and I manage it adding many divs as I need it with a css style with padding-top set it up, like this. I've used a template engine (underscore) to loop through the number of lines I need to add.

 <% var maxRow = 30; var pos = items.models.length; %>
 <% for( pos; pos < maxRow; pos++ ){ %>
       <div class="blankRow"></div>
 <% }; %>

My css file:

 .blankRow:{ padding-top: 15px;}
Zoe
  • 27,060
  • 21
  • 118
  • 148
miguel.angel
  • 49
  • 1
  • 8
0

I posted this in another question, but I find using tables with iTextSharp offers a great level of precision.

document.Add(BlankLineDoc(16));

public static PdfPTable BlankLineDoc(int height)
{
    var table = new PdfPTable(1) {WidthPercentage = 100};
    table = BlankLineTable(table, height);
    return table;
}

public static PdfPTable BlankLineTable(PdfPTable table, int height, int border = Rectangle.NO_BORDER)
{
    var cell = new PdfPCell(new Phrase(" "))
    {
        Border = border,
        Colspan = table.NumberOfColumns,
        FixedHeight = height
    };
    table.AddCell(cell);
    return table;
}

BlankLineTable can be used directly when working with tables

Jason Butler
  • 626
  • 1
  • 13
  • 25