[英]docx4j how to convert embedded doc properties into simple text while converting docx to pdf
我想知道如何通過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);
根據ConvertOutPDF示例,您需要:
// Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.