使用Android PdfDocument API创建PDF-文件大小太大
我尝试使用使用Android PdfDocument API创建PDF-文件大小太大,android,pdf,Android,Pdf,我尝试使用PdfDocumentAPI制作PDF。这就是我尝试过的: Paint paint = new Paint(); // create a new document PdfDocument document = new PdfDocument(); FileOutputStream fos = new FileOutputStream(pdfFile, true); for (int i = 0; i < mImagePathList.s
PdfDocument
API制作PDF。这就是我尝试过的:
Paint paint = new Paint();
// create a new document
PdfDocument document = new PdfDocument();
FileOutputStream fos = new FileOutputStream(pdfFile, true);
for (int i = 0; i < mImagePathList.size(); i++) {
//current image path
String imagePath = mImagePathList.get(i);
//get a bitmap
Bitmap bitmap = createBitmap(imagePath); // This method returns a bitmap from path
Drawable d = new BitmapDrawable(getResources(), bitmap);
d.setBounds(0, 0, 100, 100);
// crate a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), i + 1).create();
// start a pagegit
PdfDocument.Page page = document.startPage(pageInfo);
// get the page canvas
Canvas pdfCanvas = page.getCanvas();
if (pdfCanvas != null) {
// draw bitmap on the page
//pdfCanvas.draw(d, 0, 0, paint);
d.draw(pdfCanvas);
} else {
Log.e(TAG " Canvas null!");
}
paint.reset();
// finish the page
document.finishPage(page);
}
// write the document content
document.writeTo(fos);
document.close();
//publishProgress(i);
fos.close();
我看过很多这样的帖子,但大多数都使用第三方库来解决这个问题。我想继续使用Android的PdfDocument
如果我插入5MB的图像,则生成的PDF文件大小为45MB。它通常会乘以10,有时甚至是20。读入位图时可以缩小位图的比例吗?在这种情况下,逃跑是没有帮助的。我试过同样的代码。我试过缩小图像的比例,但没有效果。问题是,即使我把图像的大小降低到原来的20Kb,生成的pdf文件也只有大约600到900KB。我还尝试用空白画布创建pdf(没有绘制位图),pdf大小为1-2Kb。“drawBitmap”方法似乎有些问题,但我不确定。请提个建议,我也有同样的问题。pdf是巨大的!你找到解决办法了吗?我也有同样的问题。你如何解决这个问题?
//get a bitmap
Bitmap bitmap = createBitmap(imagePath);
pdfCanvas.drawBitmap(bitmap, 0, 0, null); //passing paint object instead of null have no effect