Skip to content
Advertisement

how to change image position to center in PDF android

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

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement