从右至左对齐的文本的最后一行应与右侧对齐,但不应对齐

时间:2019-01-03 15:28:31

标签: itext7 text-alignment

设置TextAlignment.JUSTIFIED时,最后一行不合理,这是正确的。但是,对于像希伯来语这样的从右到左语言的文本,最后一行是左对齐,而不是右对齐。

正如您在代码中看到的那样,我尝试在Document对象上设置各种属性,这些属性都不会对问题产生任何影响。

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();

1 个答案:

答案 0 :(得分:1)

此错误已在当前的iText 7.1.5-SNAPSHOT开发版本中修复,该修复程序将在下一版本中提供。结果如下所示:

enter image description here

同时,您现在可以将开发版本与修复一起使用。可以使用以下Maven配置从iText Artifactory进行访问:

<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>