动态创建布局并将其另存为PDF

时间:2018-04-22 17:32:03

标签: java android pdf dynamically-generated

我想将数据导出为PDF。作为测试,我首先要打印一个TextView。这是我的代码:

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
    ...
}

结果是一个空页面。我做错了什么?

编辑:我不打算在pdf的画布上写一个文字。我想显示一个布局。 TextView只是附带条件。

0 个答案:

没有答案