1

I'm having a word document, .docx, containing tables, paragraphs and images. I have been able to successfully convert the file to pdf but the pdf file is missing images. This is a code snippet I'm using:

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfOptions options = PdfOptions.create();

        PdfConverter.getInstance().convert(xwpfDocument, baos, options);

        new FileOutputStream(new File("/home/sam/test.pdf")).write(baos.toByteArray());

The final file, test.pdf does not contain images on the .docx. Is there something else I'm supposed to do?

Wafula Samuel
  • 530
  • 1
  • 8
  • 25
  • 1
    There is no `PdfOptions` class provided by Apache POI? Where did it come from? If it's your own class, what does it contain? – Gagravarr Jun 28 '18 at 10:34
  • There is. this is how I've imported it: `import org.apache.poi.xwpf.converter.pdf.PdfOptions;` – Wafula Samuel Jun 28 '18 at 10:45
  • 1
    It is not `apache poi`. It is `opensagres` and package is bad named only. And it has [limitations](https://github.com/opensagres/xdocreport/wiki/XWPFConverterPDFViaIText#limitations). – Axel Richter Jun 28 '18 at 11:10
  • That package name indicates you're using an old version of the opensagres library, what happens when you upgrade it? – Gagravarr Jun 28 '18 at 11:16
  • I'm using the latest version of `opensagres`. Version `1.0.6` – Wafula Samuel Jun 28 '18 at 11:34
  • Limitations clearly states: "draw/shape graphics are not converted". A picture in a `*.docx` document is nothing else than a `blipFill` of a `pic` within a `graphicData` within a `graphic` in a `drawing`. – Axel Richter Jun 28 '18 at 11:43
  • Thanks alot for that @AxelRichter, how can I handle that please? – Wafula Samuel Jun 28 '18 at 11:55
  • I would try the newer [XDocReport](https://github.com/opensagres/xdocreport/wiki/Overview). There is improved version of [Converter](https://github.com/opensagres/xdocreport/wiki/Overview#converter). Hint to the sample code: There also is `DocumentKind.DOCX`. I have not tried it though. – Axel Richter Jun 28 '18 at 13:12
  • Thanks, let me take a look at them. – Wafula Samuel Jun 28 '18 at 13:18
  • I'm working on implementing my own converter, I'm stuck at getting a picture's offset from the word document. Does anyone have an idea? @AxelRichter – Wafula Samuel Jun 28 '18 at 17:32
  • See https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157 for an example using `fr.opensagres.poi.xwpf.converter.pdf.PdfConverter`. Converts pictures in my example. – Axel Richter Jul 14 '18 at 09:42

0 Answers0