Support image

This commit is contained in:
Efstratios Giannopoulos 2024-05-09 14:08:46 +03:00
parent 62f69d2ea5
commit a96f62abf8
1 changed files with 36 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package org.opencdmp.filetransformer.docx.service.wordfiletransformer.word;
import org.opencdmp.commonmodels.enums.FieldType;
import org.opencdmp.commonmodels.models.FileEnvelopeModel;
import org.opencdmp.commonmodels.models.description.DescriptionModel;
import org.opencdmp.commonmodels.models.description.PropertyDefinitionFieldSetItemModel;
import org.opencdmp.commonmodels.models.description.PropertyDefinitionFieldSetModel;
@ -37,9 +38,7 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.management.InvalidApplicationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.math.BigInteger;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@ -108,8 +107,8 @@ public class WordBuilderImpl implements WordBuilder {
this.optionsInTable.put(ParagraphStyle.IMAGE, (mainDocumentPart, item) -> {
XWPFParagraph paragraph = mainDocumentPart.addParagraph();
XWPFRun run = paragraph.createRun();
if (item != null)
run.setText(((Map<String, String>) item).get("name"));
if (item instanceof FileEnvelopeModel)
run.setText(((FileEnvelopeModel)item).getFilename());
run.setFontSize(11);
run.setItalic(true);
return paragraph;
@ -201,14 +200,23 @@ public class WordBuilderImpl implements WordBuilder {
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");
String fileType = ((Map<String, String>) item).get("type");
int format;
format = IMAGE_TYPE_MAP.getOrDefault(fileType, 0);
FileEnvelopeModel itemTyped = (FileEnvelopeModel)item;
if (itemTyped == null) return paragraph;
try {
FileInputStream image = new FileInputStream(fileStorageServiceProperties.getTemp() + imageId);
ImageInputStream iis = ImageIO.createImageInputStream(new File(fileStorageServiceProperties.getTemp() + imageId));
String fileName = itemTyped.getFilename();
String fileType = itemTyped.getMimeType();
int format;
format = IMAGE_TYPE_MAP.getOrDefault(fileType, 0);
InputStream image;
ImageInputStream iis;
if (itemTyped.getFile() == null) {
throw new RuntimeException("Not implemented");
} else {
image = new ByteArrayInputStream(itemTyped.getFile());
iis = ImageIO.createImageInputStream(new ByteArrayInputStream(itemTyped.getFile()));
}
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
ImageReader reader = readers.next();
@ -219,17 +227,17 @@ public class WordBuilderImpl implements WordBuilder {
float ratio = initialImageHeight / (float) initialImageWidth;
int marginLeftInDXA = (int) mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getLeft();
int marginRightInDXA = (int) mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getRight();
int pageWidthInDXA = (int) mainDocumentPart.getDocument().getBody().getSectPr().getPgSz().getW();
int marginLeftInDXA = this.toIntFormBigInteger(mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getLeft());
int marginRightInDXA = this.toIntFormBigInteger(mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getRight());
int pageWidthInDXA = this.toIntFormBigInteger(mainDocumentPart.getDocument().getBody().getSectPr().getPgSz().getW());
int pageWidth = Math.round((pageWidthInDXA - marginLeftInDXA - marginRightInDXA) / (float) 20); // /20 converts dxa to points
int imageWidth = Math.round(initialImageWidth * (float) 0.75); // *0.75 converts pixels to points
int width = Math.min(imageWidth, pageWidth);
int marginTopInDXA = (int) mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getTop();
int marginBottomInDXA = (int) mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getBottom();
int pageHeightInDXA = (int) mainDocumentPart.getDocument().getBody().getSectPr().getPgSz().getH();
int marginTopInDXA = this.toIntFormBigInteger(mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getTop());
int marginBottomInDXA = this.toIntFormBigInteger(mainDocumentPart.getDocument().getBody().getSectPr().getPgMar().getBottom());
int pageHeightInDXA = this.toIntFormBigInteger(mainDocumentPart.getDocument().getBody().getSectPr().getPgSz().getH());
int pageHeight = Math.round((pageHeightInDXA - marginTopInDXA - marginBottomInDXA) / (float) 20); // /20 converts dxa to points
int imageHeight = Math.round(initialImageHeight * ((float) 0.75)); // *0.75 converts pixels to points
@ -258,6 +266,11 @@ public class WordBuilderImpl implements WordBuilder {
return paragraph;
});
}
private int toIntFormBigInteger(Object object){
if (object instanceof BigInteger) return ((BigInteger)object).intValue();
return (int) object;
}
@Override
public void build(XWPFDocument document, DescriptionTemplateModel descriptionTemplate, PropertyDefinitionModel propertyDefinitionModel, VisibilityService visibilityService) {
@ -460,7 +473,7 @@ public class WordBuilderImpl implements WordBuilder {
}
if (isImage) {
if (fieldValueModel != null && fieldValueModel.getTextValue() != null && !fieldValueModel.getTextValue().isEmpty()) {
XWPFParagraph paragraph = addCellContent(fieldValueModel.getTextValue(), mainDocumentPart, ParagraphStyle.IMAGE, numId, 0, numOfRows, numOfCells, 0); //TODO
XWPFParagraph paragraph = addCellContent(fieldValueModel.getFile(), mainDocumentPart, ParagraphStyle.IMAGE, numId, 0, numOfRows, numOfCells, 0);
if (paragraph != null) {
hasValue = true;
}
@ -577,7 +590,7 @@ public class WordBuilderImpl implements WordBuilder {
}
if (isImage) {
if (fieldValueModel.getTextValue() != null && !fieldValueModel.getTextValue().isEmpty()) {
XWPFParagraph paragraph = addParagraphContent(fieldValueModel.getTextValue(), mainDocumentPart, ParagraphStyle.IMAGE, numId, 0); //TODO
XWPFParagraph paragraph = addParagraphContent(fieldValueModel.getFile(), mainDocumentPart, ParagraphStyle.IMAGE, numId, 0); //TODO
if (paragraph != null) {
hasValue = true;
}
@ -675,8 +688,9 @@ public class WordBuilderImpl implements WordBuilder {
return hasValue;
}
private XWPFParagraph addCellContent(String content, XWPFTableRow mainDocumentPart, ParagraphStyle style, BigInteger numId, int indent, int numOfRows, int numOfCells, int numOfValuesInCell) {
if (content == null || content.isEmpty()) return null;
private XWPFParagraph addCellContent(Object content, XWPFTableRow mainDocumentPart, ParagraphStyle style, BigInteger numId, int indent, int numOfRows, int numOfCells, int numOfValuesInCell) {
if (content == null) return null;
if (content instanceof String && ((String) content).isEmpty()) return null;
this.indent = indent;
XWPFTableCell cell;