Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - PdfDocument - breaking a LinearLayout into multiple pdf pages

Tags:

android

pdf

I have an android linear layout which I want to convert as a PDF. Many a times, this PDF would span multiple pages. While I am able to convert the content into a single page PDF, have some difficulty in breaking the PDF into a multi-page one. I have given the relevant code block below, hard coded some numbers for simplicity sake. Here, PdfDocument.PageInfo.Builder takes 3 arguments - width, height and page number (earlier, it used to accept new Rect(0,0,800,100) which is not working any longer in API 23). Now, when I iterate through the for loop, I am not sure how I can get different content for a multi-page PDF. The current piece of code given below generates same content for every page, since it is based on width and height and not based on co-ordinates.

    PdfDocument.PageInfo pageInfo;
    int noOfPages = (int)Math.floor(content.getHeight()/1000)+1;
    for (int i=1;i<=noOfPages;i++) {
        pageInfo = new PdfDocument.PageInfo.Builder
                (800,1000,i).create();
        PdfDocument.Page page = document.startPage(pageInfo);
        content.draw(page.getCanvas());
        document.finishPage(page);
    }
like image 715
Sankararaman Santhanaraman Avatar asked Feb 03 '17 11:02

Sankararaman Santhanaraman


1 Answers

Its been more than 2 years, since i last worked on PDFDocument stuff, so sorry i don't have exact answer for your question, but i will share my little knowledge on it, hope it will help you.

  1. In Android PDFDocument, we will give View for individual PDFPage.
  2. While creating PDFPage, we will Width & Height for PDFPage (598px * 842px for A4 size).
  3. Hence View you give to write PDFPage should be also the same size as PDFPage size.
  4. So, i think its our responsible to create View's for individual page with correct size (width & height) matching PDFPage size (Width & Height).
  5. PDFDocument is dump, what ever view you give for a PDFPage it will write, hence it is our responsible to create view for individual page & give it to the PDFDocument.

Sample

Look at the code which i wrote 2Years back, which may help you - https://gist.github.com/apvasanth03/ed903535aed12c93e30b102d9596c399

Note

It will be difficult, how to break your view into different page's if it contains a TextView with several lines. I don't have answer for it.

Kindly, share if you find any solution.

like image 198
Vasanth Avatar answered Nov 11 '22 04:11

Vasanth