ColdFusion& Java(docx4j库) [英] ColdFusion & Java (docx4j library)

查看:245
本文介绍了ColdFusion& Java(docx4j库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做docx操作(查找/替换占位符和检查/取消选中复选框)。由于ColdFusion 10与Java集成良好,我决定尝试使用Java库docx4j,它基本上模仿了OpenXML SDK(.net平台)。

I need to do docx manipulation (find/replace on placeholders and checking/unchecking checkboxes). Since ColdFusion 10 integrates well with Java, I decided to try and use the Java library docx4j, which basically mimics the OpenXML SDK (.net platform).

我有docx4j JAR在一个自定义文件夹内,我在我的Application.cfc中通过JavaSettings(新的CF10,我尝试与其他JARS和它的工作原理)设置:

I have the docx4j JAR inside a custom folder, which I have setup in my Application.cfc via JavaSettings (new in CF10, and I tried it with other JARS and it works):

<cfcomponent output="false">

    <cfset this.javaSettings =
        {LoadPaths = ["/myJava/lib"], loadColdFusionClassPath = true, reloadOnChange= true, 
        watchInterval = 100, watchExtensions = "jar,class,xml"} />

</cfcomponent>

现在,我试图使用这个示例: https://github.com/plutext/docx4j/blob/master/src /main/java/org/docx4j/samples/VariableReplace.java

Now, I'm trying to use this sample:https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/samples/VariableReplace.java

但是试图调用WordprocessingMLPackage失败,并且函数 CreateObject()表示特定类不存在:

But trying to call the WordprocessingMLPackage fails with the function CreateObject() saying that particular class doesn't exist:

<cfset docObj = createObject("java","org.docx4j.openpackaging.packages.WordprocessingMLPackage") />

任何想法?我不是一个真正的Java人,但没有很多选项,有docx操作。

Any ideas? I'm not really a Java guy, but there are not many options out there for docx manipulation.

推荐答案

好吧。似乎我得到一切工作。我只是想出了如何做一个查找/替换,以及我想在一个docx文档中做的一切。这里是我的代码到目前为止,告诉你们,它看起来像是工作(确保您的Application.cfc看起来像原始帖子,如果你在CF10):

Alright. Seems like I got everything working. I just got to figure out how to do a find/replace, and everything else I want to do in a docx document. Here's my code so far to show you guys that it looks like it is working (make sure that your Application.cfc looks like the original post if you are on CF10):

<cfscript>

    docPackageObj = createObject("java","org.docx4j.openpackaging.packages.WordprocessingMLPackage").init();
    docObj = createObject("java","org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart").init();
    xmlUtilObj = createObject("java","org.docx4j.XmlUtils").init();
    wmlDocObj = createObject("java","org.docx4j.wml.Document").init();
    saveToZipFile = createObject("java","org.docx4j.openpackaging.io.SaveToZipFile").init(docPackageObj);

    strFilePath = getDirectoryFromPath(getCurrentTemplatePath()) & "testDoc.docx";

    wordMLPackage = 
        docPackageObj.load(createObject("java","java.io.File").init(javaCast("string",strFilePath)));

    documentPart = wordMLPackage.getMainDocumentPart();

    // unmarshallFromTemplate requires string input     
    strXml = xmlUtilObj.marshaltoString(documentPart.getJaxbElement(),true);

    writeDump(var="#strXml#");

</cfscript>

现在,有没有人知道如何将ColdFusion中的结构转换为hashmaps我认为CF中的结构实际上是util.Vector,而hashmaps是util.HashMap。所有的例子我看到与Docx4j演示find / replace在占位符使用:

Now, does anybody know how to cast structures in ColdFusion into hashmaps (or collections in general)? I think structures in CF are actually util.Vector, whereas hashmaps are util.HashMap. All of the examples I see with Docx4j that demonstrates find/replace in placeholders use this:

HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("colour", "green");
mappings.put("icecream", "chocolate");

这篇关于ColdFusion&amp; Java(docx4j库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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