Docx4j - How to replace placeholder with value

前端 未结 2 1382
轮回少年
轮回少年 2021-02-06 10:46

I\'ve been trying to work through the examples FieldMailMerge and VariableReplace but can\'t seem to get a local test case running. I\'m basically trying to start with one docx

相关标签:
2条回答
  • 2021-02-06 11:16

    I had the same issue and of course I could not force user to do some extra stuff when composing their word document so I decided to just write an algo to scan the whole document for expressions appending run after run, inserting replacement value and remove expressions in the second run. In case other people may need it below is what I did. I got the class from somewhere so it may be familiar. I just added the method searchAndReplace()

    package com.my.docx4j;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.JAXBException;
    
    import org.docx4j.openpackaging.exceptions.Docx4JException;
    import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
    import org.docx4j.wml.ContentAccessor;
    import org.docx4j.wml.Text;
    
    public class Docx4j {
    
        public static void main(String[] args) throws Docx4JException, IOException, JAXBException {
            String filePath = "C:\\Users\\markamm\\Documents\\tmp\\";
            String file = "Hello.docx";
    
            Docx4j docx4j = new Docx4j();
            WordprocessingMLPackage template = docx4j.getTemplate(filePath+file);
    
    //      MainDocumentPart documentPart = template.getMainDocumentPart();
    
            List<Object> texts = getAllElementFromObject(
                    template.getMainDocumentPart(), Text.class);
            searchAndReplace(texts, new HashMap<String, String>(){
                {
                    this.put("${abcd_efg.soanother_hello_broken_shit}", "Company Name here...");
                    this.put("${I_dont_know}", "Hmmm lemme see");
                    this.put("${${damn.right_lol}", "Gotcha!!!");
                    this.put("${one_here_and}", "Firstname");
                    this.put("${one}", "ChildA");
                    this.put("${two}", "ChildB");
                    this.put("${three}", "ChildC");
                }
                @Override
                public String get(Object key) {
                    // TODO Auto-generated method stub
                    return super.get(key);
                }
            });
    
            docx4j.writeDocxToStream(template, filePath+"Hello2.docx");
        }
    
        public static void searchAndReplace(List<Object> texts, Map<String, String> values){
    
            // -- scan all expressions  
            // Will later contain all the expressions used though not used at the moment
            List<String> els = new ArrayList<String>(); 
    
            StringBuilder sb = new StringBuilder();
            int PASS = 0;
            int PREPARE = 1;
            int READ = 2;
            int mode = PASS;
    
            // to nullify
            List<int[]> toNullify = new ArrayList<int[]>();
            int[] currentNullifyProps = new int[4];
    
            // Do scan of els and immediately insert value
            for(int i = 0; i<texts.size(); i++){
                Object text = texts.get(i);
                Text textElement = (Text) text;
                String newVal = "";
                String v = textElement.getValue();
    //          System.out.println("text: "+v);
                StringBuilder textSofar = new StringBuilder();
                int extra = 0;
                char[] vchars = v.toCharArray();
                for(int col = 0; col<vchars.length; col++){
                    char c = vchars[col];
                    textSofar.append(c);
                    switch(c){
                    case '$': {
                        mode=PREPARE;
                        sb.append(c);
    //                  extra = 0;
                    } break;
                    case '{': {
                        if(mode==PREPARE){
                            sb.append(c);
                            mode=READ;
                            currentNullifyProps[0]=i;
                            currentNullifyProps[1]=col+extra-1;
                            System.out.println("extra-- "+extra);
                        } else {
                            if(mode==READ){
                                // consecutive opening curl found. just read it
                                // but supposedly throw error
                                sb = new StringBuilder();
                                mode=PASS;
                            }
                        }
                    } break;
                    case '}': {
                        if(mode==READ){
                            mode=PASS;
                            sb.append(c);
                            els.add(sb.toString());
                            newVal +=textSofar.toString()
                                    +(null==values.get(sb.toString())?sb.toString():values.get(sb.toString()));
                            textSofar = new StringBuilder();
                            currentNullifyProps[2]=i;
                            currentNullifyProps[3]=col+extra;
                            toNullify.add(currentNullifyProps);
                            currentNullifyProps = new int[4];
                            extra += sb.toString().length();
                            sb = new StringBuilder();
                        } else if(mode==PREPARE){
                            mode = PASS;
                            sb = new StringBuilder();
                        }
                    }
                    default: {
                        if(mode==READ) sb.append(c);
                        else if(mode==PREPARE){
                            mode=PASS;
                            sb = new StringBuilder();
                        }
                    }
                    }
                }
                newVal +=textSofar.toString();
                textElement.setValue(newVal);
            }
    
            // remove original expressions
            if(toNullify.size()>0)
            for(int i = 0; i<texts.size(); i++){
                if(toNullify.size()==0) break;
                currentNullifyProps = toNullify.get(0);
                Object text = texts.get(i);
                Text textElement = (Text) text;
                String v = textElement.getValue();
                StringBuilder nvalSB = new StringBuilder();
                char[] textChars = v.toCharArray();
                for(int j = 0; j<textChars.length; j++){
                    char c = textChars[j];
                    if(null==currentNullifyProps) {
                        nvalSB.append(c);
                        continue;
                    }
                    // I know 100000 is too much!!! And so what???
                    int floor = currentNullifyProps[0]*100000+currentNullifyProps[1];
                    int ceil = currentNullifyProps[2]*100000+currentNullifyProps[3];
                    int head = i*100000+j;
                    if(!(head>=floor && head<=ceil)){
                        nvalSB.append(c);
                    } 
    
                    if(j>currentNullifyProps[3] && i>=currentNullifyProps[2]){
                        toNullify.remove(0);
                        if(toNullify.size()==0) {
                            currentNullifyProps = null;
                            continue;
                        }
                        currentNullifyProps = toNullify.get(0);
                    }
                }
                textElement.setValue(nvalSB.toString());
            }
        }
    
        private WordprocessingMLPackage getTemplate(String name)
                throws Docx4JException, FileNotFoundException {
            WordprocessingMLPackage template = WordprocessingMLPackage
                    .load(new FileInputStream(new File(name)));
            return template;
        }
    
        private static List<Object> getAllElementFromObject(Object obj,
                Class<?> toSearch) {
            List<Object> result = new ArrayList<Object>();
            if (obj instanceof JAXBElement)
                obj = ((JAXBElement<?>) obj).getValue();
    
            if (obj.getClass().equals(toSearch))
                result.add(obj);
            else if (obj instanceof ContentAccessor) {
                List<?> children = ((ContentAccessor) obj).getContent();
                for (Object child : children) {
                    result.addAll(getAllElementFromObject(child, toSearch));
                }
    
            }
            return result;
        }
    
        private void replacePlaceholder(WordprocessingMLPackage template,
                String name, String placeholder) {
            List<Object> texts = getAllElementFromObject(
                    template.getMainDocumentPart(), Text.class);
    
            for (Object text : texts) {
                Text textElement = (Text) text;
                if (textElement.getValue().equals(placeholder)) {
                    textElement.setValue(name);
                }
            }
        }
    
        private void writeDocxToStream(WordprocessingMLPackage template,
                String target) throws IOException, Docx4JException {
            File f = new File(target);
            template.save(f);
        }
    }
    
    0 讨论(0)
  • 2021-02-06 11:24

    The issue is that I was trying to create the placeholders as just plain text within the docx file. What I should've been doing instead is using the MergeField functionality within Word which I didn't fully understand and appreciate, hence the confusion. Basically I didn't know that this is what was being meant within the documentation because I'd never used it, I just assumed it was still some kind of xml text replacement.

    That being said it's still fairly difficult to find a good explanation of this Word feature. After looking at a few dozen explanations I still couldn't find a nice clean explanation of this Word feature. The best explanation I was able find can be found here. Basically you want to do Step 3.

    That being said, once I created MergeFields in Word and ran the code, it worked perfectly. The method to use is docx4jReplaceTwoPeopleTest. The problem wasn't in the code but in my understanding of how it worked within Word.

    0 讨论(0)
提交回复
热议问题