Click here to Skip to main content
15,886,799 members
Home / Discussions / Java
   

Java

 
Generaljava Pin
Member 1336471716-Aug-17 19:34
Member 1336471716-Aug-17 19:34 
GeneralRe: java Pin
Richard MacCutchan16-Aug-17 21:17
mveRichard MacCutchan16-Aug-17 21:17 
GeneralRe: java Pin
Patrice T23-Aug-17 16:19
mvePatrice T23-Aug-17 16:19 
QuestionHow to Connect Fingerprint Reader To Java Program Pin
Kistlak12-Aug-17 10:34
Kistlak12-Aug-17 10:34 
AnswerRe: How to Connect Fingerprint Reader To Java Program Pin
Richard MacCutchan12-Aug-17 20:50
mveRichard MacCutchan12-Aug-17 20:50 
SuggestionRe: How to Connect Fingerprint Reader To Java Program Pin
Richard Deeming13-Aug-17 23:42
mveRichard Deeming13-Aug-17 23:42 
GeneralRe: How to Connect Fingerprint Reader To Java Program Pin
Kistlak14-Aug-17 21:28
Kistlak14-Aug-17 21:28 
QuestionReplace placeholders with HTML text in dotx file using docx4 Pin
Sandeep Reddy9-Aug-17 0:08
Sandeep Reddy9-Aug-17 0:08 
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:

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:

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:
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));
   }

AnswerRe: Replace placeholders with HTML text in dotx file using docx4 Pin
jschell14-Aug-17 8:25
jschell14-Aug-17 8:25 
QuestionConvert ms word to PDF on linux Pin
Member 128526088-Aug-17 0:47
Member 128526088-Aug-17 0:47 
AnswerRe: Convert ms word to PDF on linux Pin
Richard MacCutchan8-Aug-17 5:35
mveRichard MacCutchan8-Aug-17 5:35 
GeneralRe: Convert ms word to PDF on linux Pin
Member 128526088-Aug-17 16:00
Member 128526088-Aug-17 16:00 
AnswerRe: Convert ms word to PDF on linux Pin
maba00117-Aug-17 7:07
maba00117-Aug-17 7:07 
Questionjava program using oops concept Pin
coderyadav6-Aug-17 9:21
coderyadav6-Aug-17 9:21 
AnswerRe: java program using oops concept Pin
Richard MacCutchan6-Aug-17 21:42
mveRichard MacCutchan6-Aug-17 21:42 
AnswerRe: java program using oops concept Pin
Patrice T16-Aug-17 12:41
mvePatrice T16-Aug-17 12:41 
QuestionBluetooth low Energy Sensor Pin
Member 133472935-Aug-17 0:23
Member 133472935-Aug-17 0:23 
AnswerRe: Bluetooth low Energy Sensor Pin
Richard MacCutchan5-Aug-17 1:34
mveRichard MacCutchan5-Aug-17 1:34 
Questionwhy do we need serialization?if we dont do serialization what will happen.SCenarios please Pin
Prakashhhh2-Aug-17 7:35
Prakashhhh2-Aug-17 7:35 
AnswerRe: why do we need serialization?if we dont do serialization what will happen.SCenarios please Pin
Richard MacCutchan2-Aug-17 20:45
mveRichard MacCutchan2-Aug-17 20:45 
Question[Java] New to programming Pin
CoderKlip30-Jul-17 12:46
CoderKlip30-Jul-17 12:46 
AnswerRe: [Java] New to programming Pin
Richard MacCutchan30-Jul-17 20:40
mveRichard MacCutchan30-Jul-17 20:40 
AnswerRe: [Java] New to programming Pin
jschell1-Aug-17 8:00
jschell1-Aug-17 8:00 
AnswerRe: [Java] New to programming Pin
OM Bharatiya15-Aug-17 7:06
OM Bharatiya15-Aug-17 7:06 
Questionnot able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
LOKENDRA YADAV15-Jul-17 9:46
LOKENDRA YADAV15-Jul-17 9:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.