I am working on a program to convert a txt file to a pdf with alot of changes on line indentation. However, I am unable to find the exact command that can achieve this in iText 7. I am aware that in iText 5 there were methods such as setIndentationLeft()
and setIndentationRight()
of paragraph object which allowed explicit indenting, but this is not available in the latest version. The latest version only offers setFirstLineIndent()
which doesn't suffice for my needs.
This is what I wanna achieve:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
As you can see, line starts with different indentation. I have considered the use prepending spaces to the line but I find that very inefficient. How can I go about solving this?
Answering my own question to help others with same problem.
There are 3 ways to solve this problem:
1) Since I would like to set it for each line in the paragraph you can split the lines of text into different chunks and then set indent for that individually. Once done you can add it to your document.
2) Though iText5 is outdated it can still be used to solve many problems. If you are using Maven as your build tool then simply add the below code to your dependencies and you can then start using the left and right indent methods.
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
3) You can also use a table cell approach, where you can add the text inside a given cell and set appropriate margins, see below.
Table table = new Table(1);
Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");
right.setMarginLeft(20);
Cell rightCell = new Cell().add(right);
table.addCell(rightCell);
doc.add(table);
This will be rendered as folllows.
I would personally discourage the use of the second method but if it can solve your problem then there's nothing stopping you.