2

我正在使用 Java 和 xdocreport 来编辑 .odt 文件(模板)并将其另存为 odt。一切正常,但是当我尝试将 odt 转换为 pdf 时,出现此错误:

ODFConverterException:java.lang.ClassCastException:非法元素的插入:12

此错误是由我的 odt 源文件中的文本框引起的。

从 odt 到 odt 一切正常,它保留了我的文本框。

所以我尝试将我的 pom 中的依赖项更改为最新版本。现在我不再有这个问题了,但它会生成一个没有文本框的 pdf 文件。

我真的需要一个带有文本框的 pdf,在互联网上我发现 ODFConvertor 是有限的。

public void generate(ctModel ctmodel) throws Exception {

    final InputStream in =new FileInputStream(new File("C:\\Users\\A_PC\\Desktop\\AB\\abs-templateGenerator\\src\\main\\resources\\ODM.odt"));
    final IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);

    // 2) Create context Java model and add model
    final IContext context = report.createContext();

    context.put("ct",ctmodel);

    OutputStream out = new FileOutputStream(new File("C:\\Users\\ANDRICE_PC\\Desktop\\ANDRICE-BRAIN\\abs-templateGenerator\\src\\main\\resources\\ODM_final.odt"));
    report.process(context, out);


    //generate PDF
    InputStream inPdf= new FileInputStream(new File("cdi_final.odt"));
    OdfTextDocument document = OdfTextDocument.loadDocument(inPdf);


    PdfOptions options = PdfOptions.create().fontEncoding("");

    OutputStream outPdf = new FileOutputStream(new File("cdi_final.pdf"));
    PdfConverter.getInstance().convert(document, outPdf, options);
}

我的pom:

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document.odt</artifactId>
    <version>1.0.6</version>
</dependency>

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.converter.odt.odfdom</artifactId>
    <version>1.0.6</version>
</dependency>

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template.velocity</artifactId>
    <version>1.0.6</version>
</dependency>

有人可以提供一个解决方案来将我的文本框保留在 pdf 中,或者提供另一种解决方案来使用我的文本框将我的 odt 转换为 pdf 吗?

4

0 回答 0