java 尝试使用Docx4J.toPDF时,使用Docx4J有困难

sc4hvdpw  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(372)

所以基本上我要做的就是写一个类,叫做WordProcessor。
这个类应该加载一个MS Word模板,用${variableName}替换MS Word模板中标记的一些空格值。然后它应该有两个功能,一个是将文件保存为MS Word文档,它工作得很好。它还应该将文件保存为PDF,不幸的是这是造成困难的部分。
我的文字处理器类:

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;

import org.docx4j.Docx4J;
import org.docx4j.model.datastorage.migration.VariablePrepare;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class WordProcessor {
    /**
     * Holds list of values for placeHolders in the word template
     */
    public HashMap<String, String> dictionary = new HashMap<String, String>();

    private WordprocessingMLPackage wordMLPackage;

    /**
     * Provides functions that allow to manipulate/edit a MS office document and
     * then to export it as PDF
     * 
     * @param documentTemplatePath
     * @param documentOutputPath
     * @throws Exception
     */
    public WordProcessor(String documentTemplatePath) throws Exception {
        this.wordMLPackage = WordprocessingMLPackage.load(new java.io.File(documentTemplatePath));
        VariablePrepare.prepare(this.wordMLPackage);
    }

    /**
     * Exports document as PDF
     * 
     * @param documentOutputPath
     * @throws Exception
     */
    public void exportAsPDF(String documentOutputPath) throws Exception {
        replaceAllWord();
        FileOutputStream os = new FileOutputStream(documentOutputPath);
        Docx4J.toPDF(wordMLPackage, os);
        os.flush();
        os.close();
    }

    /**
     * Exports document as MS word document
     * 
     * @param documentOutputPath
     * @throws Exception
     */
    public void exportAsDoc(String documentOutputPath) throws Exception {
        replaceAllWord();
        this.wordMLPackage.save(new File(documentOutputPath));
    }

    /**
     * Adds additional vocabulary, to replace placeHolder marked as ${variableName}
     * inside of the MS Word Template.
     * 
     * @param placeHolder - marked as ${variableName} inside MS template
     * @param value       - value that will be put instead of placeHolder
     */
    public void addVocabulary(String placeHolder, String value) {
        dictionary.put(placeHolder, value);
    }

    /**
     * Replaces all placeHolders in the template with relevant values from
     * vocabulary
     * 
     * @throws Exception
     */
    private void replaceAllWord() throws Exception {
        this.wordMLPackage.getMainDocumentPart().variableReplace(this.dictionary);
    }
}

我的主要职业:

import Functions.WordProcessor;

public class Test {
    public static void main(String[] args) throws Exception {
        
        // Initiate object
        WordProcessor wordProcessor = new WordProcessor("Test.docx");
        
        // Add vocabulary for placeholders
        wordProcessor.addVocabulary("Client", "David");
        wordProcessor.addVocabulary("date", "15.09.2022");
        
        // Save document as doc & PDF
        wordProcessor.exportAsDoc("TestOutPut.docx");
        wordProcessor.exportAsPDF("TestOutPut.pdf");
    }
}

我的POM Maven依赖项:

<dependencies>
        <!-- ALL DEPENDENCIES BELOW ALLOW EDITING MS Documents -->
        <!-- use the JAXB shipped in Java 8 -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-Internal</artifactId>
            <version>8.3.3</version>
        </dependency>
        <!-- use the JAXB Reference Implementation -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
            <version>8.3.3</version>
        </dependency>
        <!-- use the MOXy JAXB implementation -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-MOXy</artifactId>
            <version>8.3.3</version>
        </dependency>
        <!-- Helps to export as PDF -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-export-fo</artifactId>
            <version>8.3.3</version>
        </dependency>
    </dependencies>
    • 当我尝试使用方法时遇到的主要问题:**wordProcessor. exportAsPDF(path)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:108)
    at org.docx4j.Docx4J.toFO(Docx4J.java:710)
    at org.docx4j.Docx4J.toPDF(Docx4J.java:739)
    at Functions.WordProcessor.exportAsPDF(WordProcessor.java:41)
    at Debug.Test.main(Test.java:17)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: You must invoke FORendererApacheFOP.getFOUserAgent
    at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:124)
    at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
    at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:83)
    ... 4 more

请有人能帮我解决这个问题,我想该文件被导出为PDF格式。
先谢谢你了。

    • 几个小时后更新:**我自己找到了这个问题的解决方案。我用不同版本的Docx4J替换了我的POM Maven依赖项,这起作用了:
<dependencies>
        <!-- ALL DEPENDENCIES BELOW ALLOW EDITING MS Documents -->
        <!-- use the JAXB shipped in Java 8 -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-Internal</artifactId>
            <version>8.2.9</version>
        </dependency>
        <!-- use the JAXB Reference Implementation -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
            <version>8.2.9</version>
        </dependency>
        <!-- use the MOXy JAXB implementation -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-MOXy</artifactId>
            <version>8.2.9</version>
        </dependency>
        <!-- Helps to export as PDF -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-export-fo</artifactId>
            <version>8.2.9</version>
        </dependency>
    </dependencies>
83qze16e

83qze16e1#

依赖项docx4j-JAXB-Internaldocx4j-JAXB-ReferenceImpldocx4j-JAXB-MOXy是互斥的。您应该只选择其中一个,而不是全部,如https://www.docx4java.org/downloads.html中所述:

梅文·帕姆

docx 4j位于Maven Central:选择一个且仅一个

  • docx 4j-JAXB-内部(仅适用于docx 4j 8.x),
  • 文件4j-JAXB-MOXy(适用于文件4j 8或11),
  • docx 4j-JAXB-引用实现(对于docx 4j 8或11),

相关问题