Home > Net >  how to change image position to center in PDF android
how to change image position to center in PDF android

Time:10-06

My app create pdf using this code

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();
}

problem is when user select smaller size image than page size it will move to top left corner

Screenshot

i have tried

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

and if image is bigger than page size it goes out of the page I want to shrink the image size to page size

screenshot

CodePudding user response:

I have Changed this image position to

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

This and It works

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