Reputation: 151
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);
}
Upvotes: 5
Views: 5906
Reputation: 2428
what i did.. so ive just finished with my implementation of this and hit many pit falls, i tried just getting the views drawing cache and writing that to a pdf document, this works however it didnt give me my desired result page size issues centering problems etc, i also ran into a layout too large issue once or twice, what i ended up doing was creating a layout programmatically and working out all my dimensions which works beautifully, (if a lot of work) i use
getResources().getDimensionPixelSize(R.dimen.your_dimen)
to get the right size from dp to pixel and worked out how many views would fit in my page, i made a kind of diagram it looks like this
// This needs to resemble an A4 piece of paper
// <-------- W 2480 -------->
//borderH | | |
// ^ | | |
// 59dp | | | |
// 105px| | | |
// v | | |
// ^ | vertical linear layout | |
// DHH | ______________________________________________ | |
// 32dp | | Date header | | |
// 84px | """""""""""""""""""""""""""""""""""""""""""""" | |
// v | | |
// ^ | | |
// padding | <Horizontal Linear layout> | |
// 16dp | | |
// 42px | | |
// v | ______ ______ ______ ______ | |
// ^ | | | | | | | | | | > H 3508px // overall height
// CHH |<-->| Card |<-->| Card |<-->| Card |<-->| Card |<-->| |
// 118dp |40dp| |16dp| |16dp| |16dp| |40dp| |
// 310px |105px------ 42px ------ 42px ------42px ------105px| |
// v | | |
//borderH | | |
// ^ | | |
// 40dp | | | |
// 105px| | | |
// v |__________________Bottom of page____________________| _|
using this diagram i worked out how many headers or card rows would fit on a page and checked it as i added views, if it was too big a added a new parent linear layout (page) added all these pages to a list of views and then for each page i start a new pdf document page add it to the document and write it to a file
heres the code i ended up with although its still kind of a work in progress
Upvotes: 0
Reputation: 6385
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.
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.
Upvotes: 8