2

I am testing Apache POI and I have questions.
I am working with maven and java.

  1. Is possible convert an excel file to PDF without reading it (Something similar to PdfConverter.getInstance().convert)
  2. In case there is no function for doing as in step 1, how can I keep the excel styles, formats and others things?
  3. Why does this code PdfConverter.getInstance().convert(doc,outFile,options); have this error java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart (see the code below)?
  4. Is there some other library that is FREE that I can use?.

I have attached the pom.xml and the code for the word conversion:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.0</version>
</dependency>

<!-- org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>   

<!-- org.apache.poi.xwpf.converter.pdf -->
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
    <version>1.0.6</version>
</dependency>    

<!-- com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

Code for the word conversion:

File file1 = new File("myWord.docx");
FileInputStream fileInpStr1 = new FileInputStream(file1.getAbsolutePath());

XWPFDocument doc = new XWPFDocument(fileInpStr1);
OutputStream outFile = new FileOutputStream("myPDFout.pdf"));

PdfOptions options = null;
PdfConverter.getInstance().convert(doc,outFile,options); <-- here the error jumps

outFile.close();
doc.close();
  • 1
    For the classnotfound see this answer: https://stackoverflow.com/questions/26024193/java-lang-classnotfoundexception-org-apache-xmlbeans-xmloptions – AndreaTaroni86 Sep 25 '18 at 14:29
  • didn't try by myself, but looks promissing https://github.com/yeokm1/docs-to-pdf-converter – pburgr Sep 25 '18 at 14:34
  • 1
    fr.opensagres.xdocreport does not support poi 4.0.0 - https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.core/1.0.6 - dependency on poi 3.10-FINAL – PJ Fanning Sep 25 '18 at 15:53
  • PJ Fanning i add the dependency and change poi version, but i got another error in class XWPFDocument. – Marco Gonzales Sep 25 '18 at 16:21
  • 1
    Please take a moment to consult the site's [help] on asking questions. This is a Q&A site, not a "forum". As such, only one question per "Question" is allowed - you ask four. Asking for library or software recommendations is off-topic. You can use the [edit] link under the question to make it conform to Stack Overflow's guidelines. – Cindy Meister Sep 25 '18 at 16:56
  • The code works perfect but generates a corrupt PDF file. – iltaf khalid Jan 25 '20 at 12:17

1 Answers1

2

I've met the same problem while trying to convert word(.docx) to pdf via POI.The error is java.lang.NoSuchMethodError:org.apache.poi.POIXMLDocumentPart.getPackageRelationship()Lorg/apache/poi/openxml4j/opc/PackageRelationshipe and I fix this problem by changing POI's version from 3.16 to 3.15(see the dependency below).But I don't think it's the Optimal solution.

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>  <-- right version number
        </dependency>

So if you resolve this problem by not changing the version,please show me your code.