使用android.graphics.pdf创建多页PDF [英] Creating multiple pages of PDF using android.graphics.pdf

查看:288
本文介绍了使用android.graphics.pdf创建多页PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用android.graphics.pdf创建PDF.我的问题是多页.我可以提供android.graphics.pdf html,然后将其打印为PDF.现在,如果文本超出了设置的页面大小,该功能将不起作用.是否有可能全部提供html,并会根据内容(相对于页面大小)创建多个页面?就像TCPDF:)

I am trying to create an PDF using android.graphics.pdf. My issue is with multiple pages. I can give android.graphics.pdf html which could be then printed to a PDF. Now that doesn't work if text overflows the set page size. Is it possible to give it all the html and it would create multiple pages according to the content with respect to the page size? As does TCPDF :)

注意.我试图通过计算内容的高度来避免创建单独的多个页面.

Note. I am trying to avoid creating separate multiple pages by calculating the height of the content.

推荐答案

为此,您需要将 iTextG的jar 添加到您的项目中:

For this you'll need to add the jar of iTextG to your project:

   public void createandDisplayPdf(String text) {

    Document doc = new Document();

    try {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";

        File dir = new File(path);
        if(!dir.exists())
            dir.mkdirs();

        File file = new File(dir, "newFile.pdf");
        FileOutputStream fOut = new FileOutputStream(file);

        PdfWriter.getInstance(doc, fOut);

        //open the document
        doc.open();

        Paragraph p1 = new Paragraph(text);
        Font paraFont= new Font(Font.COURIER);
        p1.setAlignment(Paragraph.ALIGN_CENTER);
        p1.setFont(paraFont);

        //add paragraph to document
        doc.add(p1);    

    } catch (DocumentException de) {
        Log.e("PDFCreator", "DocumentException:" + de);
    } catch (IOException e) {
        Log.e("PDFCreator", "ioException:" + e);
    }
    finally {
        doc.close();
    }

    viewPdf("newFile.pdf", "Dir");
}

// Method for opening a pdf file
private void viewPdf(String file, String directory) {

    File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);
    Uri path = Uri.fromFile(pdfFile);

    // Setting the intent for pdf reader
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(path, "application/pdf");
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    try {
        startActivity(pdfIntent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(TableActivity.this, "Can't read pdf file", Toast.LENGTH_SHORT).show();
    }
}

这篇关于使用android.graphics.pdf创建多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆