安卓。在PdfDocument的画布上创建相同的视图进行打印

问题描述 投票:0回答:1

我使用XML中的Views来绘制PdfDocument.Page.的Canvas,问题是,生成的PDF在不同的设备上看起来是不一样的(文字大小,高度和宽度),这取决于屏幕大小等。问题是,生成的PDF在不同的设备上看起来是不同的(文字大小,高度和宽度),取决于屏幕大小等。

是否有一种方法可以创建相同的视图,不管屏幕大小是什么,所以我得到相同的PDF。Dp, sp, px, pt都以某种方式尊重物理设备.也许可以在Context中覆盖一些屏幕尺寸的值等等?

这是我生成PDF文档的代码。

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pdf_person_container, null);

PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595,842,1).create();
PdfDocument.Page page = document.startPage(pageInfo);

int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
view.measure(measureWidth, measuredHeight);
view.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

view.draw(page.getCanvas());

document.finishPage(page);
document.writeTo(new FileOutputStream(filePath + "Detail.pdf"));

手机上的PDF文件 1

PDF手机2

android pdf printing view
1个回答
0
投票
Configuration config = sourcecontext.getResources().getConfiguration();
config.densityDpi = DisplayMetrics.DENSITY_HIGH;
config.setTo(config);
Context context sourcecontext.createConfigurationContext(config);

并使用这个新的上下文为Inflater。

© www.soinside.com 2019 - 2024. All rights reserved.