Add image caption on Word/Pdf export

This commit is contained in:
George Kalampokis 2022-06-07 13:10:06 +03:00
parent 54c5017609
commit 7985b13cca
1 changed files with 14 additions and 2 deletions

View File

@ -59,12 +59,15 @@ public class WordBuilder {
private CTAbstractNum cTAbstractNum;
private BigInteger numId;
private Integer indent;
private static final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper;
private Integer imageCount;
public WordBuilder(Environment environment) {
this.cTAbstractNum = CTAbstractNum.Factory.newInstance();
this.cTAbstractNum.setAbstractNumId(BigInteger.valueOf(1));
this.indent = 0;
this.imageCount = 0;
this.mapper = new ObjectMapper();
this.buildOptions(environment);
}
@ -157,6 +160,8 @@ public class WordBuilder {
this.options.put(ParagraphStyle.IMAGE, (mainDocumentPart, item) -> {
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
paragraph.setPageBreak(true);
paragraph.setSpacingAfter(0);
paragraph.setAlignment(ParagraphAlignment.CENTER); //GK: Center the image if it is too small
XWPFRun run = paragraph.createRun();
String imageId = ((Map<String, String>)item).get("id");
String fileName = ((Map<String, String>)item).get("name");
@ -198,8 +203,15 @@ public class WordBuilder {
width = Math.round(height/ratio);
}
run.addPicture(image, format, fileName, Units.toEMU(width), Units.toEMU(height));
XWPFPicture picture = run.addPicture(image, format, fileName, Units.toEMU(width), Units.toEMU(height));
paragraph.setPageBreak(false);
imageCount++;
XWPFParagraph captionParagraph = mainDocumentPart.createParagraph();
captionParagraph.setAlignment(ParagraphAlignment.CENTER);
captionParagraph.setSpacingBefore(0);
XWPFRun captionRun = captionParagraph.createRun();
captionRun.setText("Image " + imageCount);
}
} catch (IOException | InvalidFormatException e){
logger.error(e.getMessage(), e);