Skip to content

Commit

Permalink
Provide PDF/A conform annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
vuilleumierc authored and asturio committed Mar 4, 2024
1 parent 41112eb commit 3a1b1aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public PdfAnnotation(PdfWriter writer, Rectangle rect) {
if (rect != null) {
put(PdfName.RECT, new PdfRectangle(rect));
}
addFKey();
}

/**
Expand All @@ -239,6 +240,7 @@ public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ur
put(PdfName.T, title);
put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
put(PdfName.CONTENTS, content);
addFKey();
}

/**
Expand All @@ -259,6 +261,7 @@ public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ur
put(PdfName.A, action);
put(PdfName.BORDER, new PdfBorderArray(0, 0, 0));
put(PdfName.C, new PdfColor(0x00, 0x00, 0xFF));
addFKey();
}

/**
Expand Down Expand Up @@ -1073,4 +1076,10 @@ public String toString() {
return buf.toString();
}
}

private void addFKey() {
if (writer.isPdfA1()) {
put(PdfName.F, new PdfNumber(FLAGS_PRINT));
}
}
}
4 changes: 4 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,10 @@ public boolean isPdfX() {
return pdfxConformance.isPdfX();
}

public boolean isPdfA1() {
return pdfxConformance.isPdfA1();
}

// [C11] Output intents

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.verapdf.core.ModelParsingException;
import org.verapdf.gf.model.GFModelParser;
Expand All @@ -24,10 +25,12 @@
public class PDFValidationTest {

@Test
void validatePDFWithVera() throws Exception {
public void testValidatePDFWithVera() throws Exception {
Document document = new Document(PageSize.A4);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(document, byteArrayOutputStream);
PdfWriter pdfWriter = PdfWriter.getInstance(document, byteArrayOutputStream);
pdfWriter.setPDFXConformance(PdfWriter.PDFA1B);
pdfWriter.createXmpMetadata();

try {
document.open();
Expand All @@ -39,7 +42,7 @@ void validatePDFWithVera() throws Exception {
document.close();

// Create a veraPDF validator
PDFAFlavour flavour = PDFAFlavour.PDFA_1_A;
PDFAFlavour flavour = PDFAFlavour.PDFA_1_B;
try (InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
GFModelParser parser = GFModelParser.createModelWithFlavour(inputStream, flavour)) {
PDFAValidator validator = ValidatorFactory.createValidator(flavour, false, 10);
Expand All @@ -56,6 +59,7 @@ void validatePDFWithVera() throws Exception {
}

}
Assertions.assertTrue(result.isCompliant());
}
} catch (ModelParsingException e) {
e.printStackTrace();
Expand Down

0 comments on commit 3a1b1aa

Please sign in to comment.