use of org.apache.xmpbox.schema.PDFAIdentificationSchema in project mustangproject by ZUGFeRD.
the class ZUGFeRDExporter method makeDocPDFA3compliant.
private PDDocumentCatalog makeDocPDFA3compliant(String producer, String creator, boolean attachZugferdHeaders) throws IOException, TransformerException {
String fullProducer = producer + " (via mustangproject.org " + Version.VERSION + ")";
PDDocumentCatalog cat = doc.getDocumentCatalog();
PDMetadata metadata = new PDMetadata(doc);
cat.setMetadata(metadata);
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
PDFAIdentificationSchema pdfaid = new PDFAIdentificationSchema(xmp);
xmp.addSchema(pdfaid);
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
dc.addCreator(creator);
XMPBasicSchema xsb = xmp.createAndAddXMPBasicSchema();
xsb.setCreatorTool(creator);
xsb.setCreateDate(GregorianCalendar.getInstance());
// PDDocumentInformation pdi=doc.getDocumentInformation();
PDDocumentInformation pdi = new PDDocumentInformation();
pdi.setProducer(fullProducer);
pdi.setAuthor(creator);
doc.setDocumentInformation(pdi);
AdobePDFSchema pdf = xmp.createAndAddAdobePDFSchema();
pdf.setProducer(fullProducer);
/*
*
* To be on the safe side, we use level B without Markinfo because we
* can not guarantee that the user correctly tagged the templates for
* the PDF.
*/
try {
//$NON-NLS-1$ //$NON-NLS-1$
pdfaid.setConformance(conformanceLevel.getLetter());
} catch (BadFieldValueException ex) {
// supplied, however the enum enforces that the conformance level is valid.
throw new Error(ex);
}
pdfaid.setPart(3);
if (attachZugferdHeaders) {
addZugferdXMP(xmp);
/*
* this is the only line where we do something
* Zugferd-specific, i.e. add PDF metadata
* specifically for Zugferd, not generically for
* a embedded file
*/
}
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(xmp, baos, false);
metadata.importXMPMetadata(baos.toByteArray());
return cat;
}
use of org.apache.xmpbox.schema.PDFAIdentificationSchema in project pdfbox by apache.
the class CreatePDFA method main.
public static void main(String[] args) throws IOException, TransformerException {
if (args.length != 3) {
System.err.println("usage: " + CreatePDFA.class.getName() + " <output-file> <Message> <ttf-file>");
System.exit(1);
}
String file = args[0];
String message = args[1];
String fontfile = args[2];
try (PDDocument doc = new PDDocument()) {
PDPage page = new PDPage();
doc.addPage(page);
// load the font as this needs to be embedded
PDFont font = PDType0Font.load(doc, new File(fontfile));
//
if (!font.isEmbedded()) {
throw new IllegalStateException("PDF/A compliance requires that all fonts used for" + " text rendering in rendering modes other than rendering mode 3 are embedded.");
}
// create a page with the message
try (PDPageContentStream contents = new PDPageContentStream(doc, page)) {
contents.beginText();
contents.setFont(font, 12);
contents.newLineAtOffset(100, 700);
contents.showText(message);
contents.endText();
}
// add XMP metadata
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
try {
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
dc.setTitle(file);
PDFAIdentificationSchema id = xmp.createAndAddPFAIdentificationSchema();
id.setPart(1);
id.setConformance("B");
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(xmp, baos, true);
PDMetadata metadata = new PDMetadata(doc);
metadata.importXMPMetadata(baos.toByteArray());
doc.getDocumentCatalog().setMetadata(metadata);
} catch (BadFieldValueException e) {
// won't happen here, as the provided value is valid
throw new IllegalArgumentException(e);
}
// sRGB output intent
InputStream colorProfile = CreatePDFA.class.getResourceAsStream("/org/apache/pdfbox/resources/pdfa/sRGB.icc");
PDOutputIntent intent = new PDOutputIntent(doc, colorProfile);
intent.setInfo("sRGB IEC61966-2.1");
intent.setOutputCondition("sRGB IEC61966-2.1");
intent.setOutputConditionIdentifier("sRGB IEC61966-2.1");
intent.setRegistryName("http://www.color.org");
doc.getDocumentCatalog().addOutputIntent(intent);
doc.save(file);
}
}
use of org.apache.xmpbox.schema.PDFAIdentificationSchema in project pdfbox by apache.
the class PDFAIdentificationValidation method validatePDFAIdentifer.
/**
* Check if PDFAIdentification is valid
*
* @param metadata the XMP MetaData.
* @return the list of validation errors.
* @throws ValidationException
*/
public List<ValidationError> validatePDFAIdentifer(XMPMetadata metadata) throws ValidationException {
List<ValidationError> ve = new ArrayList<>();
PDFAIdentificationSchema id = metadata.getPDFIdentificationSchema();
if (id == null) {
ve.add(new ValidationError(ERROR_METADATA_PDFA_ID_MISSING, "PDF/A identification schema " + PDFAIdentificationSchema.class.getAnnotation(StructuredType.class).namespace() + " is missing"));
return ve;
}
// According to the PDF/A specification, the prefix must be pdfaid for this schema.
StructuredType stBasic = XMPBasicSchema.class.getAnnotation(StructuredType.class);
StructuredType stPdfaIdent = PDFAIdentificationSchema.class.getAnnotation(StructuredType.class);
if (!id.getPrefix().equals(stPdfaIdent.preferedPrefix())) {
if (metadata.getSchema(stPdfaIdent.preferedPrefix(), stBasic.namespace()) == null) {
ve.add(unexpectedPrefixFoundError(id.getPrefix(), stPdfaIdent.preferedPrefix(), PDFAIdentificationSchema.class.getName()));
} else {
id = (PDFAIdentificationSchema) metadata.getSchema(stPdfaIdent.preferedPrefix(), stPdfaIdent.namespace());
}
}
checkConformanceLevel(ve, id.getConformance());
checkPartNumber(ve, id.getPart() == null ? -1 : id.getPart());
return ve;
}
use of org.apache.xmpbox.schema.PDFAIdentificationSchema in project pdfbox by apache.
the class PDFMergerExample method createXMPMetadata.
private PDMetadata createXMPMetadata(COSStream cosStream, String title, String creator, String subject) throws BadFieldValueException, TransformerException, IOException {
LOG.info("Setting XMP metadata (title, author, subject) for merged PDF");
XMPMetadata xmpMetadata = XMPMetadata.createXMPMetadata();
// PDF/A-1b properties
PDFAIdentificationSchema pdfaSchema = xmpMetadata.createAndAddPFAIdentificationSchema();
pdfaSchema.setPart(1);
pdfaSchema.setConformance("B");
// Dublin Core properties
DublinCoreSchema dublinCoreSchema = xmpMetadata.createAndAddDublinCoreSchema();
dublinCoreSchema.setTitle(title);
dublinCoreSchema.addCreator(creator);
dublinCoreSchema.setDescription(subject);
// XMP Basic properties
XMPBasicSchema basicSchema = xmpMetadata.createAndAddXMPBasicSchema();
Calendar creationDate = Calendar.getInstance();
basicSchema.setCreateDate(creationDate);
basicSchema.setModifyDate(creationDate);
basicSchema.setMetadataDate(creationDate);
basicSchema.setCreatorTool(creator);
// Create and return XMP data structure in XML format
try (ByteArrayOutputStream xmpOutputStream = new ByteArrayOutputStream();
OutputStream cosXMPStream = cosStream.createOutputStream()) {
new XmpSerializer().serialize(xmpMetadata, xmpOutputStream, true);
cosXMPStream.write(xmpOutputStream.toByteArray());
return new PDMetadata(cosStream);
}
}
use of org.apache.xmpbox.schema.PDFAIdentificationSchema in project mustangproject by ZUGFeRD.
the class MustangReaderWriterEdgeTest method testEdgeExportIsPDFA3.
/**
* This test is based on #testEdgeExport but test only that the PDFIdentificationSchema is present and identifies
* the pdf as PDF/A-3 conformant.
*/
public void testEdgeExportIsPDFA3() throws Exception {
final String SOURCE_PDF = "/MustangGnuaccountingBeispielRE-20170509_505PDFA3.pdf";
final String TARGET_PDF = "./target/testout-MustangGnuaccountingBeispielRE-20170509_505newEdge.pdf";
// the writing part
setupPdfUnderTest(SOURCE_PDF, TARGET_PDF);
// now check the contents
PDDocument doc = PDDocument.load(new File(TARGET_PDF));
PDMetadata meta = doc.getDocumentCatalog().getMetadata();
assertNotNull("The pdf must contain XMPMetadata", meta);
byte[] xmpBytes = meta.toByteArray();
assertNotNull("The xmp metadata stream must not be null", xmpBytes);
DomXmpParser xmpParser = new DomXmpParser();
XMPMetadata xmp = xmpParser.parse(xmpBytes);
PDFAIdentificationSchema pdfai = xmp.getPDFIdentificationSchema();
assertNotNull("The pdf must contain a PDFIdentificationSchema", pdfai);
assertTrue("The PDF/A conformance must be A, B or U", Arrays.asList("A", "B", "U", "a", "b", "u").contains(pdfai.getConformance()));
assertEquals("The PDF/A must be 3", (Integer) 3, pdfai.getPart());
}
Aggregations