首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从图像创建PDF -使用ItextPdf的安卓

从图像创建PDF -使用ItextPdf的安卓
EN

Stack Overflow用户
提问于 2017-11-28 21:41:06
回答 0查看 418关注 0票数 0

我有一个包含以下数据的活动:

我使用下面的代码从这个视图生成pdf:

代码语言:javascript
运行
复制
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行,我会得到这样的错误:

代码语言:javascript
运行
复制
exceptionconverter: com.itextpdf.text.documentexception: the page size must be smaller than 14400 by 14400. it's 1080.0 by 25288.0

这是因为图像的高度超过了最大页面大小14400。我知道那部分了。

但我想知道如果图像大小超过页面大小,如何将图像拆分为两页。

我尝试了以下代码:

代码语言:javascript
运行
复制
        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();

但还是不能让它工作。有人能在这件事上给我指个方向吗?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47533036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档