In last part we created a static PDF in Android.
But creating PDF from static data has a big disadvantage. You cannot set data at runtime, i.e. you cannot set text from DB or load image from internet.
Creating PDF: Advanced
To solve this problem:
- We will create empty Activity with
LinearLayout
of size [595xwrap_content], we will get reference of thatLinearLayout
and Add our Views programmatically. - We only need above LinearLayout so we can calculate height of views we are about to add. As
View
s created programmatically cannot give height they will take after rendering. i.e. rendering of eachView
is mandatory. - Now we know why we need to first render
View
to layout, let's proceed: - Let's sat we need to add a
TextView
to Pdf, we will call it 'textView1'. - 'textView1' will be added to 595xwrap_content
LinearLayout
, So it can render and we take a note of its height.
private final Map<String, int> viewToHeightMap = new HashMap<>();
final LinearLayout layout595xwrap_content = findViewById(R.id.layout595xwrap_content);
final TextView textView1 = new TextView(context);
layout595xwrap_content.add(textView1);
textView1.post(new Runnable() {
void run() {
layout595xwrap_content.put('textView1', textView1.getHeight())
}
})
- Accordingly add all view to 595xwrap_content
LinearLayout
one-by-one and take a note of height of each view. - Create
LinearLayout
of size 595x842px which will be Single Page in Pdf. Add Views one-by-one until height sum reaches 842px. - When 842px height is reached, again create new LinearLayout of size 595x842px which will be Second Page in the Pdf and so on.
- After you have create List of
LineatLayout
s as Pdf Pages, we simply have to use methods in previous part to generate Pdf.
In Next Part we will generate preview of any Pdf File using PdfRenderer
To make your work easier i have created a Library to generate dynamic PDF using above methods View on Github