【问题标题】:Android: PrintedPdfDocument Resolution has no effectAndroid:PrintedPdfDocument 分辨率没有效果
【发布时间】:2015-06-17 21:30:20
【问题描述】:

在 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


【解决方案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 稍后证明自己。

祝你好运。

【讨论】:

  • private static final int MILS_PER_INCH = 1000;
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多