@Gptrnt In your code you store part of document content as HTML. It is not always possible to retain all MS Word document formatting in HTML. I have modified your code to use FlatOpc (MS Word 2007 XML) format instead of HTML and the result produced looks like what you need. Here are code modifications I have made:
- I have replaced
htmlSaveOption
method with the following:
private static OoxmlSaveOptions ooxmlSaveOption() throws Exception{
OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setSaveFormat(SaveFormat.FLAT_OPC);
return options;
}
- In the
TokenService
class I have modified addAgendaItemContent
method like the following:
private void addAgendaItemContent(DocumentBuilder builder, String contentString, String field, int slNo) throws Exception {
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
builder.getParagraphFormat().getStyle().setName("Normal");
contentString = contentString == null ? "" :contentString.trim();
addAgendaItemContentCore(builder,contentString, field, slNo);
}
The second overload of this method is renamed to addAgendaItemContentCore
and modified as the following:
private void addAgendaItemContentCore(DocumentBuilder builder, String contentString, String field, int slNo) throws Exception {
try
{
if (!contentString.equals(""))
{
ByteArrayInputStream bais = new ByteArrayInputStream(contentString.getBytes());
LoadOptions opts = new LoadOptions();
opts.setLoadFormat(LoadFormat.FLAT_OPC);
Document tempDoc = new Document(bais, opts);
DocumentBuilder tempBuilder = new DocumentBuilder(tempDoc);
if (tempDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0)
{
tempDoc.getLastSection().getBody().getLastParagraph().remove();
}
insertHiddenWord(tempBuilder, field + slNo, false);
tempBuilder.moveToDocumentEnd();
insertHiddenWord(tempBuilder, field + slNo, true);
int importFormatting = ImportFormatMode.KEEP_SOURCE_FORMATTING;
ImportFormatOptions importFormatOptions = new ImportFormatOptions();
builder.insertDocument(tempDoc, importFormatting, importFormatOptions);
}
}
catch (Exception e)
{
System.out.println("Error while creating document for inserting " + field);
e.printStackTrace();
}
}
Here is the output document produced on my side: output.docx (13.8 KB)
output.pdf (64.1 KB)