Page 1 of 1

Replace placeholders with HTML text in dotx file using docx4

PostPosted: Wed Aug 09, 2017 1:17 am
by sandeepreddy792
I am working on dotx file template to generate word documents using docx4j in java. I will be replacing some placeholders in dotx file with text which comes from database columns. But problem is one of my database field contains HTML code(rich text editor value), when I place this text in placeholder in template, generated document display as-is content of html. I want that HTML code to be rendered to be proper text with styles.
Cold you please let me know how can I achieve this?

Sample Code which I have written to replace placeholders:

Code: Select all
ByteArrayOutputStream baos = null;
      DataRequestVO dataRequest = null;
      WordprocessingMLPackage wordMLPackageMain = null;
      try {
         dataRequest = reportDAO.fetchDocketData(dataRequestNO);

         String templateFile = dataRequest.getReportType().getTemplatePath();
         String fileName = dataRequest.getDocketNo() + " - " + dataRequest.getDataRequestNo() + "-" + dataRequest.getReportType().getReportName();

         URL url = this.getClass().getClassLoader().getResource(templateFile);

         String templateFilePath = url.toURI().getPath();
         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(url.toURI().getPath()));

         ContentTypeManager ctm = wordMLPackage.getContentTypeManager();

         CTOverride override = ctm.getOverrideContentType().get(new URI("/word/document.xml"));
         if (templateFilePath.endsWith("dotm")) {
            override.setContentType(
                  org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
         } else {
            override.setContentType(org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT);
         }

         // Now save it
         DocketReportHelper.prepare(wordMLPackage);

         List<Object> texts = DocketReportHelper.getAllElementFromObject(wordMLPackage.getMainDocumentPart(),
               Text.class);

         DocketReportHelper.replacePlaceholder(texts, dataRequest.getDocketNo(), PLACEHOLDER_DOCKET_NO);
         
         DocketReportHelper.replacePlaceholder(texts, dataRequest.getQuestion(), PLACEHOLDER_QUESTION); //html code text
         DocketReportHelper.replacePlaceholder(texts, dataRequest.getResponse(), PLACEHOLDER_RESPONSE); //html code text
         

         // Confidential Reports
         if ("yes".equalsIgnoreCase(dataRequest.getConfidential())) {

            // Get Confidential Header Footer template
            wordMLPackageMain = createConfHeaderFooter();
            List<Object> listParamMain = wordMLPackage.getMainDocumentPart().getContent();
            for (Object param : listParamMain) {
               if (param instanceof P) {
                  wordMLPackageMain.getMainDocumentPart().addObject((P) param);
               } else if(param instanceof JAXBElement) {
                  wordMLPackageMain.getMainDocumentPart().addObject((JAXBElement) param);
               }
            }
         } else {
            wordMLPackageMain = wordMLPackage;
         }


         baos = new ByteArrayOutputStream();
         SaveToZipFile saver = new SaveToZipFile(wordMLPackageMain);
         saver.save(baos);


replacePlaceholder method:

Code: Select all
static void replacePlaceholder(List<Object> texts, String name, String placeholder) {
      log.debug("placeholder : " + placeholder);
      for (Object text : texts) {
         Text textElement = (Text) text;
         if (textElement != null && placeholder.equals(textElement.getValue())) {
            textElement.setValue(name);
            break;
         }
      }
   }


Prepare method:
Code: Select all
public static void prepare(WordprocessingMLPackage wmlPackage) throws Exception {
      // Apply the filter
      WordprocessingMLPackage.FilterSettings filterSettings = new WordprocessingMLPackage.FilterSettings();
      filterSettings.setRemoveProofErrors(true);
      filterSettings.setRemoveContentControls(true);
      filterSettings.setRemoveRsids(true);
      wmlPackage.filter(filterSettings);

      log.debug(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(), true, true));

      // Now clean up some more
      org.docx4j.wml.Document wmlDocumentEl = wmlPackage.getMainDocumentPart().getJaxbElement();
      Body body = wmlDocumentEl.getBody();

      SingleTraversalUtilVisitorCallback paragraphVisitor = new SingleTraversalUtilVisitorCallback(
            new TraversalUtilParagraphVisitor());
      paragraphVisitor.walkJAXBElements(body);

      log.debug(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(), true, true));
   }

Re: Replace placeholders with HTML text in dotx file using d

PostPosted: Wed Aug 09, 2017 9:40 am
by jason
I replied at your cross-post. Please don't do that...