xhtml fragments into existing word document
Posted: Fri May 23, 2014 9:24 am
I have an existing word document that we take user input from a web application to fill out completely for the user to download. It's a pretty large word document, and the application takes a lot of input. We had previously been building the word document with HTML that gets converted to a docx, but it seems easier just to use docx4j. I've had some good success with just taking the original sample document from our client and adding bookmarks to the points where we need the data entry to be popped in place.
The reason we have to deal with html is there are points where we allow user entry of rich text that gets saved to our database as html formatted text. So, I've been hacking together the samples for xhtml import to get a list of paragraph elements and appending it into the document at the point of the bookmarks.
The problem is I've got a segment of html that is an ordered list, when I append the parts in, it's losing the list basically. If I dump out the contents of what was imported into the list object from xhtmlimporter, it looks like the list is there, but when it's appended at the bookmark it's gone.
Here's the function I currently have to replace a single bookmark as a test:
Sorry if it's a little messy and it is ColdFusion since its our primary language and this was a feasibility test. Paragraphs is the bodycontents of the word doc and the datatext var is the list returned from xhtmlimporter.convert.
I hope all this makes sense and I'm doing something that makes sense and I'm not approaching this completely backwards.
Thanks for any help in advance!
The reason we have to deal with html is there are points where we allow user entry of rich text that gets saved to our database as html formatted text. So, I've been hacking together the samples for xhtml import to get a list of paragraph elements and appending it into the document at the point of the bookmarks.
The problem is I've got a segment of html that is an ordered list, when I append the parts in, it's losing the list basically. If I dump out the contents of what was imported into the list object from xhtmlimporter, it looks like the list is there, but when it's appended at the bookmark it's gone.
Here's the function I currently have to replace a single bookmark as a test:
Sorry if it's a little messy and it is ColdFusion since its our primary language and this was a feasibility test. Paragraphs is the bodycontents of the word doc and the datatext var is the list returned from xhtmlimporter.convert.
- Code: Select all
function replaceBookmarkContents(paragraphs, bookmarkName, dataText, XMLUtils, factory) {
pStyle = factory.createPPrBasePStyle();
pStyle.setVal("Normal");
bigInt = createObject( "java", "java.math.BigInteger" );
size = loader.create("org.docx4j.wml.HpsMeasure");
size.setVal(bigInt.valueOf(24));
pprbaseind = factory.createPPrBaseInd();
pprbaseind.setLeft( bigInt.valueOf( 720) );
rt = loader.create("org.docx4j.finders.RangeFinder").init("CTBookmark", "CTMarkupRange");
loader.create("org.docx4j.TraversalUtil").init(paragraphs, rt);
bookmarks = rt.getStarts();
for(i = 0; i < bookmarks.size(); i++) {
bm = bookmarks.get(i);
if(bm.getName() == javacast("null",'')) {
continue;
}
if(bm.getName() eq 'SectionASuccessNarrative') {
bmparent = bm.getParent();
if(bmparent.getClass().getName() eq "org.docx4j.wml.P") {
theList = bmParent.getContent();
} else {
continue;
}
bmparentList = bmParent.getParent();
bookmarkIndex = bmparentList.indexOf(bmparent);
for(datas = 0; datas < dataText.size(); datas++) {
paragraph = dataText.get(datas);
ppr = paragraph.getPPr();
ppr.setInd(pprbaseind);
recursiveSetSpacingFontSizeFamily(paragraph, "preserve", size, "Times New Roman");
}
// writeDump(var=XMLUtils.marshalToString(dataText), abort=true);
bmParentList.addAll(bookmarkIndex+1, dataText);
return;
}
}
I hope all this makes sense and I'm doing something that makes sense and I'm not approaching this completely backwards.
Thanks for any help in advance!