使用 monodroid 创建或填充 pdf

发布于 2024-12-02 23:12:17 字数 332 浏览 3 评论 0原文

我正在寻找使用 monodroid 创建 pdf 文件的解决方案。它也可能是一个 pdf 表格,我可以在其中填写内容。我尝试了不同的库,例如 pdfsharp_on_mono 或 itextsharp,但它不起作用。创建一个新的空 pdf 文件没有问题。但当我尝试填写内容时,总是出现错误。

我的目标是拥有一个 PDF 文件,稍后应通过 xml 文件填充该文件。目前,如果我能创建一个 pdf 并在其中“写”一些东西,我会很高兴。 有谁提示,我怎样才能实现它?我真的是 monodroid 中的菜鸟。

如果您需要代码或错误消息,请直接说明。我尝试过不同的解决方案。 干杯 安娜

PS:抱歉我的英语不好。

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 bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-12-09 23:12:17

如何在 Android 应用程序中创建 PDF?

您可以编写 java 类这将使用现有的 java 解决方案完成 pdf 的所有工作,然后使用 JNIEnv 为其创建代理类并在托管代码中使用它。

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.

极致的悲 2024-12-09 23:12:17

使用 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;
    }
}
}

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;
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文