提问人:Guglio 提问时间:11/15/2023 更新时间:11/15/2023 访问量:26
Docx4j docx toPdf 不保留空格
Docx4j docx toPdf don't preserve whitespaces
问:
我对 docx4j 有疑问。输出的 pdf 被剪切,所有空格或制表符现在都只替换为一个。例如,如果在 docx 中有一个字符串,例如 “一个,一个,一个” 输出将为“a a a”。与选项卡相同。我的代码和我的pom在下面。我做错了什么?谢谢。
InputStream isNodo = new ByteArrayInputStream(contentNodoTempl);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(isNodo);
VariablePrepare.prepare(wordMLPackage);
// output file
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Docx4J.toPDF(wordMLPackage, bos);
bos.flush();
isNodo.close();
return bos.toByteArray();
我的绒球看起来像
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.9</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.9</version>
</dependency>
我尝试过使用 t.setSpace(“preserve”)(下面的代码),但空格仍然被剪切。
public static void findAndReplace(WordprocessingMLPackage doc){
List<Object> paragraphs = getAllElementFromObject(doc.getMainDocumentPart(), P.class);
for(Object par : paragraphs){
P p = (P) par;
List<Object> texts = getAllElementFromObject(p, Text.class);
for(Object text : texts){
Text t = (Text)text;
t.setSpace("preserve");
}
}
}
public 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;
}
答: 暂无答案
评论
t.setSpace(..