如何用Docx4j中的表替换书签

时间:2015-12-08 18:47:30

标签: java docx4j

我是Docx4j的新手,我能够创建一个类,用docx模板中的String替换书签。 我将值放入:

new HashMap<String,String>

现在我需要的是如何用表替换书签,所以我有

new HashMap<String,Object>

问题是在删除书签后,我的代码只设置了文本值:

org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.Text t = factory.createText();
run.getContent().add(t);
t.setValue(value);

这是我目前的工作:

public class Export3 {

private static boolean DELETE = false;
private static org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();

public static void main(String[] args) throws Docx4JException, IOException,JAXBException {

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("C:\\D.docx"));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
    Body body = wmlDocumentEl.getBody();
    Export3 ex = new Export3();

    ex.replace(body.getContent(), new HashMap<String, String>() {
        private static final long serialVersionUID = 1L;
        {
            this.put("pere", "SAM");            
        }
    });

    wordMLPackage.save(new java.io.File("C:\\D_" + System.nanoTime()
            + ".docx"));
}

private void replace(List<Object> content, Map<String, String> map2) {

    RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(content, rt);

    for (CTBookmark bm : rt.getStarts()) {

        if (bm.getName() == null)
            continue;
        String value = map2.get(bm.getName());
        if (value == null)
            continue;

        try {

            List<Object> theList = null;
            if (bm.getParent() instanceof P) {
                System.out.println("OK!");
                theList = ((ContentAccessor) (bm.getParent())).getContent();
            } else {
                continue;
            }

            int rangeStart = -1;
            int rangeEnd = -1;
            int i = 0;
            for (Object ox : theList) {
                Object listEntry = XmlUtils.unwrap(ox);
                if (listEntry.equals(bm)) {
                    if (DELETE) {
                        rangeStart = i;
                    } else {
                        rangeStart = i + 1;
                    }
                } else if (listEntry instanceof CTMarkupRange) {
                    if (((CTMarkupRange) listEntry).getId().equals(
                            bm.getId())) {
                        if (DELETE) {
                            rangeEnd = i;
                        } else {
                            rangeEnd = i - 1;
                        }
                        break;
                    }
                }
                i++;
            }

            if (rangeStart > 0 && rangeEnd > rangeStart) {

                for (int j = rangeEnd; j >= rangeStart; j--) {
                    theList.remove(j);
                }

                org.docx4j.wml.R run = factory.createR();
                org.docx4j.wml.Text t = factory.createText();
                run.getContent().add(t);
                t.setValue(value);

                theList.add(rangeStart, run);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我该如何管理?

任何帮助都应该得到真正的赞赏。

0 个答案:

没有答案