安卓:PrintedPdfDocument 分辨率无效

3
当将视图绘制到PrintedPdfDocument画布中时,PDF文件的大小(以字节为单位)可能会明显增加,特别是当视图包含位图(例如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文件大小?


相关链接:https://dev59.com/so3da4cB1Zd3GeqP5t10#37635559 - JJD
2个回答

1

最佳解决方案是在为PDF生成设计视图时使用像素而不是dp和sp。


1
基于我的经验,分辨率设置不会影响生成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文件。
因此,我的建议是在PrintedPDfDocument证明自己之前,使用另一个PDF生成库。
祝好运。

private static final int MILS_PER_INCH = 1000; - nyconing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接