3

我正在尝试使用来自 Itext 5.5 和 XMLWorker 的阿拉伯文本转换 HTML 字符串。转换后,阿拉伯字符显示为空白。

使用的代码片段如下:

public class CreateArabic {

    public static void main(String args[]) {

        try {

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

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

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

            writer.getAcroForm().setNeedAppearances(true);

            document.open();

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


            document.open();



             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.getFont("C:\\damase.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
             fontProvider.register("C:\\damase.ttf");


             CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
             HtmlPipelineContext htmlContext = new HtmlPipelineContext(
             cssAppliers);
             htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

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

             XMLWorker worker = new XMLWorker(css, true);

             XMLParser p = new XMLParser(worker);

                         String htmlString = "<html><head></head><body>"+"اب"+"</body></html>";

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

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



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

        }

    }
}
4

1 回答 1

2

我有同样的问题,只是不同的是我使用了土耳其字体并且丢失了:-

请参阅我的答案解决方案

希望对你有帮助 问候,

于 2014-05-26T13:28:56.713 回答