简体   繁体   中英

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 ).

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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