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
1 change: 1 addition & 0 deletions changelogs/4.0.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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.

## 🛠 Bug Fixes

Expand Down
43 changes: 43 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 @@ -73,6 +73,8 @@
import org.openpdf.text.pdf.interfaces.PdfXConformance;
import org.openpdf.text.pdf.internal.PdfVersionImp;
import org.openpdf.text.pdf.internal.PdfXConformanceImp;
import org.openpdf.text.xml.xmp.PdfAExtensionSchema;
import org.openpdf.text.xml.xmp.XmpSchema;
import org.openpdf.text.xml.xmp.XmpWriter;
import java.awt.Color;
import java.awt.color.ICC_Profile;
Expand All @@ -89,6 +91,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
Expand Down Expand Up @@ -573,6 +576,16 @@ public class PdfWriter extends DocWriter implements
* The files associated with the whole document, written as the {@code /AF} array of the catalog.
*/
private PdfArray documentAssociatedFiles;

/**
* Additional XMP schemas that {@link #createXmpMetadata()} writes.
*/
private final List<XmpSchema> additionalXmpSchemas = new ArrayList<>();

/**
* The PDF/A extension schemas that describe {@link #additionalXmpSchemas}.
*/
private final PdfAExtensionSchema pdfAExtensionSchema = new PdfAExtensionSchema();
/**
* The root of the page tree.
*/
Expand Down Expand Up @@ -1846,13 +1859,43 @@ public void createXmpMetadata() {
setXmpMetadata(createXmpMetadataBytes());
}

/**
* Adds an XMP schema with properties of a standard namespace, for example Dublin Core, to the metadata that
* {@link #createXmpMetadata()} creates. Add the schemas before calling {@link #createXmpMetadata()}.
*
* @param schema the schema with its properties
*/
public void addXmpSchema(XmpSchema schema) {
additionalXmpSchemas.add(Objects.requireNonNull(schema, "schema"));
}

/**
* Adds an XMP schema with properties of a custom namespace to the metadata that {@link #createXmpMetadata()}
* creates, together with the PDF/A extension schema that describes the namespace. PDF/A requires this description
* for every namespace that is not predefined. An example is the {@link org.openpdf.text.xml.xmp.FacturXSchema} of
* a ZUGFeRD or Factur-X invoice in a PDF/A-3 document. Add the schemas before calling {@link #createXmpMetadata()}.
*
* @param schema the schema with its properties
* @param description the description of the namespace and its properties
*/
public void addXmpSchema(XmpSchema schema, PdfAExtensionSchema.Schema description) {
addXmpSchema(schema);
pdfAExtensionSchema.addSchema(Objects.requireNonNull(description, "description"));
}

/**
* @return an XmpMetadata byte array
*/
private byte[] createXmpMetadataBytes() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
XmpWriter xmp = new XmpWriter(baos, pdf.getInfo(), pdfxConformance.getPDFXConformance());
for (XmpSchema schema : additionalXmpSchemas) {
xmp.addRdfDescription(schema);
}
if (!pdfAExtensionSchema.isEmpty()) {
xmp.addRdfDescription(pdfAExtensionSchema);
}
xmp.close();
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "Could not create the XMP metadata", ioe);
Expand Down
182 changes: 182 additions & 0 deletions openpdf-core/src/main/java/org/openpdf/text/xml/xmp/FacturXSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package org.openpdf.text.xml.xmp;

import java.util.List;

/**
* The XMP properties of a ZUGFeRD or Factur-X electronic invoice ({@code fx} prefix). A ZUGFeRD 2.1 or later and
* Factur-X invoice is a PDF/A-3 document with the invoice XML as an associated file. The XMP metadata names that file
* and the profile of the invoice with these properties, and describes them in a PDF/A extension schema, see
* {@link #getPdfAExtensionSchema()}.
* <pre>{@code
* FacturXSchema fx = new FacturXSchema();
* fx.setDocumentFileName(FacturXSchema.FACTUR_X_FILE_NAME);
* fx.setConformanceLevel(FacturXSchema.EN_16931);
* writer.addXmpSchema(fx, fx.getPdfAExtensionSchema());
* writer.createXmpMetadata();
* }</pre>
*/
public class FacturXSchema extends XmpSchema {

/**
* The namespace URI of Factur-X 1.0 and ZUGFeRD 2.1 and later.
*/
public static final String FACTUR_X_NAMESPACE_URI = "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#";
/**
* The namespace URI of ZUGFeRD 2.0.
*/
public static final String ZUGFERD_2P0_NAMESPACE_URI = "urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0#";
/**
* The namespace prefix of the properties.
*/
public static final String PREFIX = "fx";
/**
* The name of the invoice XML in Factur-X and ZUGFeRD 2.1 and later.
*/
public static final String FACTUR_X_FILE_NAME = "factur-x.xml";
/**
* The name of the invoice XML of an XRechnung profile in ZUGFeRD 2.1 and later.
*/
public static final String XRECHNUNG_FILE_NAME = "xrechnung.xml";
/**
* The document type of an invoice.
*/
public static final String INVOICE = "INVOICE";
/**
* The conformance level (profile) MINIMUM.
*/
public static final String MINIMUM = "MINIMUM";
/**
* The conformance level (profile) BASIC WL.
*/
public static final String BASIC_WL = "BASIC WL";
/**
* The conformance level (profile) BASIC.
*/
public static final String BASIC = "BASIC";
/**
* The conformance level (profile) EN 16931, also called COMFORT.
*/
public static final String EN_16931 = "EN 16931";
/**
* The conformance level (profile) EXTENDED.
*/
public static final String EXTENDED = "EXTENDED";
/**
* The conformance level (profile) XRECHNUNG.
*/
public static final String XRECHNUNG = "XRECHNUNG";

/**
* The property with the name of the invoice XML.
*/
public static final String DOCUMENT_FILE_NAME = "fx:DocumentFileName";
/**
* The property with the document type.
*/
public static final String DOCUMENT_TYPE = "fx:DocumentType";
/**
* The property with the version of the XML schema of the invoice.
*/
public static final String VERSION = "fx:Version";
/**
* The property with the conformance level (profile) of the invoice.
*/
public static final String CONFORMANCE_LEVEL = "fx:ConformanceLevel";

private static final long serialVersionUID = -4520412961417532094L;

private final String namespaceUri;

/**
* Creates the properties of a Factur-X or ZUGFeRD 2.1 or later invoice, with the document type
* {@link #INVOICE} and the version {@code 1.0}.
*/
public FacturXSchema() {
this(FACTUR_X_NAMESPACE_URI);
}

/**
* Creates the properties of an invoice with the document type {@link #INVOICE} and the version {@code 1.0}.
*
* @param namespaceUri the namespace URI, {@link #FACTUR_X_NAMESPACE_URI} or {@link #ZUGFERD_2P0_NAMESPACE_URI}
*/
public FacturXSchema(String namespaceUri) {
super("xmlns:" + PREFIX + "=\"" + namespaceUri + "\"");
this.namespaceUri = namespaceUri;
setDocumentType(INVOICE);
setVersion("1.0");
}

/**
* Sets the name of the invoice XML, which is the name of the associated file.
*
* @param fileName the file name, for example {@link #FACTUR_X_FILE_NAME}
*/
public void setDocumentFileName(String fileName) {
setProperty(DOCUMENT_FILE_NAME, fileName);
}

/**
* Sets the document type.
*
* @param documentType the document type, usually {@link #INVOICE}
*/
public void setDocumentType(String documentType) {
setProperty(DOCUMENT_TYPE, documentType);
}

/**
* Sets the version of the XML schema of the invoice.
*
* @param version the version, for example {@code 1.0}
*/
public void setVersion(String version) {
setProperty(VERSION, version);
}

/**
* Sets the conformance level (profile) of the invoice.
*
* @param conformanceLevel the conformance level, for example {@link #EN_16931}
*/
public void setConformanceLevel(String conformanceLevel) {
setProperty(CONFORMANCE_LEVEL, conformanceLevel);
}

/**
* Gets the namespace URI of the properties.
*
* @return the namespace URI
*/
public String getNamespaceUri() {
return namespaceUri;
}

/**
* Gets the PDF/A extension schema that describes these properties. PDF/A requires it in the XMP metadata.
*
* @return the description of the schema
*/
public PdfAExtensionSchema.Schema getPdfAExtensionSchema() {
String external = PdfAExtensionSchema.Property.EXTERNAL;
return new PdfAExtensionSchema.Schema("Factur-X PDFA Extension Schema", namespaceUri, PREFIX, List.of(
new PdfAExtensionSchema.Property("DocumentFileName", "Text", external,
"The name of the embedded XML document"),
new PdfAExtensionSchema.Property("DocumentType", "Text", external,
"The type of the hybrid document in capital letters, e.g. INVOICE or ORDER"),
new PdfAExtensionSchema.Property("Version", "Text", external,
"The actual version of the standard applying to the embedded XML document"),
new PdfAExtensionSchema.Property("ConformanceLevel", "Text", external,
"The conformance level of the embedded XML document")));
}

@Override
public synchronized boolean equals(Object o) {
return super.equals(o) && o instanceof FacturXSchema other && namespaceUri.equals(other.namespaceUri);
}

@Override
public synchronized int hashCode() {
return 31 * super.hashCode() + namespaceUri.hashCode();
}
}
Loading
Loading