Android:PrintedPdfDocument Resolution无效

问题描述 投票:2回答:2

将视图绘制到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尺寸?

android pdf resolution
2个回答
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文件。

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

祝好运。


0
投票

最好的解决方案是使用pixles而不是dp和sp来设计PDF生成视图

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