Warm tip: This article is reproduced from serverfault.com, please click

java-itext7裸露的骨头你好世界示例

(java - Itext7 bare bones hello world example)

发布于 2020-12-28 12:15:58

因此,我从https://github.com/itext/itext7/releases/latest下载了已编译的itext jar文件 ,并将它们放置在与iText示例C01E01_HelloWorld.java相同的文件夹中,但是当我运行时

javac C01E01_HelloWorld.java

我懂了

$ javac C01E01_HelloWorld.java 
C01E01_HelloWorld.java:3: error: package com.itextpdf.kernel.pdf does not exist
import com.itextpdf.kernel.pdf.PdfDocument;
                              ^
C01E01_HelloWorld.java:4: error: package com.itextpdf.kernel.pdf does not exist
import com.itextpdf.kernel.pdf.PdfWriter;
                              ^
C01E01_HelloWorld.java:5: error: package com.itextpdf.layout does not exist
import com.itextpdf.layout.Document;
                          ^
C01E01_HelloWorld.java:6: error: package com.itextpdf.layout.element does not exist
import com.itextpdf.layout.element.Paragraph;
                                  ^
C01E01_HelloWorld.java:25: error: cannot find symbol
        PdfWriter writer = new PdfWriter(dest);
        ^
  symbol:   class PdfWriter
  location: class C01E01_HelloWorld
C01E01_HelloWorld.java:25: error: cannot find symbol
        PdfWriter writer = new PdfWriter(dest);
                               ^
  symbol:   class PdfWriter
  location: class C01E01_HelloWorld
C01E01_HelloWorld.java:28: error: cannot find symbol
        PdfDocument pdf = new PdfDocument(writer);
        ^
  symbol:   class PdfDocument
  location: class C01E01_HelloWorld
C01E01_HelloWorld.java:28: error: cannot find symbol
        PdfDocument pdf = new PdfDocument(writer);
                              ^
  symbol:   class PdfDocument
  location: class C01E01_HelloWorld
C01E01_HelloWorld.java:31: error: cannot find symbol
        Document document = new Document(pdf);
        ^
  symbol:   class Document
  location: class C01E01_HelloWorld
C01E01_HelloWorld.java:31: error: cannot find symbol
        Document document = new Document(pdf);
                                ^
  symbol:   class Document
  location: class C01E01_HelloWorld
C01E01_HelloWorld.java:34: error: cannot find symbol
        document.add(new Paragraph("Hello World!"));
                         ^
  symbol:   class Paragraph
  location: class C01E01_HelloWorld
11 errors

我也试过了

javac -cp /home/user01/itext/demo/ C01E01_HelloWorld.java

即-cp指向iText jar(和C01E01_HelloWorld.java)具有相同结果的位置。如何获取导入行以了解和使用iText jar文件?

还是不能以简单的方式完成此操作,而需要蚀或Maven或?

所以我跑了以下没有错误

javac -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar C01E01_HelloWorld.java 

如果我遗漏了任何jar文件,则会导致与丢失的jar有关的错误。但是当我尝试

java C01E01_HelloWorld 

我懂了

Error: Unable to initialize main class C01E01_HelloWorld
Caused by: java.lang.NoClassDefFoundError: com/itextpdf/layout/element/IBlockElement

我也试过了

java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar C01E01_HelloWorld

这给了稍有不同

Error: Could not find or load main class C01E01_HelloWorld
Caused by: java.lang.ClassNotFoundException: C01E01_HelloWorld

所以下一步产生

$ java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:. C01E01_HelloWorld
Exception in thread "main" java.lang.NullPointerException at C01E01_HelloWorld.main(C01E01_HelloWorld.java:21)

我的来源是

/*package tutorial.chapter01;*/

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.File;
import java.io.IOException;

/**
 * Simple Hello World example.
 */
public class C01E01_HelloWorld {
    
    /* public static final String DEST = "results/chapter01/hello_world.pdf";  */
    
    public static final String DEST = "hello_world.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E01_HelloWorld().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF writer
        PdfWriter writer = new PdfWriter(dest);

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(writer);
        
        // Initialize document
        Document document = new Document(pdf);

        //Add paragraph to the document
        document.add(new Paragraph("Hello World!"));

        //Close document
        document.close();
    }
}
Questioner
kalpha
Viewed
0
kalpha 2020-12-29 18:51:00

所以我使用提供的链接下载了slf4j.api,slf4j-log4j12和log4j

SLF4J:iText7到底需要哪些.jar文件?

并从以下位置下载了已编译的itext jar文件:

https://github.com/itext/itext7/releases/latest

然后我跑了

javac -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:slf4j.api-1.6.1.jar:slf4j-log4j12-1.6.1.jar:log4j-1.2.16.jar C01E01_HelloWorld.java

然后

java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:slf4j.api-1.6.1.jar:slf4j-log4j12-1.6.1.jar:log4j-1.2.16.jar:. C01E01_HelloWorld

最后,我得到了所谓的简单的hello world pdf。

我已经尝试过maven,eclipse和现在的IDEA,但未能按照教程或提示进行任何工作。