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

标签 android html css pdf tcpdf

我正在尝试使用 android.graphics.pdf 创建 PDF。我的问题是多页。我可以提供 android.graphics.pdf html,然后可以将其打印为 PDF。现在,如果文本溢出设置的页面大小,那将不起作用。是否可以给它所有的 html,它会根据页面大小的内容创建多个页面?和 TCPDF 一样 :)

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

最佳答案

为此,您需要将 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();
    }
}

关于android - 使用 android.graphics.pdf 创建多页 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42158792/

相关文章:

html - 当浏览器小于内容时添加空格

html - 具有宽度和高度的图像的解释是响应式的

javascript - 元素返回显示后样式丢失

android - 在eclipse中编译android应用程序忽略java错误

android - PhoneGap + Android + jQuery Mobile - 连接到服务器失败

css - Main 元素、Content div 和 wrapper div 之间的区别?

jquery - 创建动画网页的方法

android - Redis 发布/订阅身份验证

java - Android AudioTrack.write 返回 -4

html - 在图像上添加元素