I'm using below code to generate word document with HTML content using docx4j
and able to generate document successfully.
My requirement is to write content with some custom properties, so it would be easy to read the same document after modification made by user.
String finalData = "<h1> Heading One </h1>".aapend("<h2> Heading two </h2>");
String str1 = new StringBuffer()
.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;
charset=UTF-8\" /><style type='text/css'>
* { font-family: 'Arial Unicode MS'; } </style></head>")
.append(finalData).append("</html>").toString();
str1 = fixWhitespaceIssue(str1);
str1 = cleanHTML(str1);
System.out.println(str1);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
XHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(str1, null));
File exportFile = new File("test.docx");
wordMLPackage.save(exportFile);
For example:
<h1> Heading One </h1> // i'll bind custom property for first element as c_property1
<h2> Heading two </h2> // i'll use custom property for second element as c_property2
Generated document can be reviewed by some user, he will made some changes after that the same updated document will come to my code so the code must be capable to read the document using custom properties.
If I wish to pull updated values from document, so I just wanted to provide custom property then it should return its associated values.
For c_property1
, the code should return Heading One
or update value, e.g. Updated Heading One
.
For c_property2
, the code should return Heading two
or update value, e.g. Updated Heading two
.
Content controls
. These can be linked to aCustom XML Part
so that the control's content is saved to the XML file (Custom XML Part). Your code can then read the XML file. – Cindy Meister