【问题标题】:Remove white space from top in pdf从pdf顶部删除空白
【发布时间】:2020-01-23 13:51:33
【问题描述】:

我正在使用 "itextPdf" 库将我的图像保存为 PDF 格式。 一切正常,但我在生成的 pdf 顶部得到白色填充。

Document document = new Document();
Image image = Image.getInstance(imagePath);
document.setPageSize(new Rectangle(image.getWidth(),image.getHeight()));
document.open()
image.scalePercent(100f);
image.setDpi(300,300);
image.setAlignment(ALIGN_MIDDLE);
document.add(image);
document.close();

Screenshot

【问题讨论】:

    标签: android itext pdf-generation


    【解决方案1】:

    如果您正在使用iText 的自动布局功能(因为您没有具体说明要添加图像的位置),那么iText 将始终为Document 添加一个边距。

    您可以做的是在绝对位置添加图像,这样您就可以避免自动边距。

    你会发现他们网站上的这个例子很有帮助:

    https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-precisely-position-image-top-table

    或者,您也可以将页面的边距设置为 0。

    https://itextpdf.com/en/resources/faq/getting-started/itext-7/how-use-full-size-page

    请记住,我列出了 iText 7 Java 的示例。您可能需要环顾四周才能找到等效的 android API。

    【讨论】:

    • 白色填充仍然存在,尝试上述解决方案后宽度已减小。我已将所有四个方向的对齐设置为 0,并在 scaleAbsolute() 中传递图像宽度和高度
    • 请更新您的示例代码和输出。这样可以进一步诊断问题。