【问题标题】:how to change image position to center in PDF android如何在PDF android中将图像位置更改为中心
【发布时间】:2021-11-23 22:58:00
【问题描述】:

我的应用程序使用此代码创建 pdf

public void createPdf(String dest, int pageNum, int pageHeight, int pageWidth) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    document.setPageCount(pageNum);
    for (Uri image : allSelectedImages) {
        Image img = Image.getInstance(image.getPath());
        document.setPageSize(new Rectangle(pageHeight, pageWidth));
        document.newPage();
        document.add(img);
        document.addAuthor("PDF Reader Osdifa's User");
        document.addCreator("PDF Reader Osdifa");
    }
    document.close();
}

问题是当用户选择比页面尺寸更小的图像时,它会移动到左上角

我试过了

img.setAbsolutePosition(pageHeight/2, pageWidth/2);

如果图像大于页面大小,它会超出页面我想将图像大小缩小到页面大小

【问题讨论】:

  • @KJ 谢谢它有效

标签: java android pdf itext


【解决方案1】:

我已将此图像位置更改为

img.setAbsolutePosition(pageHeight/2, pageWidth/2);

这和它的工作原理

img.setAbsolutePosition(pageHeight / 2 - img.getWidth() / 2, pageWidth / 2 - img.getHeight() / 2);

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多