1

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.

TGr
  • 36
  • 3
  • Possible duplicate of [Creating a pdf file in android programmatically and writing in it](https://stackoverflow.com/questions/34296149/creating-a-pdf-file-in-android-programmatically-and-writing-in-it) – Robert Apr 22 '18 at 18:20
  • I don't just want to write to the file but draw a LinearLayout and I don't get an error message – TGr Apr 22 '18 at 18:29

0 Answers0