从itext 7.0.4迁移到7.1.11时遇到问题

时间:2020-05-07 08:50:54

标签: itext

我在itext 7中编写了一些代码,可以使用IRandomAccessSource加载大型PDF

IRandomAccessSource rdSource = null;
File pdf = new File(folder, getFilename());
rdSource = new RandomAccessSourceFactory().createBestSource(pdf.getCanonicalPath());

pdfReader = new PdfReader(rdSource, null);
PdfDocument doc = new PdfDocument(pdfReader); // --> Throws NullPointerException with 7.1.11

如果调用PdfDocument doc = new PdfDocument(pdfReader),则会收到NullPointerException,因为我将Maven依赖关系从7.0.4更改为7.1.11

java.lang.NullPointerException
    at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1867)
    at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:252)
    at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:234)
    at pdfTextExtractor.ValidateSendung.execute(ValidateSendung.java:68)
    at pdfTextExtractor.TestValidateSendunge.test2(TestValidateSendunge.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

Maven依赖项是:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>kernel</artifactId>
    <version>7.1.11</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>io</artifactId>
    <version>7.1.11</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>layout</artifactId>
    <version>7.1.11</version>
</dependency> 

任何提示,为什么新版本会发生这种情况? 谢谢

关于米歇尔

Method PDFDocument.open --> Crashes on 
 memoryLimitsAwareHandler = reader.properties.memoryLimitsAwareHandler;
as reader.properties is null

   protected void open(PdfVersion newPdfVersion) {
        this.fingerPrint = new FingerPrint();

        try {
            EventCounterHandler.getInstance().onEvent(CoreEvent.PROCESS, properties.metaInfo, getClass());
            if (reader != null) {
                if (reader.pdfDocument != null) {
                    throw new PdfException(PdfException.PdfReaderHasBeenAlreadyUtilized);
                }
                reader.pdfDocument = this;
                memoryLimitsAwareHandler = reader.properties.memoryLimitsAwareHandler;

1 个答案:

答案 0 :(得分:2)

很显然,需要在新版本中传递ReaderProperties。 我将代码更改为

pdfReader = new PdfReader(rdSource, new ReaderProperties());

现在它可以工作了。 怎么可能在V.7.0.4中起作用,而现在在7.1.11中更改了?

无论如何:问题已解决

致谢