开发者

create or fill a pdf with monodroid

I'm looking for a solution for creating a pdf-file with monodroid. It might be also a pdf-form in which I would fill in the content. I tried different librarys like pdfsharp_on_mono or itextsharp, but it doesn't work. Creating a new empty pdf-file is no problem. But when I try to fill in content, there are always errors.

My goal is to have a PDF-file, which at a later time should be filled through a xml-file. At the moment I would be happy if I just could create a pdf and "write" something in it. Has anyone a hint, how I can realize it? I'm a really noob in monodroid.

If you need code or error messages, just say. I have tried different solutions. cheers anna

ps: sorry for my开发者_JAVA百科 bad english.


How to create PDFs in an Android app?

You can write java class that will do all job with pdf using existing solutions for java, and then using JNIEnv create proxy class for it and use it in managed code.


Using iText library we can create pdf file in android application.

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;
    }
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜