docx4j如何在将docx转换为pdf时将嵌入的doc属性转换为简单文本

时间:2015-01-27 09:40:23

标签: java xml pdf converter docx4j

我想知道如何通过docx4j将显示的doc属性转换为“简单文本”。我的文档架构如下:

Full Name: [DOCPROPERTY] [DOCPROPERTY]
Date and Place of Birth: [DOCPROPERTY] [DOCPROPERTY]

主要问题是,当我将文档转换为PDF时,我将丢失上述信息。这是代码:

String inputfilepath = "...somePath"    
String outputfilepathWord = "...anotherPath"

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
wordMLPackage.save(new File(outputfilepathWord));

FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
OutputStream os = new java.io.FileOutputStream(outputfilepath);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

并且在pdf转换中我有以下结构:

Full Name: 
Date and Place of Birth: 

提前致谢!

编辑:

使用FieldUpdater

FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);

捕获以下异常:

java.util.NoSuchElementException
at java.util.LinkedList.removeFirst(LinkedList.java:268)
at java.util.LinkedList.pop(LinkedList.java:799)
at org.docx4j.model.fields.FieldsPreprocessor.inParentResult(FieldsPreprocessor.java:214)
at org.docx4j.model.fields.FieldsPreprocessor.handleRun(FieldsPreprocessor.java:346)
at org.docx4j.model.fields.FieldsPreprocessor.handleContent(FieldsPreprocessor.java:180)
at org.docx4j.model.fields.FieldsPreprocessor.canonicaliseInstance(FieldsPreprocessor.java:142)
at org.docx4j.model.fields.FieldsPreprocessor.canonicalise(FieldsPreprocessor.java:128)

这里:

else if (p.getParent() instanceof java.util.List) {
            // This does happen!
            index = ((java.util.List)p.getParent()).indexOf(p);
            P newP = FieldsPreprocessor.canonicalise(p, fieldRefs); //<<--EXCEPTION
            //log.debug("NewP length: " + newP.getContent().size() );
            ((java.util.List)p.getParent()).set(index, newP);       

1 个答案:

答案 0 :(得分:0)

根据ConvertOutPDF示例,您需要:

    // Refresh the values of DOCPROPERTY fields 
    FieldUpdater updater = new FieldUpdater(wordMLPackage);
    updater.update(true);