【问题标题】:how to create Persian PDF with iText如何使用 iText 创建波斯语 PDF
【发布时间】:2016-01-26 22:34:04
【问题描述】:

我知道以前可能有很多人问过这个问题。我已经阅读了几乎所有的内容`但它不能帮助我解决我的问题。 我正在使用 iText java 库生成波斯语 PDF。我正在使用以下

如何使用PdfWriter.RUN_DIRECTION_RTL

代码:

String ruta = txtruta.getText();
    String contenido= txtcontenido.getText();
    try {
        FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
        Document doc = new Document(PageSize.A4,50,50,50,50);
          PdfWriter.getInstance(doc, archivo);
          doc.open();
          BaseFont bfComic = BaseFont.createFont("D:\\Font\\B Lotus.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
          Font font = new Font(bfComic, 12,Font.NORMAL);
           doc.add(new Paragraph(contenido,font));
       doc.close();
       JOptionPane.showMessageDialog(null,"ok");

    } catch (Exception e) {
        System.out.println("Eroor"+e);    
    }

输出: Problem

【问题讨论】:

  • 你能问出问题正文中的确切问题吗?您对问题的解释越多,获得帮助的机会就越大:) 祝您好运!
  • 我想在 pdf 中保存时使用文本 RTL
  • 我现在的问题是 RTL 文本

标签: java pdf netbeans


【解决方案1】:

Document.add() 不支持 RTL 文本。您必须使用ColumnText.setRunDirectionPdfPTable.setRunDirection

【讨论】:

  • 我不知道使用 ColumnText.setRunDirection,PdfPTable.setRunDirection 请帮助我。请帮我写代码谢谢
【解决方案2】:

我没有使用过波斯语。但是,我认为您的问题在于您使用的字体 (B Lotus.ttf)。在大多数情况下,使用注册的 Unicode 字体可以解决问题。请使用其他字体重试。

您还可以使用以下代码 RTL 文本短语。

PdfPCell pdfCell = new PdfPCell(new Phrase(contenido, myUnicodePersianFont));
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

你会发现similar question here

【讨论】:

  • 如果您发现此答案可以解决您的问题,请对答案进行投票或标记为“已接受”。老实说,我在这里寻找网站上的一些声誉。 :-)
【解决方案3】:

我成功了

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

      JFileChooser dlg = new JFileChooser();
               int option = dlg.showSaveDialog(this);
               if(option==JFileChooser.APPROVE_OPTION){
                  File f = dlg.getSelectedFile();
                  txtaddress.setText(f.toString());
               }


}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         


    String ruta = txtaddress.getText();
    String con= content.getText();
    try {
          FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
          Document doc = new Document(PageSize.A4,50,50,50,50);
          PdfWriter Writer = PdfWriter.getInstance(doc, archivo);
          doc.open();
          LanguageProcessor al = new ArabicLigaturizer();
          Writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
          BaseFont bfComic = BaseFont.createFont("D:\\Font\\titr.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
          Font font = new Font(bfComic, 12,Font.NORMAL);
          Paragraph p = new Paragraph(al.process(con),font);
          p.setAlignment(Element.ALIGN_RIGHT);
          doc.add(p);
          doc.close();
          JOptionPane.showMessageDialog(null,"Yes");

    } catch (Exception e) {
           System.out.println("Eroor"+e);    
    }




}     

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多