XPage - docx4j - 用文本替换书签 [英] XPages - docx4j - replacing a bookmark with text

查看:45
本文介绍了XPage - docx4j - 用文本替换书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XPage 应用程序中使用 docx4j 来创建包含来自 XPage 的内容的 Word 文档.Word 文档(.docx 格式)是基于模板(.dotx 格式)创建的.我的 .dotx 模板中的一个书签如下:

I'm using docx4j in an XPages application to create Word documents containing content from an XPage. The Word document (in .docx format) is created based on a template (in .dotx format). One bookmark from my .dotx template is as follows:

<w:p>
<w:bookmarkStart w:name="Fachkompetenz" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
</w:p>

使用函数

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

我可以通过调用

List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), P.class);

或 CTBookmark 对象调用

or the CTBookmark object calling

List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), P.class);

但是,一旦我有了这些对象,我就不知道如何添加文本(XPage 内容)来替换书签.我已经在互联网上阅读了有关此主题的尽可能多的内容,但找不到任何方法.有人有什么建议吗?

However, once I have these objects I don't know how to add text (XPage content) to replace the bookmark. I've read as much as I can find on the internet on this topic but can't find any way of doing this. Does anybody have any suggestion?

推荐答案

我通过使用下面的函数设法解决了这个问题.将来可能需要重构,但现在可以使用:

I managed to solve this problem by using the below function. It may need refactoring in future but it works for now:

private void replaceParagraph(String placeholder, String textToAdd, WordprocessingMLPackage template) {


        List<Object> paragraphs = getAllElementFromObject(template.getMainDocumentPart(), P.class);

        for (Object p : paragraphs) {  
            RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
            new TraversalUtil(p, rt);

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

                if (content.getName().equals(placeholder)) {  

                    List<Object> theList = null;
                    if (content.getParent() instanceof P) {
                    theList = ((ContentAccessor)(content.getParent())).getContent();
                    } else {
                    continue; 
                    }

                    if (textToAdd == ""){ 

                        int index = theList.indexOf(content);
                        Object removed = theList.remove(index);
                    } else {
                    org.docx4j.wml.R run = factory.createR();
                    org.docx4j.wml.Text t2 = factory.createText();
                    run.getContent().add(t2);   
                    t2.setValue(textToAdd);
                    theList.add(0, run); 
                    break;
                    } 
                } 
            }  

        } 

    }

杰森,谢谢你的帮助.

这篇关于XPage - docx4j - 用文本替换书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆