Android:PrintedPdfDocument Resolution 无效

标签 android pdf resolution

在 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/

相关文章:

ubuntu - GPL 鬼脚本 9.10 : ColorConversionStrategy is incompatible to ProcessColorModel

pdf - 如何在 ColdFusion 的封面中添加批准的图章

c - 使用 C 查找 JPG/JPEG 图像的高度和宽度

css - 分辨率较小,内容尽量适合

java - 如何将警报值传递给警报接收器

Java Servlet : java. lang.ClassNotFoundException : org. json.simple.parser.ParseException

android - 无法创建jvm错误代码4 android studio

android - 通过 Gson 转换异常将 json 转换为 java 对象

python - 如何使用 matplotlib 创建超过 100 个值的可理解条形图?

Android编程——屏幕分辨率