Android从布局视图创建和打印pdf [英] Android create and print pdf from layout view

查看:334
本文介绍了Android从布局视图创建和打印pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从xml布局视图创建PDF文件. 我在该布局中有一个listview,根据子项添加项并设置高度. PDF正在创建,但无法填充整个页面. 我尝试过的是

I am trying to create PDF file from xml layout view. I have a listview in that layout, adding items and setting height based on child. PDF is creating but not filling the whole page. What I have tried is,

 PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();

        // start a page
        PdfDocument.Page page = document.startPage(pageInfo);


        // draw something on the page
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.pdf_layout, null);

        content.measure(2250, 1400);
        content.layout(0,0, 2250, 1400);

        tvName = (TextView)content.findViewById(R.id.tvName);
        tvDate = (TextView)content.findViewById(R.id.tvDate);
        tvAge = (TextView)content.findViewById(R.id.tvAge);
        tvGender = (TextView)content.findViewById(R.id.tvGender);
        tvPhone = (TextView)content.findViewById(R.id.tvPhone);
        lvList = (ListView)content.findViewById(R.id.lvList);
        lvList.setAdapter(adapter);
        Utils.setListViewHeight(lvList, CreatePDFDemo.this);

        tvName.setText(name);
        tvAge.setText(age + "Y");
        tvGender.setText(gender);
        tvPhone.setText(phone);

        content.draw(page.getCanvas());

        // finish the page
        document.finishPage(page);
        // add more pages
        // write the document content
        try {
            document.writeTo(output);
        } catch (IOException e) {
            e.printStackTrace();
        }

它的输出就像这张图片,

This its output is like this image,

如何编写覆盖pdf页面整个宽度的布局视图?

How can I write layout view covering full width of pdf page?

推荐答案

更改为此,

        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());

这将获得页面的全部高度和宽度.

This will get page full height and width.

这篇关于Android从布局视图创建和打印pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆