生成pdf Android的问题

时间:2011-09-20 20:19:54

标签: android pdf-generation

我无法创建PDF文件并将其保存在SD上,并且希望有人建议我做什么,按照我开始做的事情,如果他们有任何示例,我很感激送我!

我的课程试图生成PDF:

public class TestActivity extends Activity {

    private static String FILE = "sdcard/exaple.pdf";
    private static final String TAG = "documentPDF"; 

/**
 * @see android.app.Activity#onCreate(Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tela_relatorio);
    File Directory = new File(FILE); 
    try { 
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(Directory));
        document.open();
        document.addAuthor("Name Author");
        addTitlePage(document);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

private void addTitlePage(Document document)throws DocumentException {
    Paragraph preface = new Paragraph();
    preface.add(new Paragraph("Document Title", null));
    document.add(preface);
document.newPage();
}
}

2 个答案:

答案 0 :(得分:0)

编辑:此外,您是否将此库用于pdf功能? http://sourceforge.net/projects/apwlibrary/

我会对此采取行动并尝试尽我所能地回答。一些注意事项可以真正帮助您的应用程序开发首先使用正确的代码间距。由于这是一个Android问题,我想你是在Eclipse中开发,但是一个鲜为人知但又很棒的快捷方式是输入Ctrl+A来选择你的所有代码,然后使用Ctrl+I来正确对齐你的代码这样它更具可读性。在这种情况下,我能够找到一些 MIGHT 不是你想要的东西

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tela_relatorio);
    File Directory = new File(FILE); 
    try { 
        //so here you  call your variable "document" but later...
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(Directory));
        // you reference it as documento These are not the same but it looks like they should be
        documento.open();
        document.addAuthor("Name Author");
        addTitlePage(document);
        document.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
} 

private void addTitlePage(Document document)throws DocumentException {
    Paragraph preface = new Paragraph();
    preface.add(new Paragraph("Document Title", null));
    //once again, what is this supposed to reference to?
    documento.add(preface);
    documento.newPage();

}      

所以,我不太了解所涉及的特定图书馆提供的建议比现在还多,但是再一次,你可能不想要的那些东西。另一个问题是你的try语句没有包含足够的代码。 :)仔细看看你想要做什么,然后考虑应该怎么做。

无论如何,我认为这应该至少摆脱任何编译时错误,只要你有适当的导入。哦,如果您使用Eclipse,另一个方便的快捷方式是Ctrl+Shift+I它将管理您的import语句并帮助您找到需要导入的软件包。从那里,您将需要找到与编译错误无关的任何错误

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tela_relatorio);
    File Directory = new File(FILE); 
    try { 
        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(Directory));
        document.open();
        document.addAuthor("Name Author");
        addTitlePage(document);
        document.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
} 

private void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    preface.add(new Paragraph("Document Title", null));
    document.add(preface);
    document.newPage();

}

答案 1 :(得分:0)

工作人员能够在android中生成PDF,我的代码已经从数据库中生成了pdf报告! 再见!

        public ArrayList<Visitantes> RelatorioPDF() {
    Document documento = new Document();
    ArrayList<Visitantes> lista = null;
    try {String sql_innerJoin = "SELECT Visitantes.id, Visitantes.nome, Visitantes.cpf, Visitantes.contato, Visitantes.foto"
                + " FROM Visitantes ";

        Cursor c = getReadableDatabase().rawQuery(sql_innerJoin, null);
        // Cursor c = getReadableDatabase().query(t,VisitantesDB.COLUNAS,
        // null, null, null, null, null,null);
        PdfWriter.getInstance(documento, new FileOutputStream(Environment.getExternalStorageDirectory()+ File.separator +"testeVisitante.pdf"));
        documento.open();
        documento.addAuthor("Tiago");

        lista = new ArrayList<Visitantes>();

        while (c.moveToNext()) {
            Visitantes visitante = new Visitantes();
            visitante.setId(c.getInt(0));
            visitante.setCpf(c.getString(1));
            visitante.setNome(c.getString(2));
            visitante.setContato(c.getString(3));
            visitante.setFoto(c.getString(4));
            documento.add(new Paragraph("Id:"+visitante.getId()));
            documento.add(new Paragraph("CPF:"+visitante.getCpf()));
            documento.add(new Paragraph("nome:"+visitante.getNome()));
            documento.add(new Paragraph("Contato:"+visitante.getContato()));
            documento.add(new Paragraph("|____________________________________|"));

            lista.add(visitante);
        }
        c.close();
        documento.addCreationDate();
        documento.close();
    } catch (Exception e) {
        Log.e("erro", e.toString());

    }
    return lista;
}