使用android.graphics.pdf创建多页PDF

16
我正在尝试使用android.graphics.pdf创建PDF文件。我的问题是关于多页。我可以给android.graphics.pdf HTML,然后将其打印到PDF中。如果文本超出设置的页面大小,那么这种方法就不起作用了。是否可能将所有HTML代码提供给它,并根据页面大小和内容创建多个页面?与TCPDF一样 :)
注意:我正在尝试避免通过计算内容的高度来创建单独的多个页面。

1
嘿,尝试访问这个链接,也许你会找到答案:http://stackoverflow.com/a/36349822/2888952 - Arpan24x7
1
内容溢出时是否会抛出错误? - Michael
1个回答

1

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

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

1
但它只对开源项目免费! - Morten Holmgaard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接