How to resize picture to paragraph size using Apache POI?

Eduardo

I'm trying to add a picture to a docx file using Apache POI, but the picture is larger than paragraph size. Is there a way to know paragraph size so I can resize image to fit paragraph? Below is how I attempt to add the picture.

XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();

String imgFile = "img.png";
BufferedImage img = ImageIO.read(new FileInputStream(imgFile));
int width = img.getWidth();
int height = img.getHeight();
double scaling = 1.0;
// Calculate scaling based on width and paragraph size
XWPFRun run = paragraph.createRun();
run.addPicture(new FileInputStream(imgFile), 
        XWPFDocument.PICTURE_TYPE_PNG, 
        imgFile, 
        Units.toEMU(width*scaling), 
        Units.toEMU(height*scaling));
Eduardo

After some investigation I found that, by default, the paper size and margins are not set when creating a docx file. So it is necessary to set them and to use the same value for setting image size.

int pageW = 500;
int pageH = 1000;
int pageM = 100;

CTDocument1 ctDoc = document.getDocument();
CTBody body = ctDoc.getBody();
if (!body.isSetSectPr()) {
    CTSectPr section = body.addNewSectPr();
    if (!section.isSetPgSz()) {
        CTPageSz size = section.addNewPgSz();
        size.setW(BigInteger.valueOf(pageW));
        size.setH(BigInteger.valueOf(pageH));
    }

    if (!section.isSetPgMar()) {
        CTPageMar margin = section.addNewPgMar();
        margin.setBottom(BigInteger.valueOf(pageM));
        margin.setTop(BigInteger.valueOf(pageM));
        margin.setLeft(BigInteger.valueOf(pageM));
        margin.setRight(BigInteger.valueOf(pageM));
    }
}

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
String imgFile = "img.png";
FileInputStream fis = new FileInputStream(imgFile);
BufferedImage img = ImageIO.read(fis);
int width = img.getWidth();
int height = img.getHeight();
double scaling = 1.0;
if (width > pageW - 2*pageM) {
    scaling = ((double)(pageW - 2*pageM)) / width;
}
run.addPicture(new FileInputStream(imgFile), 
        XWPFDocument.PICTURE_TYPE_PNG, 
        imgFile, 
        Units.toEMU(width * scaling / 20), 
        Units.toEMU(height * scaling / 20));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Apache POI disable auto resize of picture

How to partially bold a Paragraph in Apache POI? (Word Documents)

How to remove a line break when creating a text paragraph with apache POI

Apache POI docx - how to set page size?

How to set an ImageIcon to a JButton and resize the picture according to the button's size?

Different styles in one paragraph Apache poi

Getting paragraph styles in apache POI, language specific

apache poi get paragraph relative to other table or paragraph

how to change size of picture using openpyxl

How to cache the formula cell by using Apache POI

How to convert HSSFWorkbook to XSSFWorkbook using Apache POI?

How to create a circle using GeneralPath and apache POI

How to disable intermediate total using apache poi

How to add superscript text using Apache POI

How to remove a row using apache poi

How to format cell as currency using Apache POI

How to Highlight Replaced Word Using Apache POI

how change text direction(not paragraph alignment) in document in apache poi word?(XWPF)

How to change the font size and color of a Content Control in Apache POI

Apache POI XWPF - Check if a run contains a picture

Apache poi replace existing picture on header

Apache POI lock the cell but allow column resize

Using Apache poi

Using for loop in apache poi

Problem in fitting the excel cell size to the size of the content when using apache poi

Apache poi: insert text in a paragraph and view track changes in a word document

How to add a hyperlink to the footer of a XWPFDocument using Apache POI?

How to replace text(tag) with HTML in docx using Apache POI?

How to add an altChunk element to a XWPFDocument using Apache POI