如何在android中使用iText将其他语言写入pdf?

如何在android中使用iText将其他语言写入pdf?,android,pdf,itext,Android,Pdf,Itext,我正在开发一个android应用程序,我想从我的应用程序中导出一个pdf,其中包含一些简单的文本数据。所以我在我的应用程序中使用了iText库。我最终可以导出一个带有一些英文文本的pdf,但我想将其他语言编写为pdf,如波斯语。我阅读了关于在iText中编写其他语言的文档,但我无法在我的应用程序中实现它。我该怎么做?这是我的代码: Button pdf = (Button) view.findViewById(R.id.pdf); pdf.setOnClickLi

我正在开发一个android应用程序,我想从我的应用程序中导出一个pdf,其中包含一些简单的文本数据。所以我在我的应用程序中使用了iText库。我最终可以导出一个带有一些英文文本的pdf,但我想将其他语言编写为pdf,如波斯语。我阅读了关于在iText中编写其他语言的文档,但我无法在我的应用程序中实现它。我该怎么做?这是我的代码:

        Button pdf = (Button) view.findViewById(R.id.pdf);
        pdf.setOnClickListener(this);
        @Override
        public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pdf:

        try {
            File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder1");

            if (!newFolder.exists()) {
                newFolder.mkdir();
            }
            try {
                file = new File(newFolder, "MyTest" + ".pdf");
                file.createNewFile();
            } catch (Exception ex) {
                System.out.println("ex: " + ex);
            }
        } catch (Exception e) {
            System.out.println("e: " + e);
        }
        try {

              Document document = new Document();
              PdfWriter.getInstance(document, new FileOutputStream(file));
              document.open();
              fp.addMetaData(document);
              fp.addTitlePage(document);
              fp.addContent(document);
              document.close();
            } catch (Exception e) {
              e.printStackTrace();
            }

        break;
                }
             }
以及


这是我写英文PDF的代码,我想知道如何在代码中添加用波斯语写的代码。您使用的是标准的Type1字体Times Roman。那种字体不知道如何显示波斯语。请使用另一种字体,更具体地说,是一种支持波斯语字形的字体。@Bruno是的,你的权利,我知道这一点,我也尝试为iText创建其他英文字体,但问题是如何在android中创建波斯语字体??在回答这个问题之前,你需要回答这个问题:你有一个包含波斯字形的字体文件吗?如果没有,你不能为波斯语创建字体或基本字体对象。你是什么意思?什么是字体文件?您是在谈论例如arial.TTf吗@布鲁诺洛瓦吉
    public class FirstPdf {
  private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
      Font.BOLD);
  private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
      Font.BOLD);
  private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
      Font.BOLD);
  BaseFont tahoma;
  Font myfont;

public FirstPdf(){

  }

  // iText allows to add metadata to the PDF which can be viewed in your Adobe
  // Reader
  // under File -> Properties
  public void addMetaData(Document document) {
    document.addTitle("My PDF Export");
    document.addSubject("Using Daryan Co Application");
    document.addKeywords("Java, PDF, iText");
    document.addAuthor("Darian Co");
    document.addCreator("Simple User1");
  }

  public void addTitlePage(Document document)
      throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);
    // Lets write a big header
    preface.add(new Paragraph(b, myfont));
    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Report generated by: " + "User1" + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        smallBold));
    addEmptyLine(preface, 3);
    //preface.add(new Paragraph("This document describes something which is very important ",
    //   smallBold));

   // addEmptyLine(preface, 8);

    //preface.add(new Paragraph("This document is a preliminary version and not         subject to your license agreement or any other agreement with vogella.com ;-).",
    //    redFont));

    document.add(preface);
    // Start a new page
    //document.newPage();
  }

  public void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("Application Output", catFont);
    anchor.setName("Application Output");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    // add a list
    //createList(subCatPart);
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // add a table
    createTable(subCatPart);

    // now add all this to the document
    document.add(catPart);
  }

  private static void createTable(Section subCatPart)
      throws BadElementException {
    PdfPTable table = new PdfPTable(2);
    PdfPCell c1 = new PdfPCell(new Phrase("Parameter"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Value"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    table.setHeaderRows(1);
    table.addCell("City: ");
    table.addCell(""+ Data.p1city);
    table.addCell("A: " );
    table.addCell(""+ Data.p1result);
    table.addCell("Hight: ");
    table.addCell(""+ Data.p2h);
    table.addCell("Structure Type: ");
    table.addCell(""+Data.p2structureType);
    table.addCell("T: ");
    table.addCell("" + Data.p2period);
    table.addCell("B: ");
    table.addCell(""+ Data.p3b);
    subCatPart.add(table);

  }

    private static void createList(Section subCatPart) {
    List list = new List(true, false, 10);
    list.add(new ListItem("First point"));
    list.add(new ListItem("Second point"));
    list.add(new ListItem("Third point"));
    subCatPart.add(list);
  }

  private static void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
      paragraph.add(new Paragraph(" "));
    }
  }

    }