2

monodroid で pdf ファイルを作成するためのソリューションを探しています。また、内容を記入する PDF 形式の場合もあります。pdfsharp_on_mono や itextsharp などのさまざまなライブラリを試しましたが、うまくいきません。新しい空の pdf ファイルを作成することは問題ありません。しかし、コンテンツを入力しようとすると、常にエラーが発生します。

私の目標は、後でxmlファイルに記入する必要があるPDFファイルを作成することです。現時点では、pdf を作成して何かを「書き込む」ことができれば幸いです。どうすればそれを実現できるか、ヒントはありますか?私はモノドロイドの初心者です。

コードやエラー メッセージが必要な場合は、そのまま言ってください。私はさまざまな解決策を試しました。乾杯アンナ

ps: 下手な英語でごめんなさい。

4

2 に答える 2

0

iText ライブラリを使用して、Android アプリケーションで PDF ファイルを作成できます。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CreatePdf extends ActionBarActivity implements OnClickListener {

private static String FILE = Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/filename.pdf";
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);
Button createPdf;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_pdf);
    createPdf = (Button) findViewById(R.id.createBtn);
    createPdf.setOnClickListener(this);

}

private void createPdf() {
    try {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        addContent(document);
        document.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

private static void addContent(Document document) throws DocumentException {

    Anchor anchor = new Anchor("Anchor", catFont);
    anchor.setName("Hello PDF");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(0);

    Paragraph subPara = new Paragraph("Android PDF Created", subFont);
    addEmptyLine(subPara, 1);
    Section subCatPart = catPart.addSection(subPara);

    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(4);
    PdfPCell c1 = new PdfPCell(new Phrase("Cell 1"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Cell 2"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Cell 3"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Cell 4"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    subCatPart.add(table);
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.create_pdf, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.createBtn:

        createPdf();
        break;

    default:
        break;
    }
}
}
于 2014-10-11T10:59:59.373 に答える
0

Android アプリで PDF を作成する方法は?

Javaの既存のソリューションを使用してpdfですべてのジョブを実行するJavaクラスを作成し、JNIEnvを使用してプロキシクラスを作成し、マネージドコードで使用できます。

于 2011-09-14T15:32:38.093 に答える