Patricio Vargas
Patricio Vargas

Reputation: 5522

ApachePOI pdf creation

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.

Upvotes: 1

Views: 4071

Answers (1)

lord_hokage
lord_hokage

Reputation: 187

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 !

Upvotes: 1

Related Questions