[英]How to add space between images while creating pdf - Android
我的應用程序使用用戶選擇的圖像並使用 itextpdf 從中創建一個 PDF。
它正在成功創建 PDF 但圖像之間沒有空間
前任。
我的代碼
public void createPdf(String dest) throws IOException, DocumentException {
Image img = Image.getInstance(allSelectedImages.get(0).getPath());
Document document = new Document(img);
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
for (Uri image : allSelectedImages) {
img = Image.getInstance(image.getPath());
document.newPage();
document.setMargins(100, 100, 100, 100);
img.setAbsolutePosition(0, 0);
document.add(img);
}
document.close();
}
您每頁添加一張圖像,因此圖像之間的空間等於頁面之間的空間,由您的 PDF 查看器確定。
你能做的是在你的圖像周圍添加一些邊距——這是你已經在嘗試做的,但有些事情需要修復。
下面是一個關於如何調整代碼以在頁面的所有邊添加 100pt 邊距的示例(請注意,我正在動態計算頁面大小,以便在圖像大小不同的情況下頁面大小適應圖像大小):
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("path/to.pdf"));
document.open();
for (File image : allSelectedImages) {
Image img = Image.getInstance(image.getPath());
float leftMargin = 100;
float rightMargin = 100;
float topMargin = 100;
float bottomMargin = 100;
document.setPageSize(new Rectangle(img.getWidth() + leftMargin + rightMargin, img.getHeight() + topMargin + bottomMargin));
document.newPage();
document.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
img.setAbsolutePosition(leftMargin, bottomMargin);
document.add(img);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.