如何在线程“主”中修复此异常java.lang.NoSuchMethodError:org.apache.poi.POIXMLDocumentPart.getPackageRelationship

问题描述 投票:1回答:2
public class CreatePdf {

    public static void main(String[] args) {
        CreatePdf pdf = new CreatePdf();
        System.out.println("start");
        pdf.ConvertToPDF("D:\\doctopdf.docx", "D:\\Test1.pdf");
    }

    public void ConvertToPDF(String docPath, String pdfPath) {

        try {
            InputStream doc = new FileInputStream(new File(docPath));
            XWPFDocument document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();
            OutputStream out = new FileOutputStream(new File(pdfPath));
            PdfConverter.getInstance().convert(document, out, options);
            System.out.print("Done");
      /*}catch(FileNotFoundException ex){
            System.out.print(ex.getMessage());*/
        }catch(IOException ex){
            System.out.print(ex.getMessage());
        }
    }   
}

我的要求是创建一个Java代码,以适当的格式和对齐方式将现有docx转换为pdf。

java pdf apache-poi
2个回答
2
投票

关于标题的快速谷歌给了我这个(仍然开放)的问题https://github.com/opensagres/xdocreport/issues/208

以及最后的评论说

khausam在3月12日评论了我正在使用的:编译组:'fr.opensagres.xdocreport',名称:'org.apache.poi.xwpf.converter.xhtml',版本:'1.0.6'

我切换到:编译组:'fr.opensagres.xdocreport',名称:'fr.opensagres.poi.xwpf.converter.xhtml',版本:'2.0.1'

问题已解决。谁能确认这是否是合理可行的升级途径?


0
投票

我刚刚尝试了Document4j:

  try {
        InputStream in = testXdocReportDoc.class.getResourceAsStream("testXdocReportDocV2.docx");
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);

        IContext context = report.createContext();
        Project project = new Project("test1", "test2", "test3", "test4", "test5");
        context.put("project", project);
        File tempFile = new File(f.getAbsolutePath().replace("pdf", "docx"));
        OutputStream out = new FileOutputStream(tempFile);
        report.process(context, out);

        out.flush();
        out.close();
        in.close();

        IConverter converter2 = RemoteConverter.builder()
                .baseFolder(new File(HistorisationDevisManager.getAbsolutePathDevisPdf(devis)))
                .workerPool(20, 25, 2, TimeUnit.SECONDS)
                .requestTimeout(10, TimeUnit.SECONDS)
                .baseUri("http://localhost:8080")
                .build();

        Future<Boolean> conversion = converter2
                .convert(tempFile).as(DocumentType.MS_WORD)
                .to(bo).as(DocumentType.PDF)
                .prioritizeWith(1000) // optional
                .schedule();
        conversion.get();

        try {
            OutputStream outputStream = new FileOutputStream(f);
            bo.writeTo(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        bo.close();
    }  catch (Exception e) {
        e.printStackTrace();
    }
}

[一切似乎都正常,我的docx是一个简单的文件,里面带有“ test”,但是当我尝试打开生成的PDF时,它的工作方式被打破了,我不知道为什么...

有人可以帮我吗!我完全不知道下一步该怎么做...

© www.soinside.com 2019 - 2024. All rights reserved.