Add support for Html text when exporting to docx (alpha)

This commit is contained in:
George Kalampokis 2021-10-05 17:58:46 +03:00
parent 54c862ecbb
commit 3ec942cfa7
5 changed files with 98 additions and 6 deletions

View File

@ -1142,7 +1142,7 @@ public class DataManagementPlanManager {
wordBuilder.addParagraphContent("Data Management Plan Information", document, ParagraphStyle.HEADER1, BigInteger.ZERO); wordBuilder.addParagraphContent("Data Management Plan Information", document, ParagraphStyle.HEADER1, BigInteger.ZERO);
// DMP title custom style. // DMP title custom style.
wordBuilder.addParagraphContent(dmpEntity.getLabel(), document, ParagraphStyle.HEADER2, BigInteger.ZERO); wordBuilder.addParagraphContent(dmpEntity.getLabel(), document, ParagraphStyle.HEADER2, BigInteger.ZERO);
wordBuilder.addParagraphContent(dmpEntity.getDescription(), document, ParagraphStyle.TEXT, BigInteger.ZERO); wordBuilder.addParagraphContent(dmpEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO);
wordBuilder.addParagraphContent("Funder", document, ParagraphStyle.HEADER3, BigInteger.ZERO); wordBuilder.addParagraphContent("Funder", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
if (dmpEntity.getGrant().getFunder() != null) if (dmpEntity.getGrant().getFunder() != null)
@ -1246,7 +1246,7 @@ public class DataManagementPlanManager {
}*/ }*/
wordBuilder.addParagraphContent(datasetEntity.getDescription(), document, ParagraphStyle.TEXT, BigInteger.ZERO); wordBuilder.addParagraphContent(datasetEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO);
// Dataset Description custom style. // Dataset Description custom style.
XWPFParagraph datasetDescriptionParagraph = document.createParagraph(); XWPFParagraph datasetDescriptionParagraph = document.createParagraph();

View File

@ -4,7 +4,7 @@ package eu.eudat.logic.utilities.documents.types;
* Created by ikalyvas on 2/26/2018. * Created by ikalyvas on 2/26/2018.
*/ */
public enum ParagraphStyle { public enum ParagraphStyle {
TEXT(0), HEADER1(1), HEADER2(2), HEADER3(3), HEADER4(4), TITLE(5), FOOTER(6), COMMENT(7), HEADER5(8), HEADER6(9); TEXT(0), HEADER1(1), HEADER2(2), HEADER3(3), HEADER4(4), TITLE(5), FOOTER(6), COMMENT(7), HEADER5(8), HEADER6(9), HTML(10);
private Integer value; private Integer value;
@ -38,6 +38,8 @@ public enum ParagraphStyle {
return HEADER5; return HEADER5;
case 9: case 9:
return HEADER6; return HEADER6;
case 10:
return HTML;
default: default:
throw new RuntimeException("Unsupported ParagraphStyle Code"); throw new RuntimeException("Unsupported ParagraphStyle Code");
} }

View File

@ -13,6 +13,7 @@ import eu.eudat.models.data.user.components.datasetprofile.FieldSet;
import eu.eudat.models.data.user.components.datasetprofile.Section; import eu.eudat.models.data.user.components.datasetprofile.Section;
import eu.eudat.models.data.user.composite.DatasetProfilePage; import eu.eudat.models.data.user.composite.DatasetProfilePage;
import eu.eudat.models.data.user.composite.PagedDatasetProfile; import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
@ -48,6 +49,19 @@ public class WordBuilder {
run.setFontSize(11); run.setFontSize(11);
return paragraph; return paragraph;
}); });
this.options.put(ParagraphStyle.HTML, (mainDocumentPart, item) -> {
try {
XWPFHtmlDocument xwpfHtmlDocument = XWPFHtmlDocument.addHtmlDocument(mainDocumentPart);
if (item != null) {
xwpfHtmlDocument.setHtml(item);
}
mainDocumentPart.getDocument().getBody().addNewAltChunk().setId(xwpfHtmlDocument.getId());
} catch (InvalidFormatException e) {
logger.error(e.getLocalizedMessage(), e);
}
return null;
});
this.options.put(ParagraphStyle.TITLE, (mainDocumentPart, item) -> { this.options.put(ParagraphStyle.TITLE, (mainDocumentPart, item) -> {
XWPFParagraph paragraph = mainDocumentPart.createParagraph(); XWPFParagraph paragraph = mainDocumentPart.createParagraph();
paragraph.setStyle("Title"); paragraph.setStyle("Title");
@ -223,11 +237,13 @@ public class WordBuilder {
public XWPFParagraph addParagraphContent(String text, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId) { public XWPFParagraph addParagraphContent(String text, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId) {
if (text != null && !text.isEmpty()) { if (text != null && !text.isEmpty()) {
XWPFParagraph paragraph = this.options.get(style).apply(mainDocumentPart, text); XWPFParagraph paragraph = this.options.get(style).apply(mainDocumentPart, text);
if (paragraph != null) {
if (numId != null) { if (numId != null) {
paragraph.setNumID(numId); paragraph.setNumID(numId);
} }
return paragraph; return paragraph;
} }
}
return null; return null;
} }

View File

@ -0,0 +1,63 @@
package eu.eudat.logic.utilities.documents.word;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;
public class XWPFHtmlDocument extends POIXMLDocumentPart {
private String html;
private String id;
public XWPFHtmlDocument(PackagePart pkg, String id) {
super(pkg);
this.html = "<!DOCTYPE html><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><style></style><title>HTML import</title></head><body><p></p></body>";
this.id = id;
}
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = this.html.replace("<p></p>", html);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
protected void commit() throws IOException {
PackagePart packagePart = getPackagePart();
OutputStream outputStream = packagePart.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(html);
writer.close();
outputStream.close();
}
public static XWPFHtmlDocument addHtmlDocument(XWPFDocument document) throws InvalidFormatException {
OPCPackage oPCPackage = document.getPackage();
String id = UUID.randomUUID().toString();
PackagePartName partName = PackagingURIHelper.createPartName("/word/" + id + ".html");
PackagePart part = oPCPackage.createPart(partName, "text/html");
XWPFHtmlDocument xWPFHtmlDocument = new XWPFHtmlDocument(part, id);
document.addRelation(xWPFHtmlDocument.getId(), new XWPFHtmlRelation(), xWPFHtmlDocument);
return xWPFHtmlDocument;
}
}

View File

@ -0,0 +1,11 @@
package eu.eudat.logic.utilities.documents.word;
import org.apache.poi.ooxml.POIXMLRelation;
public class XWPFHtmlRelation extends POIXMLRelation {
public XWPFHtmlRelation() {
super("text/html",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk",
"/word/htmlDoc#.html");
}
}