Netbeans 中的基本 Maven iText 程序

Basic Maven iText program in Netbeans

我需要从数据库中的一些数据生成一个 Pdf 文档,我遇到了 iTEXT 7。我刚刚在 Netbeans 上开始了我的第一个 Maven 项目(从一个基本的 hello world 开始),这是输出:

Building mavenproject2 1.0-SNAPSHOT

Exception in thread "main" java.lang.NullPointerException
 at com.mycompany.mavenproject2.class2.main(class2.java:18) BUILD FAILURE [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

代码:

package com.mycompany.mavenproject2;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.*;

public class class2 {
    public static void main(String args[]) throws FileNotFoundException 
    {
        File file = new File("hello.pdf");
        file.getParentFile().mkdirs();
        PdfWriter writer = new PdfWriter("hello.pdf");
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
}

pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenproject2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <repositories>
        <repository>
            <id>itext</id>
            <name>iText Repository - releases</name>
            <url>https://repo.itextsupport.com/releases</url>
        </repository>
    </repositories>

    <dependencies>

        <!-- add all iText Core modules -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.0.3</version>
            <type>pom</type>
        </dependency>

        <!-- pdfSweep -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>cleanup</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- pdfCalligraph -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>typography</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- pdfInvoice -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>zugferd</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!-- pdfHTML -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- pdfXFA -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>pdfxfa</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!-- iText 7 License Key Library -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-licensekey</artifactId>
            <version>2.0.4</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

这是我用于简单项目的 POM 文件:

<repositories>
    <repository>
        <id>itext-releases</id>
        <name>iText Repository - releases</name>
        <url>https://repo.itextsupport.com/releases</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>     

...

<dependencies>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-licensekey</artifactId>
        <version>2.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.4</version>
    </dependency>

    ...

</dependencies>

正如评论中所建议的,它包括发行版(许可证密钥需要)和对许可证密钥存储库的依赖。