【问题标题】:Android - Crops Image while Converting to PDFAndroid - 在转换为 PDF 时裁剪图像
【发布时间】:2017-08-17 05:04:17
【问题描述】:

我使用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();

【问题讨论】:

  • 此行为可能是由于制造商在视图中对绘图缓存的不同实现所致。也许某些三星设备存储图像的缓存较少,这会导致 PDF 内的图像被裁剪。我建议你不要信任drawingCache,而是创建自己的自定义实现将图像转换为PDF
  • 嘿,谢谢@MatPag 那么我们该如何应对这种情况呢?如果我将相同的绘图缓存保存到 jpg 或 png 则图像很好。问题仅与pdf有关
  • 你的图片来源是什么?为什么要从 imageview 中获取它?
  • 因为我们在保存或将其放入 pdf 之前对图像进行了裁剪和编辑
  • 您是否使用库来裁剪和编辑图像?

标签: android image pdf image-processing itext


【解决方案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);

【讨论】:

  • 图像丢失,我应该采用 myImg 的宽度吗?
  • 嘿,谢谢它有效!我想知道我们是否可以删除它周围的边框白色和黑色边框
猜你喜欢
  • 2015-01-22
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多