Skip to content
Snippets Groups Projects
Commit 40a13b6e authored by juanf's avatar juanf
Browse files

SSDM-5725 : Images on doc exports have now correct sizes.

parent 08efced1
No related branches found
No related tags found
No related merge requests found
package ch.ethz.sis;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
......@@ -11,130 +10,147 @@ import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;
public class DOCXBuilder
{
public static void main(String[] args) throws Exception
{
DOCXBuilder docx = new DOCXBuilder();
docx.addTitle("TitleA");
docx.addHeader("MetaA");
docx.addProperty("PropertyA", "ValueA");
docx.addProperty("PropertyB", "ValueB");
docx.addProperty("PropertyC", "<p>I am normal</p><p style=\"color:red;\">I am red</p><p style=\"color:blue;\">I am blue</p><p style=\"font-size:36px;\">I am big</p>");
FileOutputStream out = new FileOutputStream(new File("wordFromHTML.docx"));
out.write(docx.getDocBytes());
out.close();
}
private static final String START_RICH_TEXT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>";
private static final String END_RICH_TEXT = "</body></html>";
private StringBuffer doc;
private boolean closed;
public DOCXBuilder()
{
closed = false;
doc = new StringBuffer();
startDoc();
}
private void startDoc()
{
if (!closed)
{
doc.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
doc.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
doc.append("<head></head>");
doc.append("<body>");
}
}
private void endDoc()
{
if (!closed)
{
doc.append("</body>");
doc.append("</html>");
closed = true;
}
}
public void addProperty(String key, String value)
{
if (!closed)
{
value = cleanXMLEnvelope(value);
doc.append("<p>").append("<b>").append(key).append(": ").append("</b>").append(value).append("</p>");
}
}
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class DOCXBuilder {
public static void main(String[] args) throws Exception {
DOCXBuilder docx = new DOCXBuilder();
docx.addTitle("TitleA");
docx.addHeader("MetaA");
docx.addProperty("PropertyA", "ValueA");
docx.addProperty("PropertyB", "ValueB");
docx.addProperty("PropertyC",
"<p>I am normal</p><p style=\"color:red;\">I am red</p><p style=\"color:blue;\">I am blue</p><p style=\"font-size:36px;\">I am big</p>");
FileOutputStream out = new FileOutputStream(new File("wordFromHTML.docx"));
out.write(docx.getDocBytes());
out.close();
}
private static final String START_RICH_TEXT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>";
private static final String END_RICH_TEXT = "</body></html>";
private StringBuffer doc;
private boolean closed;
public DOCXBuilder() {
closed = false;
doc = new StringBuffer();
startDoc();
}
private void startDoc() {
if (!closed) {
doc.append(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
doc.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
doc.append("<head></head>");
doc.append("<body>");
}
}
private void endDoc() {
if (!closed) {
doc.append("</body>");
doc.append("</html>");
closed = true;
}
}
public void addProperty(String key, String value) {
if (!closed) {
doc.append("<p>").append("<b>").append(key).append(": ").append("</b>").append("</p>");
addParagraph(value);
}
}
public void addParagraph(String value)
{
if (!closed)
{
value = cleanXMLEnvelope(value);
doc.append("<p>").append(value).append("</p>");
}
}
public void addTitle(String title)
{
if (!closed)
{
doc.append("<h1>").append(title).append("</h1>");
}
}
public void addHeader(String header)
{
if (!closed)
{
doc.append("<h2>").append(header).append("</h2>");
doc.append("<p>").append(getImgFixed(value)).append("</p>");
}
}
public byte[] getHTMLBytes() throws Exception
{
endDoc();
return doc.toString().getBytes();
}
public byte[] getDocBytes() throws Exception
{
// .. Finish Document
endDoc();
// .. HTML Code
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(doc.toString().getBytes());
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);
// .. content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
wordMLPackage.save(outStream);
return outStream.toByteArray();
}
private String cleanXMLEnvelope(String value)
{
if (value.startsWith(START_RICH_TEXT) && value.endsWith(END_RICH_TEXT))
{
value = value.substring(START_RICH_TEXT.length() + 3, value.length() - END_RICH_TEXT.length());
}
return value;
}
public void addTitle(String title) {
if (!closed) {
doc.append("<h1>").append(title).append("</h1>");
}
}
public void addHeader(String header) {
if (!closed) {
doc.append("<h2>").append(header).append("</h2>");
}
}
public byte[] getHTMLBytes() throws Exception {
endDoc();
return doc.toString().getBytes();
}
public byte[] getDocBytes() throws Exception {
// .. Finish Document
endDoc();
// .. HTML Code
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(doc.toString().getBytes());
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);
// .. content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
wordMLPackage.save(outStream);
return outStream.toByteArray();
}
private String cleanXMLEnvelope(String value) {
if (value.startsWith(START_RICH_TEXT) && value.endsWith(END_RICH_TEXT)) {
value = value.substring(START_RICH_TEXT.length() + 3, value.length() - END_RICH_TEXT.length());
}
return value;
}
private String getImgFixed(String value) {
Document doc = Jsoup.parse(value);
Elements elements = doc.select("img");
for (Element element : elements) {
String style = element.attr("style");
if (style != null) {
String[] rules = style.split(";");
if(rules != null) {
for (int rIdx = 0; rIdx < rules.length; rIdx++) {
String rule = rules[rIdx];
String[] ruleElements = rule.split(":");
if (ruleElements != null && ruleElements.length == 2) {
String ruleKey = ruleElements[0].trim();
String ruleValue = ruleElements[1].trim();
if ((ruleKey.toLowerCase().equals("width") || ruleKey.toLowerCase().equals("height"))
&& ruleValue.endsWith("px")) {
element.attr(ruleKey, ruleValue.substring(0, ruleValue.length() - 2));
}
}
}
}
}
}
return doc.html();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment