我有一个包含以下数据的活动:
我使用下面的代码从这个视图生成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();
但是如果回收器视图中的行数超过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();
但还是不能让它工作。有人能在这件事上给我指个方向吗?
https://stackoverflow.com/questions/47533036
复制相似问题