Page 1 of 1

SdtContentBlock to html

PostPosted: Sat Feb 09, 2019 12:19 pm
by laurenquintanilla
Hello,

Is it possible to convert just the content from a SdtContentBlock to html? Instead of converting the entire document to html and trying to parse out the pieces from various content controls.

Re: SdtContentBlock to html

PostPosted: Mon Feb 11, 2019 8:24 pm
by jason
Hi Lauren

There's a fairly straightforward hack: you can temporarily replace the main document part's content list with just the content of interest (in this case your SdtContentBlock's), perform the conversion, then swap it back again.

Alternatively, HtmlExporterNonXSLT contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public org.w3c.dom.Document export(Object blockLevelContent, String cssClass, String cssId) {
                HTMLConversionContext conversionContext =
                                new HTMLConversionContext(htmlSettings, null, null);
        Document document = XmlUtils.neww3cDomDocument();      
    Element parentNode = document.createElement("div");
    AbstractVisitorExporterGenerator<HTMLConversionContext> generator = null;
        if (cssClass!=null) {
                parentNode.setAttribute("class", cssClass);
        }
        if (cssId!=null) {
                parentNode.setAttribute("id", cssId);
        }
        document.appendChild(parentNode);
        generator = HTMLExporterVisitorGenerator.GENERATOR_FACTORY.
                        createInstance(conversionContext, document, parentNode);
        new TraversalUtil(blockLevelContent, generator);
                return document;
        }      

 
Parsed in 0.017 seconds, using GeSHi 1.0.8.4


but the Javadoc warns of cases where it might not work.

Re: SdtContentBlock to html

PostPosted: Wed Mar 06, 2019 9:06 am
by laurenquintanilla
Trying to do the 'hack', but when I log out the output stream, I get an empty body. Output stream just has some placeholders and header/footer info.
Do you see anything obvious wrong here?

Object bodyContent = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(String.format(contentXpath, theAttribute), false);
//bodyContent is size 1 ArrayList with one populated SdtContentBlock
OutputStream os = new ByteArrayOutputStream();
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
wordMLPackage.getMainDocumentPart().getContent().clear();
wordMLPackage.getMainDocumentPart().getContent().addAll((Collection<?>) bodyContent);
htmlSettings.setWmlPackage(wordMLPackage);
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

Re: SdtContentBlock to html

PostPosted: Wed Mar 06, 2019 11:35 am
by laurenquintanilla
Nevermind - just found it

Re: SdtContentBlock to html

PostPosted: Wed Mar 27, 2019 11:25 am
by laurenquintanilla
do you know where to set the property to remove this from the generated html?

TO HIDE THESE MESSAGES, TURN OFF debug level logging for org.docx4j.convert.out.common.writer.AbstractMessageWriter


I tried the following:
Docx4jProperties.setProperty("org.docx4j.convert.out.common.writer.AbstractMessageWriter", false);
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

but I still get that message in my html

Re: SdtContentBlock to html

PostPosted: Thu Mar 28, 2019 6:55 pm
by jason
Configure logging; see the notes at https://github.com/plutext/docx4j/blob/ ... g4j.xml#L5

If you do use that log4j config, adjust line 115: https://github.com/plutext/docx4j/blob/ ... j.xml#L115