David Zalkin
David Zalkin

Reputation: 57

Last line of justified right-to-left text should be aligned to right, but is not

When setting TextAlignment.JUSTIFIED the last line is not justified, which is correct. However, for text in a right-to-left language, like Hebrew, the last line is aligned to the left, not the right.

As you can see in the code, I tried setting various properties on the Document object, none of which had any impact on the problem.

Here is a sample PDF file

String path = "C:\\Users\\davidz\\Desktop\\verylittle.pdf";
PdfWriter writer = new PdfWriter(new FileOutputStream(path));
PdfDocument pdf = new PdfDocument(writer);
pdf.addNewPage();
Document doc = new Document(pdf);
doc.setBaseDirection(BaseDirection.RIGHT_TO_LEFT);
doc.setTextAlignment(TextAlignment.RIGHT);
doc.setHorizontalAlignment(HorizontalAlignment.RIGHT);
JavaItext.loadLicenses("\\\\formit7\\e$\\formit\\ConvertIT\\Resources\\Fonts");
GetFonts fontbank = new GetFonts("C:\\Users\\davidz\\Desktop\\fonts");
PdfFont sanserif = fontbank.getFont("Arial", true);

String hebrew = "כל עוד בלבב פנימה נפש יהודי הומיה ולפאתי מזרח קדימה עין לציון צופיה עוד לא אבדה תקותנו";
Paragraph p = new Paragraph(hebrew);
p.setFont(sanserif);
p.setWidth(180);
p.setBorder(new SolidBorder(1));
p.setTextAlignment(TextAlignment.JUSTIFIED);
p.setFixedLeading(12f);
p.setHeight(50f);
doc.add(p);

doc.close();
pdf.close();

Upvotes: 0

Views: 186

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12312

This bug has been fixed in the current iText 7.1.5-SNAPSHOT development version and the fix will be available in the next release. The result looks like following now:

enter image description here

Meanwhile, you can use the development version with the fix for now. It can be accessed from iText Artifactory with the following Maven configuration:

<repositories>
    <repository>
        <id>itext-snapshot</id>
        <name>iText Repository - snapshots</name>
        <url>https://repo.itextsupport.com/snapshot</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.1.5-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>typography</artifactId>
        <version>2.0.3-SNAPSHOT</version>
    </dependency>
</dependencies>

Upvotes: 1

Related Questions