我的活动包含以下数据:
我使用以下代码从此视图生成pdf:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(0, 0);
Document document = new Document(image);
PdfWriter.getInstance(document, new FileOutputStream(filePath));
document.open();
document.add(image);
document.close();
但如果我在recyclerview中有超过20行,我会收到此错误:
exceptionconverter: com.itextpdf.text.documentexception: the page size must be smaller than 14400 by 14400. it's 1080.0 by 25288.0
这是因为图像的高度超过了最大页面大小14400.我得到了那个部分。
但我想知道如果图像大小超过页面大小,如何将图像分割成两页。
我尝试了以下代码:
float width = image.getScaledWidth();
float height = image.getScaledHeight();
Rectangle page = new Rectangle(width, height / 2);
Document document = new Document(page);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
document.open();
PdfContentByte canvas = writer.getDirectContentUnder();
canvas.addImage(image, width, 0, 0, height, 0, -height / 2);
document.newPage();
canvas.addImage(image, width, 0, 0, height, 0, 0);
document.newPage();
canvas.addImage(image, width, 0, 0, height, -width / 2, - height / 2);
document.newPage();
canvas.addImage(image, width, 0, 0, height, -width / 2, 0);
document.close();
但仍然无法让它发挥作用。有人能指出我正确的方向吗?
答案 0 :(得分:0)
这可能会也可能不会奏效。试一试。 使用以下方法调整位图大小
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}