I want to export data to a PDF. As a test I first want to print just a TextView. This is my code:
public static void exportPdf (Context ctx) {
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1240, 1754, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
LinearLayout content = new LinearLayout(ctx);
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
content.measure(measureWidth, measuredHeight);
content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
TextView tv = new TextView(content.getContext());
tv.setText("Test");
content.addView(tv);
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
// write the document content
...
}
The result is an empty page. What am I doing wrong?
Edit: It is not my goal to write a text on the pdf's canvas. I want to display a Layout. The TextView is just provisory.