gpt4 book ai didi

Android:PrintedPdfDocument Resolution 无效

转载 作者:行者123 更新时间:2023-11-29 01:31:34 26 4
gpt4 key购买 nike

在 PrintedPdfDocument Canvas 中绘制 View 时,PDF 的字节大小会显着增加,尤其是当 View 包含位图(例如 ImageView)时。

减少最终大小的一种方法应该是 PrintAttributes 中的分辨率字段,示例:

PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.ISO_A4).
setResolution(new Resolution("zooey", PRINT_SERVICE,hDpi,vDpi)).
setMinMargins(Margins.NO_MARGINS).
build();
PdfDocument document = new PrintedPdfDocument(this, printAttrs);

但是,无论我选择 hDpi 还是 vDpi,PDF 的最终大小都不会改变。

我做错了什么吗?如何减小 PDF 的大小?

最佳答案

根据我的经验,分辨率设置不会影响 PrintedPDFDocument 文件生成的最终结果。

下面是PrintedPDFDocument构造函数的源代码。

private static final int POINTS_IN_INCH = 72;

public PrintedPdfDocument(Context context, PrintAttributes attributes) {
MediaSize mediaSize = attributes.getMediaSize();

// Compute the size of the target canvas from the attributes.
mPageWidth = (int) (((float) mediaSize.getWidthMils() / MILS_PER_INCH)
* POINTS_IN_INCH);
mPageHeight = (int) (((float) mediaSize.getHeightMils() / MILS_PER_INCH)
* POINTS_IN_INCH);

// Compute the content size from the attributes.
Margins minMargins = attributes.getMinMargins();
final int marginLeft = (int) (((float) minMargins.getLeftMils() / MILS_PER_INCH)
* POINTS_IN_INCH);
final int marginTop = (int) (((float) minMargins.getTopMils() / MILS_PER_INCH)
* POINTS_IN_INCH);
final int marginRight = (int) (((float) minMargins.getRightMils() / MILS_PER_INCH)
* POINTS_IN_INCH);
final int marginBottom = (int) (((float) minMargins.getBottomMils() / MILS_PER_INCH)
* POINTS_IN_INCH);
mContentRect = new Rect(marginLeft, marginTop, mPageWidth - marginRight,
mPageHeight - marginBottom);
}

您可以看到代码没有使用 DPI 参数,它使用 72 作为固定 DPI 来计算页面宽度/高度,我认为这是错误的。

当我尝试使用 PrintedPDFDocument API 在平板电脑上打印 15 页网页时,我得到了一个 1G 的 PDF 文件。

所以我对你的问题的建议是使用另一个 PDF 生成库,直到 PrintedPDFDocument 稍后证明自己。

祝你好运。

关于Android:PrintedPdfDocument Resolution 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30902727/

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