3

我有在 itext 5.5 和 xmlworker jar 的帮助下编写阿拉伯字符的 java 代码,但即使在使用writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL)之后,它的书写也是从左到右。

使用的代码是:

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

        }

    }
}

输出文件也附上。PDF 输出

4

3 回答 3

6

如文件所述,这不应该起作用:

writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

阿拉伯语(和希伯来语)只能在 和 的上下文中正确ColumnText呈现PdfPCell。换句话说:如果你想使用 XML Worker 中的阿拉伯语,你需要创建一个ElementList元素,然后将元素添加到一个ColumnText对象中,就像这里所做的那样。

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

于 2014-05-27T12:39:35.177 回答
2

//这个解决方案对我有用::)

// document
        Document document = new Document(PageSize.LEGAL);

        //fonts
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("/Users/ibrahimbakhsh/Library/Fonts/tradbdo.ttf", BaseFont.IDENTITY_H);
        fontProvider.register("/Users/ibrahimbakhsh/Library/Fonts/trado.otf", BaseFont.IDENTITY_H);
        fontProvider.register("/Users/ibrahimbakhsh/Library/Fonts/tahoma.ttf", BaseFont.IDENTITY_H);
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // CSS
        CSSResolver cssResolver =
                XMLWorkerHelper.getInstance().getDefaultCssResolver(true);



        // Pipelines
        ElementList elements = new ElementList();
        ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
        HtmlPipeline html = new HtmlPipeline(htmlContext, end);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // HTML
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        htmlContext.autoBookmark(false);


        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new FileInputStream(HTML));


        //writer
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12.5f);
        writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);


        // step 4
        document.open();


        // step 5

        for (Element e : elements) {
            //out.println(e.toString());
            if(e instanceof PdfPTable){
                PdfPTable t = (PdfPTable) e;
                t.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
                ArrayList<PdfPRow> rows = t.getRows();
                for(PdfPRow row:rows){
                    PdfPCell[] cells = row.getCells();
                    for(PdfPCell cell:cells){
                        cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
                    }
                }
                e = t;
            }
            document.add(e);
        }


        //try adding new table 

        PdfPTable table = new PdfPTable(1);
        table.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        Font f = new Font(BaseFont.createFont("/Users/ibrahimbakhsh/Library/Fonts/trado.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
        PdfPCell cell = new PdfPCell(new Paragraph("تجربة نص عربي",f));
        table.addCell(cell);
        document.add(table);

        // step 6
        document.close();
于 2014-11-07T18:48:06.297 回答
1

对于需要简单解决方案的开发人员

我使用了这个技巧,输出非常干净漂亮!

  1. 创建一个PDFPTable带有1
  2. 对于内容的每一段,创建一个Paragraph对象并将其对齐设置为Paragraph.ALIGN_JUSTIFIED(我不知道为什么,但它会导致段落对齐到页面右侧!!!)
  3. 使用创建一个PDFPCell并删除其边框setBorder(Rectangle.NO_BORDER)并将段落添加到单元格
  4. 将单元格添加到表格中

这是一个方便您的代码示例。

public void main(){
    /*
    * create and initiate document
    * */
    // repeat this for all your paragraphs
    PdfPTable pTable = new PdfPTable(1);
    Paragraph paragraph = getCellParagraph();
    paragraph.add("your RTL content");
    PdfPCell cell = getPdfPCellNoBorder(paragraph);
    pTable.addCell(cell);

    // after add all your content
    document.add(pTable);
}

private Paragraph getCellParagraph() {
    Paragraph paragraph = new Paragraph();
    paragraph.setAlignment(Paragraph.ALIGN_JUSTIFIED);
    // set other styles you need like custom font
    return paragraph;
}

private PdfPCell getPdfPCellNoBorder(Paragraph paragraph) {
    PdfPCell cell = new PdfPCell();
    cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
    cell.setPaddingBottom(8);
    cell.setBorder(Rectangle.NO_BORDER);
    cell.addElement(paragraph);
    return cell;
}
于 2017-07-10T16:54:00.027 回答