Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/4.0.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* **Associated files (PR #1806).** `PdfFileSpecification.setAFRelationship(PdfName)` and `associateWith(PdfDictionary)`, `PdfWriter.addAssociatedFile` and `addPageAssociatedFile`, and `PdfStamper.addAssociatedFile` link embedded files to the document, a page or another component, as PDF 2.0 and PDF/A-3 define them, for example the XML of a ZUGFeRD or Factur-X invoice.
* **Tagged annotations (PR #1804, port of #1617).** `PdfStructureElement.addAnnotation(PdfAnnotation)` references a link or another annotation from the structure tree, as PDF/UA requires. The structure tree root gets a `/ParentTreeNextKey` entry.
* **PDF/A extension schemas and Factur-X metadata (PR #1808).** `PdfWriter.addXmpSchema(XmpSchema, PdfAExtensionSchema.Schema)` adds custom XMP properties and the PDF/A extension schema that describes them to the metadata of `createXmpMetadata()`. `FacturXSchema` has the `fx` properties of ZUGFeRD and Factur-X invoices, so that a PDF/A-3 invoice with the invoice XML as an associated file passes veraPDF.
* **PDF/X-4 (PR #1809).** `PdfWriter.PDFX4` creates PDF/X-4 (ISO 15930-7:2010) documents for print production, with transparency, layers and ICC based colour. OpenPDF writes PDF 1.6, the PDF/X-4 identification and media management properties in the XMP metadata and `/GTS_PDFXVersion` and `/Trapped` in the info dictionary, and requires an output intent with an ICC profile and embedded fonts.

## 🛠 Bug Fixes

Expand All @@ -15,6 +16,7 @@
* **Fonts:** `PdfContentByte.showText(GlyphVector)` honors the X positions of the glyphs, so complex scripts such as Devanagari are placed correctly (PR #1804, port of #1618).
* **Tagged PDF:** Structure elements after a marked-content identifier in the kids of an element are written to the structure tree (PR #1804).
* **HTML colors:** `WebColors.getRGBColor` throws an `IllegalArgumentException`, as documented, for an `rgb()` or `rgba()` value with fewer than three components instead of a `NoSuchElementException` (PR #1801).
* **PDF/X:** The info dictionary of a PDF/X document gets a `/ModDate`, which PDF/X requires, and the XMP metadata repeats `/Trapped` as `pdf:Trapped` (PR #1809).

## 🧹 Maintenance & Refactoring

Expand Down
27 changes: 27 additions & 0 deletions openpdf-core/src/main/java/org/openpdf/text/pdf/PdfWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ public class PdfWriter extends DocWriter implements
* PDFA-3U level.
*/
public static final int PDFA3U = 10;
/**
* PDF/X-4 (ISO 15930-7:2010), the exchange format for print production that allows transparency, layers and ICC
* based colour. The document is written as PDF 1.6. It needs an output intent with an ICC profile, see
* {@link #setOutputIntents(String, String, String, String, java.awt.color.ICC_Profile)}, and all fonts must be
* embedded. The XMP metadata with the PDF/X-4 identification is created when the document is closed, unless it was
* set before.
*/
public static final int PDFX4 = 11;
/**
* No encryption
*/
Expand Down Expand Up @@ -1358,6 +1366,10 @@ public void close() {
PdfIndirectReference rootRef = root.writePageTree();
// make the catalog-object and add it to the body
PdfDictionary catalog = getCatalog(rootRef);
// PDF/X-4 requires XMP metadata with its identification
if (xmpMetadata == null && isPdfX4()) {
createXmpMetadata();
}
// [C9] if there is XMP data to add: add it
if (xmpMetadata != null) {
PdfStream xmp = new PdfStream(xmpMetadata);
Expand Down Expand Up @@ -1888,6 +1900,10 @@ public void addXmpSchema(XmpSchema schema, PdfAExtensionSchema.Schema descriptio
*/
private byte[] createXmpMetadataBytes() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (isPdfX4()) {
// the metadata of PDF/X-4 repeats the PDF/X keys of the info dictionary, such as Trapped
pdfxConformance.completeInfoDictionary(getInfo());
}
try {
XmpWriter xmp = new XmpWriter(baos, pdf.getInfo(), pdfxConformance.getPDFXConformance());
for (XmpSchema schema : additionalXmpSchemas) {
Expand Down Expand Up @@ -1933,6 +1949,8 @@ public void setPDFXConformance(int pdfx) {
} else if (pdfx == PDFA2A || pdfx == PDFA2B || pdfx == PDFA2U
|| pdfx == PDFA3A || pdfx == PDFA3B || pdfx == PDFA3U) {
setPdfVersion(VERSION_1_7);
} else if (pdfx == PDFX4) {
setPdfVersion(VERSION_1_6);
} else if (pdfx != PDFXNONE) {
setPdfVersion(VERSION_1_3);
}
Expand All @@ -1946,6 +1964,15 @@ public boolean isPdfX() {
return pdfxConformance.isPdfX();
}

/**
* Checks if the PDF has to be in conformance with PDF/X-4.
*
* @return true if the PDF has to be in conformance with PDF/X-4
*/
public boolean isPdfX4() {
return pdfxConformance.isPdfX4();
}

// [F12] tagged PDF

public boolean isPdfA1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openpdf.text.pdf.ExtendedColor;
import org.openpdf.text.pdf.PatternColor;
import org.openpdf.text.pdf.PdfArray;
import org.openpdf.text.pdf.PdfDate;
import org.openpdf.text.pdf.PdfDictionary;
import org.openpdf.text.pdf.PdfGState;
import org.openpdf.text.pdf.PdfImage;
Expand Down Expand Up @@ -229,24 +230,24 @@

/**
* Tells if the conformance level of the writer allows transparency: soft masks, constant alpha and blend modes.
* PDF/A-2 and PDF/A-3 allow it, PDF/A-1, PDF/X-1a and PDF/X-3 do not.
* PDF/A-2, PDF/A-3 and PDF/X-4 allow it, PDF/A-1, PDF/X-1a and PDF/X-3 do not.
*
* @param writer the writer
* @return {@code true} if transparency is allowed
*/
private static boolean allowsTransparency(PdfWriter writer) {
return writer.isPdfA2() || writer.isPdfA3();
return writer.isPdfA2() || writer.isPdfA3() || writer.isPdfX4();
}

/**
* Tells if the conformance level of the writer allows optional content (layers). PDF/A-2 and PDF/A-3 allow it,
* PDF/A-1, PDF/X-1a and PDF/X-3 do not.
* Tells if the conformance level of the writer allows optional content (layers). PDF/A-2, PDF/A-3 and PDF/X-4
* allow it, PDF/A-1, PDF/X-1a and PDF/X-3 do not.
*
* @param writer the writer
* @return {@code true} if optional content is allowed
*/
private static boolean allowsOptionalContent(PdfWriter writer) {
return writer.isPdfA2() || writer.isPdfA3();
return writer.isPdfA2() || writer.isPdfA3() || writer.isPdfX4();
}

/**
Expand Down Expand Up @@ -286,6 +287,15 @@
*
* @return true of the PDF has to be in conformance with PDF/X-3:2002
*/
/**
* Checks if the PDF has to be in conformance with PDF/X-4.
*
* @return true if the PDF has to be in conformance with PDF/X-4
*/
public boolean isPdfX4() {

Check warning on line 295 in openpdf-core/src/main/java/org/openpdf/text/pdf/internal/PdfXConformanceImp.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove or merge the dangling Javadoc comment(s).

See more on https://sonarcloud.io/project/issues?id=LibrePDF_OpenPDF&issues=AaI4sipKeDTtaOHy0P3m&open=AaI4sipKeDTtaOHy0P3m&pullRequest=1809
return pdfxConformance == PdfWriter.PDFX4;
}

public boolean isPdfX32002() {
return pdfxConformance == PdfWriter.PDFX32002;
}
Expand Down Expand Up @@ -339,7 +349,7 @@
return isPdfA1() || isPdfA2() || isPdfA3();
}

public void completeInfoDictionary(PdfDictionary info) {

Check failure on line 352 in openpdf-core/src/main/java/org/openpdf/text/pdf/internal/PdfXConformanceImp.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=LibrePDF_OpenPDF&issues=AaI4sipKeDTtaOHy0P3n&open=AaI4sipKeDTtaOHy0P3n&pullRequest=1809
if (isPdfX() && !isPdfA()) {
if (info.get(PdfName.GTS_PDFXVERSION) == null) {
if (isPdfX1A2001()) {
Expand All @@ -347,6 +357,8 @@
info.put(new PdfName("GTS_PDFXConformance"), new PdfString("PDF/X-1a:2001"));
} else if (isPdfX32002()) {
info.put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-3:2002"));
} else if (isPdfX4()) {
info.put(PdfName.GTS_PDFXVERSION, new PdfString("PDF/X-4"));
}
}
if (info.get(PdfName.TITLE) == null) {
Expand All @@ -358,10 +370,19 @@
if (info.get(PdfName.TRAPPED) == null) {
info.put(PdfName.TRAPPED, new PdfName("False"));
}
// PDF/X requires the modification date as well as the creation date
if (info.get(PdfName.MODDATE) == null) {
PdfObject creationDate = info.get(PdfName.CREATIONDATE);
info.put(PdfName.MODDATE, creationDate != null ? creationDate : new PdfDate());
}
}
}

public void completeExtraCatalog(PdfDictionary extraCatalog) {
if (isPdfX4()) {
checkPdfX4OutputIntent(extraCatalog);
return;
}
if (isPdfX() && !isPdfA()) {
if (extraCatalog.get(PdfName.OUTPUTINTENTS) == null) {
PdfDictionary out = new PdfDictionary(PdfName.OUTPUTINTENT);
Expand All @@ -374,4 +395,26 @@
}
}
}

/**
* Checks that a PDF/X-4 document has a PDF/X output intent with an embedded ICC profile (ISO 15930-7, 6.2.4). Unlike
* PDF/X-1a and PDF/X-3, PDF/X-4 does not allow an output intent that only names a registered characterization.
*
* @param extraCatalog the extra keys of the catalog, with the output intents
* @throws PdfXConformanceException if the output intent or its ICC profile is missing
*/
private static void checkPdfX4OutputIntent(PdfDictionary extraCatalog) {
PdfArray outputIntents = extraCatalog.getAsArray(PdfName.OUTPUTINTENTS);
if (outputIntents != null) {
for (int i = 0; i < outputIntents.size(); i++) {
PdfDictionary outputIntent = outputIntents.getAsDict(i);
if (outputIntent != null && PdfName.GTS_PDFX.equals(outputIntent.getAsName(PdfName.S))
&& outputIntent.get(PdfName.DESTOUTPUTPROFILE) != null) {
return;
}
}
}
throw new PdfXConformanceException("PDF/X-4 requires an output intent with an ICC profile. "
+ "Use PdfWriter.setOutputIntents(String, String, String, String, ICC_Profile).");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public class PdfSchema extends XmpSchema {
* The Producer.
*/
public static final String PRODUCER = "pdf:Producer";
/**
* Tells if the document has been trapped: True, False or Unknown.
*/
public static final String TRAPPED = "pdf:Trapped";
private static final long serialVersionUID = -1541148669123992185L;


Expand Down
25 changes: 25 additions & 0 deletions openpdf-core/src/main/java/org/openpdf/text/xml/xmp/XmpWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.UUID;

/**
* With this class you can create an Xmp Stream that can be used for adding Metadata to a PDF Dictionary. Remark that
Expand Down Expand Up @@ -194,6 +195,12 @@
}
if (PdfName.MODDATE.equals(key)) {
basic.addModDate(((PdfDate) obj).getW3CDate());
if (PdfXConformance == PdfWriter.PDFX4) {
basic.setProperty(XmpBasicSchema.METADATADATE, ((PdfDate) obj).getW3CDate());
}
}
if (PdfName.TRAPPED.equals(key) && obj.isName()) {
p.setProperty(PdfSchema.TRAPPED, PdfName.decodeName(obj.toString()));
}
}
if (dc.size() > 0) {
Expand Down Expand Up @@ -237,6 +244,8 @@
pdfA3Schema.addConformance("U");
}
addRdfDescription(pdfA3Schema);
} else if (PdfXConformance == PdfWriter.PDFX4) {
addPdfX4Identification();
}
}
}
Expand Down Expand Up @@ -302,6 +311,22 @@
/**
* Sets the XMP to read-only
*/
/**
* Writes the identification of PDF/X-4 (ISO 15930-7, 6.2.2): the version in {@code pdfxid:GTS_PDFXVersion} and
* the document ID, version ID and rendition class of the XMP media management schema.
*
* @throws IOException on error
*/
private void addPdfX4Identification() throws IOException {

Check warning on line 320 in openpdf-core/src/main/java/org/openpdf/text/xml/xmp/XmpWriter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove or merge the dangling Javadoc comment(s).

See more on https://sonarcloud.io/project/issues?id=LibrePDF_OpenPDF&issues=AaI4sipdeDTtaOHy0P3o&open=AaI4sipdeDTtaOHy0P3o&pullRequest=1809
addRdfDescription("xmlns:pdfxid=\"http://www.npes.org/pdfx/ns/id/\"",
"<pdfxid:GTS_PDFXVersion>PDF/X-4</pdfxid:GTS_PDFXVersion>");
XmpMMSchema mm = new XmpMMSchema();
mm.setProperty(XmpMMSchema.DOCUMENTID, "uuid:" + UUID.randomUUID());
mm.setProperty(XmpMMSchema.VERSIONID, "1");
mm.setProperty(XmpMMSchema.RENDITIONCLASS, "default");
addRdfDescription(mm);
}

public void setReadOnly() {
end = 'r';
}
Expand Down
Loading
Loading