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