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

时间:2014-06-18 20:35:54

标签: android pdf itext

我正在开发一个Android应用程序,我想从我的应用程序中导出一个pdf,其中包含一些简单的文本数据。所以我在我的App中使用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;
                }
             }

和,

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

    }

0 个答案:

没有答案