gpt4 book ai didi

Android 从布局 View 创建和打印 pdf

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:47 27 4
gpt4 key购买 nike

我正在尝试从 xml 布局 View 创建 PDF 文件。我在该布局中有一个 ListView ,根据 child 添加项目和设置高度。 PDF 正在创建但未填满整个页面。我试过的是,

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

它的输出就像这张图片,

enter image description here

如何编写覆盖 pdf 页面全宽的布局 View ?

最佳答案

改成这个,

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

这将获得页面的完整高度和宽度。

关于Android 从布局 View 创建和打印 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43699638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com