Android - 转换为PDF时裁剪图像

时间:2017-03-24 10:01:08

标签: android image pdf image-processing itext

我使用com.itextpdf:itextg将图像转换为pdf时有点奇怪的情况,它正在裁剪图像并仅占用它的25%。它在其中一部手机中运行良好,但在其他手机中无限期播种,主要是三星系列

下面是我的代码

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
ImageView iv = (ImageView) this.findViewById(R.id.imageViewNamed);
iv.buildDrawingCache(true);
Bitmap img = iv.getDrawingCache(true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
document.add(myImg);
stream.close();
document.close();

1 个答案:

答案 0 :(得分:1)

使用以下代码

Image myImg = Image.getInstance(stream.toByteArray());
myImg.scaleToFit(PageSize.A4);
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                - document.rightMargin() - 0) / image.getWidth()) * 100;

myImg.scalePercent(scaler);
document.add(myImg);
相关问题