iText7 - 希伯来文字显示为反转?

时间:2018-05-18 23:49:45

标签: java itext7

我有一个问题,希伯来文字被反转显示 我在iText5中找到了很多关于如何做到这一点的样本,我正在寻找iText7解决方案 我尝试的一种可能的解决方案是反转字符串,但如果字符串不包含字母字符,则会搞砸。

另外如何告诉PDF从右到左运行?
我试过了

title.setBaseDirection(BaseDirection.RIGHT_TO_LEFT);

我没有看到任何区别 谢谢!

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.BaseDirection;

public class SimpleHebPdfWriter {


    public static final String REGULAR =  "c:\\windows\\fonts\\ARIALUNI.TTF";

    public static void main(String[] args) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
        String dest = "c:\\temp\\" + formatter.format(Calendar.getInstance().getTime()) + ".pdf";
        File file = new File(dest);
        file.getParentFile().mkdirs();
        new SimpleHebPdfWriter().manipulatePdf(dest);
    }

    protected void manipulatePdf(String dest) throws Exception { 
        FontProgramFactory.registerFont(REGULAR, "arialUnicode");
        PdfFont myFont = PdfFontFactory.createRegisteredFont("arialUnicode", PdfEncodings.IDENTITY_H, true);
        StringBuilder builder = new StringBuilder("-> אבגדהוזחטיכלמנסעפצקרשת  ->");
        Text title = new Text(builder.toString()).setFont(myFont);
        Text title2 = new Text(builder.reverse().toString()).setFont(myFont);
        title.setBaseDirection(BaseDirection.RIGHT_TO_LEFT);
        title2.setBaseDirection(BaseDirection.RIGHT_TO_LEFT);

        Paragraph p = new Paragraph().setFont(myFont).add(title).add(System.lineSeparator()).add(title2);       

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        pdfDoc.addFont(myFont);
        Document doc = new Document(pdfDoc);
        doc.setBaseDirection(BaseDirection.RIGHT_TO_LEFT);
        doc.add(p);
        doc.close();
    }
}

下图显示了它的外观,没有做任何事情(第一行)
然后反转弦(第二行)
和期望的输出(第三行) sample output

0 个答案:

没有答案