- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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/
在 PrintedPdfDocument Canvas 中绘制 View 时,PDF 的字节大小会显着增加,尤其是当 View 包含位图(例如 ImageView)时。 减少最终大小的一种方法应该是
我正在尝试根据用户在我的 Android 应用程序中显示的表单创建 PDF 文档。我使用以下代码创建 PDF 文件,然后将其附加到电子邮件中: public static File generate(
我正在使用Android的内置PrintedPdfDocument使用 Canvas 绘制 PDF,如 this 中所述文档的示例代码: private void drawPage(PdfDocume
我正在尝试将 bmp 图像保存为 pdf: @Override protected void onActivityResult(int requestCode, int resultCode,
在遇到许多问题之后,我终于能够使用 PrintedPDFClass 生成具有内容且未损坏的 PDF 文档。 但是,问题是 PDF 中的内容似乎与我给它呈现的 View 没有任何关系...... 我要渲
有没有办法用一些模板打开现有的 PDF 文件并继续在其上绘图? 我正在使用https://developer.android.com/reference/android/print/pdf/Print
我经历过这个link创建一个包含文本的 sample.pdf 文件。 但由于此 API 提供了仅使用 Canvas 类来编写文本的方法。所以我无法从生成的 sample.pdf 文件(因为它被视为图像
我正在做Android的打印自定义文件,在PrintedPdfDocument的开发者网站上有一些疑惑 // open a new document PrintedPdfDocument doc
我正在尝试在 Android 上创建多页 PDF 文件。这是导致问题的我的代码的一部分: document.finishPage(page); // new page page
我有这个布局,我想将屏幕上的所有内容打印成 PDF。包括 ScrollView 中的所有内容。稍后我还将尝试使用 IText 来执行此操作,但现在我想看看我是否可以使用 PrintedPdfDocum
我是一名优秀的程序员,十分优秀!