简体   繁体   English

使用Android PdfDocument API编写PDF文件

[英]Writing PDF File using Android PdfDocument API

I am using following code to write a PDF from View using Android PDFDocument API ( https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html ). 我正在使用以下代码使用Android PDFDocument API( https://developer.android.com/reference/android/graphics/pdf/PdfDocument.html )从View编写PDF。

Code

    /**
     * Writes given PDFDocument using content view.
     *
     * @param pdfDocument PDFDocument to be written.
     */
    private void writePDFDocument(final PdfDocument pdfDocument) {

        // crate a page description
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(PDF_PAGE_WIDTH, PDF_PAGE_HEIGHT, 1).create();

        // start a page
        PdfDocument.Page page = pdfDocument.startPage(pageInfo);

        // draw view on the page
        int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
        contentView.measure(measureWidth, measuredHeight);
        contentView.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
        contentView.draw(page.getCanvas());

        // finish the page
        pdfDocument.finishPage(page);
    }



Problem 问题
If am writing PDF of A4 size. 如果正在编写A4大小的PDF。

So if my View size is more than my PDF size how to automatically write it to second page, now those views gets cuts off if the view size is more than pdf page size. 因此,如果我的视图大小大于PDF大小,则如何自动将其写入第二页,现在,如果视图大小大于pdf页面大小,这些视图将被截断。

Thanks in Advance. 提前致谢。

I think that you have two options here. 我认为您在这里有两个选择。 One is to scale the environment to make the view fit onto one page. 一种是缩放环境以使视图适合一页。 The other is to "tile" or "paginate" the view manually. 另一种是手动“平铺”或“分页”视图。 You would have to calculate how many pages you would need, and then translate the canvas between pages 您将需要计算所需的页面数,然后在页面之间转换画布

contentView.draw(page.getCanvas());
pdfDocument.finishPage(page);
page.getCanvas().translate(595,842);
page = document.startPage(pageInfo);
contentView.draw(page.getCanvas());
//etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM