Page 1 of 1

Append rtf content to docx

PostPosted: Tue Apr 07, 2015 6:33 pm
by Zeko
Hello,

I would like to know if it is possible to append a String rtf content to a docx ?
The code I'm currently using replace bookmarks with string and the rtf string I use to replace "DestAdresse3" do not conserve the formatting.

Code: Select all
Map<DataFieldName, String> map = new HashMap<DataFieldName, String>();
map.put( new DataFieldName("DestAdresse1"), "test1");
map.put( new DataFieldName("DestAdresse2"), "test2");
map.put( new DataFieldName("DestAdresse3"), someRtfString);
WordprocessingMLPackage wordMLPackage = Docx4J.load(file);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
Body body = wmlDocumentEl.getBody();
BookmarksReplaceWithText bti = new BookmarksReplaceWithText();
bti.replaceBookmarkContents(body.getContent(), map);
wordMLPackage.save(new File("D:\\dotx2.dotx"));


Thanks,

Zeko

Re: Append rtf content to docx

PostPosted: Wed Apr 08, 2015 10:32 am
by jason
You'd have to parse/convert RTF yourself; there are some open source Java libraries which could help with this.

Alternatively, docx4j can convert both XHTML and Flat OPC XML. If you use content control data binding (ie instead of bookmarks), it'll do it automatically for you.