Add image caption on Word/Pdf export
This commit is contained in:
parent
54c5017609
commit
7985b13cca
|
@ -59,12 +59,15 @@ public class WordBuilder {
|
||||||
private CTAbstractNum cTAbstractNum;
|
private CTAbstractNum cTAbstractNum;
|
||||||
private BigInteger numId;
|
private BigInteger numId;
|
||||||
private Integer indent;
|
private Integer indent;
|
||||||
private static final ObjectMapper mapper = new ObjectMapper();
|
private final ObjectMapper mapper;
|
||||||
|
private Integer imageCount;
|
||||||
|
|
||||||
public WordBuilder(Environment environment) {
|
public WordBuilder(Environment environment) {
|
||||||
this.cTAbstractNum = CTAbstractNum.Factory.newInstance();
|
this.cTAbstractNum = CTAbstractNum.Factory.newInstance();
|
||||||
this.cTAbstractNum.setAbstractNumId(BigInteger.valueOf(1));
|
this.cTAbstractNum.setAbstractNumId(BigInteger.valueOf(1));
|
||||||
this.indent = 0;
|
this.indent = 0;
|
||||||
|
this.imageCount = 0;
|
||||||
|
this.mapper = new ObjectMapper();
|
||||||
this.buildOptions(environment);
|
this.buildOptions(environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,6 +160,8 @@ public class WordBuilder {
|
||||||
this.options.put(ParagraphStyle.IMAGE, (mainDocumentPart, item) -> {
|
this.options.put(ParagraphStyle.IMAGE, (mainDocumentPart, item) -> {
|
||||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||||
paragraph.setPageBreak(true);
|
paragraph.setPageBreak(true);
|
||||||
|
paragraph.setSpacingAfter(0);
|
||||||
|
paragraph.setAlignment(ParagraphAlignment.CENTER); //GK: Center the image if it is too small
|
||||||
XWPFRun run = paragraph.createRun();
|
XWPFRun run = paragraph.createRun();
|
||||||
String imageId = ((Map<String, String>)item).get("id");
|
String imageId = ((Map<String, String>)item).get("id");
|
||||||
String fileName = ((Map<String, String>)item).get("name");
|
String fileName = ((Map<String, String>)item).get("name");
|
||||||
|
@ -198,8 +203,15 @@ public class WordBuilder {
|
||||||
width = Math.round(height/ratio);
|
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);
|
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){
|
} catch (IOException | InvalidFormatException e){
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
|
|
Loading…
Reference in New Issue