我不明白为什么在这个非常基本的iText测试中会出现异常:
package com.itextpdf.testpdf4;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.text.DocumentException;
import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;
@WrapToTest
public class HelloWorld {
public static final String DEST = "result/hello.pdf";
public static void main(String[] args)
throws DocumentException, IOException {
LicenseKey.loadLicenseFile("C:\\dev\\testPDF4\\src\\main\\java\\com\\itextpdf\\testpdf4\\itextkey1544447451310_0.xml");
File file = new File(DEST);
file.getParentFile().mkdirs();
new HelloWorld().createPdf(DEST);
}
public void createPdf(String dest) throws DocumentException, IOException {
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
// Create a PdfFont
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
// Add a Paragraph
document.add(new Paragraph("iText is:").setFont(font));
// Create a List
List list = new List()
.setSymbolIndent(12)
.setListSymbol("\u2022")
.setFont(font);
// Add ListItem objects
list.add(new ListItem("Never gonna give you up"))
.add(new ListItem("Never gonna let you down"))
.add(new ListItem("Never gonna run around and desert you"))
.add(new ListItem("Never gonna make you cry"))
.add(new ListItem("Never gonna say goodbye"))
.add(new ListItem("Never gonna tell a lie and hurt you"));
// Add the list
document.add(list);
//Close document
document.close();
}
}
线程“主”中的异常java.lang.NoClassDefFoundError: com / itextpdf / kernel / pdf / tagutils / DefaultAccessibilityProperties位于 com.itextpdf.testpdf4.HelloWorld.createPdf(HelloWorld.java:56)
(第56行是:document.add(新的Paragraph(“ iText是:”)。setFont(font));)
此代码来自此处:https://developers.itextpdf.com/fr/content/itext-7-jump-start-tutorial/examples/chapter-1-> C01E02_RickAstley.java
在POM.XML中:
<modelVersion>4.0.0</modelVersion>
<groupId>com.itextpdf</groupId>
<artifactId>testPDF4</artifactId>
<version>1.0</version>
(包为:包com.itextpdf.testpdf4;)
这是完整的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.itextpdf</groupId>
<artifactId>testPDF4</artifactId>
<version>1.0</version>
<properties>
<itext.version>7.1.4</itext.version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<junit.version>4.12</junit.version>
</properties>
<repositories>
<repository>
<id>itext</id>
<name>iText Repository - releases</name>
<url>https://repo.itextsupport.com/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdftest</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.18</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-licensekey</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-sandbox-parent</artifactId>
<version>2</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.p12</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<excludePackageNames>com.itextpdf.xml</excludePackageNames>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m5.1</version>
<configuration>
<!-- see goals wiki page for configuration options -->
<flowInitContext>
<masterBranchName>master</masterBranchName>
<developBranchName>develop</developBranchName>
<featureBranchPrefix>feature/</featureBranchPrefix>
<releaseBranchPrefix>release/</releaseBranchPrefix>
<hotfixBranchPrefix>hotfix/</hotfixBranchPrefix>
<versionTagPrefix />
</flowInitContext>
<allowUntracked>true</allowUntracked>
<autoVersionSubmodules>true</autoVersionSubmodules>
<updateDependencies>true</updateDependencies>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>public</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>com/itextpdf/xml/**</exclude>
<exclude>**/*.p12</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>internal</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>**/*.p12</exclude>
</excludes>
<classifier>INTERNAL</classifier>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
有人看到错了吗?我不
谢谢
答案 0 :(得分:0)
您正在混合使用不同的核心itext工件版本7.0.4和7.1.4。
...
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.4</version>
<type>jar</type>
</dependency>
...
不要混在一起。使用所有核心itext工件的相同版本。
顺便说一下,您将测试项目放入了itext组:
<groupId>com.itextpdf</groupId>
<artifactId>testPDF4</artifactId>
您不应该这样做,尤其是不用于生产用途项目时。
答案 1 :(得分:0)
非常感谢mkl; 组IP错误,版本错误以及nbaction.xml错误