Skip to content

Commit c465c21

Browse files
committed
Fixed an issue where .getFontFromAcroForm() method was called on a pdf without an AcroForm, resulting in a NullPointerException.
This is fixed by adding an extra check on the availability of the AcroForm before querying it.
1 parent 1828552 commit c465c21

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/main/java/com/itextpdf/pdfcleanup/PdfCleanUpTool.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,17 @@ private void drawRolloverAppearance(PdfCanvas canvas, PdfStream redactRolloverAp
453453
}
454454

455455
private void drawOverlayText(PdfCanvas canvas, String overlayText, Rectangle annotRect, PdfBoolean repeat, PdfString defaultAppearance, int justification) throws IOException {
456-
Map<String, List> parsedDA = parseDAParam(defaultAppearance);
456+
Map<String, List> parsedDA;
457+
try {
458+
parsedDA = parseDAParam(defaultAppearance);
459+
}catch (NullPointerException npe){
460+
throw new PdfException(PdfException.DefaultAppearanceNotFound);
461+
}
457462
PdfFont font;
458463
float fontSize = 12;
459464
List fontArgs = parsedDA.get("Tf");
460-
if (fontArgs != null) {
465+
PdfDictionary formDictionary = pdfDocument.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
466+
if (fontArgs != null && formDictionary != null) {
461467
font = getFontFromAcroForm((PdfName) fontArgs.get(0));
462468
fontSize = ((PdfNumber) fontArgs.get(1)).floatValue();
463469
} else {

src/test/java/com/itextpdf/pdfcleanup/PdfCleanUpToolTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,24 @@ This file is part of the iText (R) project.
4444

4545

4646
import com.itextpdf.kernel.color.ColorConstants;
47+
import com.itextpdf.kernel.PdfException;
4748
import com.itextpdf.kernel.geom.PageSize;
4849
import com.itextpdf.kernel.geom.Rectangle;
4950
import com.itextpdf.kernel.pdf.PdfDocument;
5051
import com.itextpdf.kernel.pdf.PdfReader;
52+
import com.itextpdf.kernel.pdf.PdfString;
5153
import com.itextpdf.kernel.pdf.PdfWriter;
54+
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
55+
import com.itextpdf.kernel.pdf.annot.PdfRedactAnnotation;
5256
import com.itextpdf.kernel.utils.CompareTool;
5357
import com.itextpdf.test.ExtendedITextTest;
5458
import com.itextpdf.test.annotations.type.IntegrationTest;
5559
import org.junit.Assert;
5660
import org.junit.BeforeClass;
61+
import org.junit.Rule;
5762
import org.junit.Test;
5863
import org.junit.experimental.categories.Category;
64+
import org.junit.rules.ExpectedException;
5965

6066
import java.io.IOException;
6167
import java.util.Arrays;
@@ -67,6 +73,9 @@ public class PdfCleanUpToolTest extends ExtendedITextTest {
6773
private static final String inputPath = "./src/test/resources/com/itextpdf/pdfcleanup/PdfCleanUpToolTest/";
6874
private static final String outputPath = "./target/test/com/itextpdf/pdfcleanup/PdfCleanUpToolTest/";
6975

76+
@Rule
77+
public ExpectedException junitExpectedException = ExpectedException.none();
78+
7079
@BeforeClass
7180
public static void before() {
7281
createOrClearDestinationFolder(outputPath);
@@ -537,6 +546,46 @@ public void cleanUpTest44() throws IOException, InterruptedException {
537546
compareByContent(cmp, output, outputPath, "diff_44");
538547
}
539548

549+
@Test
550+
public void cleanUpTest45() throws IOException, InterruptedException {
551+
String input = inputPath + "emptyPdf.pdf";
552+
String output = outputPath + "emptyPdf.pdf";
553+
String cmp = inputPath + "cmp_emptyPdf.pdf";
554+
555+
PdfAnnotation redactAnnotation = new PdfRedactAnnotation(new Rectangle(97, 405, 383, 40))
556+
.setOverlayText(new PdfString("OverlayTest"))
557+
.setDefaultAppearance(new PdfString("/Helv 0 Tf 0 g"));
558+
559+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
560+
561+
pdfDocument.getFirstPage().addAnnotation(redactAnnotation);
562+
563+
new PdfCleanUpTool(pdfDocument, true).cleanUp();
564+
565+
pdfDocument.close();
566+
compareByContent(cmp, output, outputPath, "diff_45");
567+
}
568+
569+
@Test
570+
public void cleanUpTest46() throws IOException {
571+
junitExpectedException.expect(PdfException.class);
572+
junitExpectedException.expectMessage(PdfException.DefaultAppearanceNotFound);
573+
574+
String input = inputPath + "emptyPdf.pdf";
575+
String output = outputPath + "emptyPdf.pdf";
576+
577+
PdfAnnotation redactAnnotation = new PdfRedactAnnotation(new Rectangle(97, 405, 383, 40))
578+
.setOverlayText(new PdfString("OverlayTest"));
579+
580+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
581+
582+
pdfDocument.getFirstPage().addAnnotation(redactAnnotation);
583+
584+
new PdfCleanUpTool(pdfDocument, true).cleanUp();
585+
586+
pdfDocument.close();
587+
}
588+
540589
private void cleanUp(String input, String output, List<PdfCleanUpLocation> cleanUpLocations) throws IOException {
541590
PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
542591

Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)