Caused by: java.lang.NullPointerException
at com.aspose.pdf.kit.g8.a(Unknown Source)
at com.aspose.pdf.kit.g8.int(Unknown Source)
at com.aspose.pdf.kit.t.V(Unknown Source)
at com.aspose.pdf.kit.h6.a(Unknown Source)
at com.aspose.pdf.kit.hf.a(Unknown Source)
at com.aspose.pdf.kit.PdfExtractor.extractText(Unknown Source)
For the PdfContentEditor.replaceText(), I can easily get it to successfully replaceText ONCE, and save the PDF.
However, I cannot figure out any way to successfully call the replaceText() method a second time for the same PDF.
I’ve tried a variety of work-arounds, using various combinations of ByteArray and/or File InputStream/OutputStream,
binding directly to the filePath, all of which fail with slightly different stack traces.
Here is some very basic test code to illustrate my problem. I’m using the Aspose.Pdf.Kit jar version 2.3.0.
We have an Aspose.Total java license, but it works the same in the evaluation mode.
We’re using java 1.5.
This test uses the pdftemplate.pdf file from the examples/resources directory in Aspose.Pdf.Kit 2.3.0:
File inputFile = new File(“C:\temp\pdftemplate.pdf”);
File outputFile = new File(“C:\temp\pdftemplate-out.pdf”);
PdfExtractor extractor = new PdfExtractor();
extractor.bindPdf(inputFile.getPath());
extractor.extractText();
// output to ByteArray
ByteArrayOutputStream baosPdfText = new ByteArrayOutputStream();
extractor.getText(baosPdfText);
String pdfTextString = baosPdfText.toString();
baosPdfText.close();
// this always works, first time around
System.out.println(“1st extract text: " + pdfTextString);
// If you comment out these 5 lines, extractor2 (below) will work
PdfContentEditor editor = new PdfContentEditor();
editor.bindPdf(inputFile.getPath());
editor.replaceText(”", “My Image Name”);
// a second call to replaceText throws NullPointerException at pdf.kit.g8.a()
// editor.replaceText("", “My Image2 Name”);
editor.save(outputFile.getPath());
// Extract text again
PdfExtractor extractor2 = new PdfExtractor();
extractor2.bindPdf(inputFile.getPath());
// extractText() fails if replaceText() method has been called
extractor2.extractText();
ByteArrayOutputStream baosPdfText2 = new ByteArrayOutputStream();
extractor2.getText(baosPdfText2);
String pdfTextString2 = baosPdfText2.toString();
baosPdfText2.close();
System.out.println("2nd extract text: " + pdfTextString2);
I had suspected that there is some problem with files getting properly closed, and so refactored to use ByteArray
Input and Output Streams, but some of the stack traces seemed to indicate that internally, PdfKit is trying to
use a java.io.RandomAccessFile to do its work. I’ve tried many combinations of Inputs & Outputs, but nothing
seems to get around this bug. What I’m REALLY confused about is why the call to replaceText would
corrupt any subsequent calls to extractText().
Are there any plans for bug-fixes in the works? I’ve looked through many threads here in the forum, and it
seems that PdfKit is one of the lower priority items in the development queue. This has been frustrating, because
it seems to ALMOST work, ALMOST do exactly what we need, but not quite.
John Phillips
Developer, Direxxis, Inc.