本文介绍了RTL不能用pdf生成,使用itext 5.5进行阿拉伯语文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java代码与iText的5.5和xmlworker罐的帮助下写阿拉伯字符,但留下来的 writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL)使用权后,即使它的写作。

I have java code that writes arabic characters with the help of itext 5.5 and xmlworker jars, but its writing left to right even after writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL) is used.

使用的代码是:

public class CreateArabic extends DefaultHandler {
    /** Paths to and encodings of fonts we're going to use in this example */
    public static String[][] FONTS = {
        {"C:/arialuni.ttf", BaseFont.IDENTITY_H},
        {"C:/abserif4_5.ttf", BaseFont.IDENTITY_H},
        {"C:/damase.ttf", BaseFont.IDENTITY_H},
        {"C:/fsex2p00_public.ttf", BaseFont.IDENTITY_H}

    };



    /** Holds he fonts that can be used for the peace message. */
    public FontSelector fs;

     public CreateArabic() {
            fs = new FontSelector();
            for (int i = 0; i < FONTS.length; i++) {
                fs.addFont(FontFactory.getFont(FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED));
            }
        }



    public static void main(String args[]) {

        try {

            // step 1
            Rectangle pagesize = new Rectangle(8.5f * 72, 11 * 72);

            Document document = new Document();//pagesize, 72, 72, 72, 72);// step1

            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("c:\\report.pdf"));
            writer.setInitialLeading(200.5f);

            //writer.getAcroForm().setNeedAppearances(true);
            //writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            document.open();

            FontFactory.registerDirectories();
            Font font = FontFactory.getFont("C:\\damase.ttf",
                    BaseFont.IDENTITY_H, true, 22, Font.BOLD);



            // step 3
            document.open();

            // step 4

             XMLWorkerHelper helper = XMLWorkerHelper.getInstance();

             // CSS
             CSSResolver cssResolver = new StyleAttrCSSResolver();
             CssFile cssFile = helper.getCSS(new FileInputStream(
             "D:\\Itext_Test\\Test\\src\\test.css"));
             cssResolver.addCss(cssFile);

             // HTML
             XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider();


             // fontProvider.addFontSubstitute("lowagie", "garamond");
             CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

             HtmlPipelineContext htmlContext = new HtmlPipelineContext(
             cssAppliers);

             htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

//           // Pipelines
             PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
             HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
             CssResolverPipeline css = new CssResolverPipeline(cssResolver,
             html);


             writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

             System.out.println("RUN DIRECTION --> "+writer.getRunDirection());

             XMLWorker worker = new XMLWorker(css, true);

             XMLParser p = new XMLParser(worker,Charset.forName("UTF-8"));


             String htmlString2 = "<html><body style=\"color:red;\">Hello"+"??"+"</body></html>";
             String htmlString = "<body  style='font-family:arial;'>h"+"??"+"<p  style='font-family:arial;' > ????? </p></body>";
             String html1 ="<html><head></head><body>Hello <p style=\"color:red\" >oo  ??</p>   World! \u062a\u0639\u0637\u064a \u064a\u0648\u0646\u064a\u0643\u0648\u062f \u0631\u0642\u0645\u0627 \u0641\u0631\u064a\u062f\u0627 \u0644\u0643\u0644 \u062d\u0631\u0641 "+htmlString+"Testing</body></html>";



             ByteArrayInputStream is = new ByteArrayInputStream(htmlString.getBytes("UTF-8"));

             p.detectEncoding(is);


             p.parse(is, Charset.forName("UTF-8"));//.parse(is, "UTF-8");//parse(is);//ASMO-708


            // step 5

             document.close();
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }
}

还附加了输出文件。

推荐答案

如文档所述,这不应该有效:

As documented, this is not supposed to work:

writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

阿拉伯语(和希伯来语)只能在 ColumnText的上下文中正确呈现 PdfPCell 。换句话说:如果要使用XML Worker中的阿拉伯语,则需要创建 ElementList ,然后将元素添加到 ColumnText 对象已完成。

Arabic (and Hebrew) can only be rendered correctly in the context of ColumnText and PdfPCell. In other words: if you want to use Arabic from XML Worker, you need to create an ElementList and then add the elments to a ColumnText object as is done here.

您需要在 ColumnText 对象的级别设置运行方向。

You need to set the run direction at the level of the ColumnText object.

这篇关于RTL不能用pdf生成,使用itext 5.5进行阿拉伯语文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:47