【问题标题】:Android: Create identical Views for drawing on Canvas of PdfDocument for printingAndroid:创建相同的视图以在 PdfDocument 的画布上绘图以进行打印
【发布时间】:2020-05-30 09:18:57
【问题描述】:

我正在使用从 XML 膨胀的视图在 PdfDocument.Page 的画布上绘图。 问题是,生成的 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 on phone 1

PDF on phone 2

【问题讨论】:

    标签: android pdf printing view


    【解决方案1】:
    Configuration config = sourcecontext.getResources().getConfiguration();
    config.densityDpi = DisplayMetrics.DENSITY_HIGH;
    config.setTo(config);
    Context context sourcecontext.createConfigurationContext(config);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2014-03-29
      • 2017-09-27
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多