Android生成pdf的问题

发布于 2024-12-05 20:54:58 字数 1060 浏览 0 评论 0原文

我在创建 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();
}
}

I'm having trouble creating a PDF file and save it on SD, and would like someone suggested to me what to do, follow what I started doing, if they have any examples, I am grateful for sending me!

My class that tries to generate the 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();
}
}

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

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

发布评论

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

评论(2

高速公鹿 2024-12-12 20:54:58

编辑:另外,您是否使用此库来实现 pdf 功能? http://sourceforge.net/projects/apwlibrary/

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

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 它将管理您的导入语句并帮助您找到需要导入的包。从那里,您将需要找到与编译错误无关的任何错误

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

}

Edit: Also, are you using this library for your pdf functionality? http://sourceforge.net/projects/apwlibrary/

I'm gonna take a shot at this and try to answer the best I can. A few notes though that could really help your application development is to first use proper code spacing. Since this is an Android question, I imagine that you are developing in Eclipse, but a little known, yet awesome shortcut is to type Ctrl+A to select all your code then use Ctrl+I which will properly align your code so that it is more readable. In this case, I was able to find out a few things that MIGHT not be what you wanted

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

}      

So, I don't know enough about the specific libraries involved to offer much more advice than that yet, but once again, there are somethings in there that you might not want. The other problem was your try statement wasn't surrounding enough code there. :) Look closely at what you are trying to do, and then think about how that should be done.

Anyway, I think this should at minimum get rid of any compile-time errors, provided that you have the appropriate imports. Oh, another handy shortcut if you are using Eclipse is Ctrl+Shift+I It will manage your import statements and help you find that packages you need to import. From there, you will need to find any bugs that aren't related to compile errors

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

}
吲‖鸣 2024-12-12 20:54:58

工作人员能够在 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;
}

Staff was able to generate a PDF in android, there is my code already generating a pdf report from the database!
bye!

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