1

I'm trying to create a simple table with ApachePOI in a maven project. I looked in their documentation but I didn't see a dependency for PDFs. Is there a built in way in ApachePOI do to create a PDF?

I saw this tutorial that it teaches you how to convert the file but not how to create one from scratch. https://rdtschools.com/covert-docx-file-pdf-using-apache-poi-library-java/

Then I saw this question on stackoverflow, and the answer make it seem like using using Opensagres POI is the way to go since it works with Apache POI 3.17, but requires another JAR.

Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100
  • 3
    If you want to programatically create a .pdf from scratch - and if you're uninterested in working with Microsoft documents - then maybe ApachePOI isn't the right library for you. – paulsm4 Jul 31 '18 at 18:30
  • @paulsm4 That's a very interesting feedback. Do you think PDFBox is a better approach then? or what do you suggest (we can't use iText). Is there a good tutorial on how to create a pdf using microsoft products like excel to pdf using apache POI? – Patricio Vargas Jul 31 '18 at 18:47

1 Answers1

1

If you want to create pdf file, you can try

new FileOutputStream("path.pdf");

And then using pdf writer, write this to file. But if you are interested in Pdf table generation. I would like to offer you another lib.

I worked with itextpdf, and created table in pdf documents.

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.12</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
    <dependency>
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.4.4</version>
    </dependency>

You should write something like this

public PdfStream opendocument() {
    document = new Document(PageSize.A4);
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
        document.open();
    } catch (DocumentException | FileNotFoundException e) {
        e.printStackTrace();
    }
    return this;
}

p.s. it's part from my code, don't care about PdfStream class.

Where writer and document objects are PdfWriter and Document instances. I think that write all code example here, it isn't good idea, but you can read more about this https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-1

If you have some questions, ask me) Good luck !

lord_hokage
  • 189
  • 7
  • We currently use iText where I work, but we use an old version. The new version cost money. Do you know another open source tool besides Apache POI and PDFbox I could use? Thanks a lot for taking your time to answer – Patricio Vargas Jul 31 '18 at 20:34