Skip to content
Advertisement

Is there any way to keep whitespaces before text in iText7?

I’ve added a Text object that start with whitespaces to the Paragraph object,

but the whitespace of paragraph is removed in iText7(7.0.4).

It looks like left trim. Is this a specification of Paragraph?

Is there any way to keep whitespaces before text?

Paragraph p = new Paragraph().add(new Text("  abc")); // Only "abc" appears in pdf

Advertisement

Answer

iText will trim spaces.
But it will not remove non-breaking spaces.

File outputFile = new File(System.getProperty("user.home"), "output.pdf");
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
Document layoutDocument = new Document(pdfDocument);

layoutDocument.add(new Paragraph("u00A0u00A0u00A0Lorem Ipsum"));
layoutDocument.add(new Paragraph("Lorem Ipsum"));
layoutDocument.close();
Advertisement