use of org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent in project pdfbox by apache.
the class PDDocumentCatalog method getOutputIntents.
/**
* Get the list of OutputIntents defined in the document.
*
* @return The list of PDOutputIntent
*/
public List<PDOutputIntent> getOutputIntents() {
List<PDOutputIntent> retval = new ArrayList<>();
COSArray array = (COSArray) root.getDictionaryObject(COSName.OUTPUT_INTENTS);
if (array != null) {
for (COSBase cosBase : array) {
if (cosBase instanceof COSObject) {
cosBase = ((COSObject) cosBase).getObject();
}
PDOutputIntent oi = new PDOutputIntent((COSDictionary) cosBase);
retval.add(oi);
}
}
return retval;
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent 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.pdfbox.pdmodel.graphics.color.PDOutputIntent in project pdfbox by apache.
the class PDFMergerUtility method mergeOutputIntents.
// copy outputIntents to destination, but avoid duplicate OutputConditionIdentifier,
// except when it is missing or is named "Custom".
private void mergeOutputIntents(PDFCloneUtility cloner, PDDocumentCatalog srcCatalog, PDDocumentCatalog destCatalog) throws IOException {
List<PDOutputIntent> srcOutputIntents = srcCatalog.getOutputIntents();
List<PDOutputIntent> dstOutputIntents = destCatalog.getOutputIntents();
for (PDOutputIntent srcOI : srcOutputIntents) {
String srcOCI = srcOI.getOutputConditionIdentifier();
if (srcOCI != null && !"Custom".equals(srcOCI)) {
// is that identifier already there?
boolean skip = false;
for (PDOutputIntent dstOI : dstOutputIntents) {
if (dstOI.getOutputConditionIdentifier().equals(srcOCI)) {
skip = true;
break;
}
}
if (skip) {
continue;
}
}
destCatalog.addOutputIntent(new PDOutputIntent((COSDictionary) cloner.cloneForNewDocument(srcOI)));
dstOutputIntents.add(srcOI);
}
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent in project pdfbox by apache.
the class PDDocumentCatalog method setOutputIntents.
/**
* Replace the list of OutputIntents of the document.
*
* @param outputIntents the list of OutputIntents, if the list is empty all OutputIntents are
* removed.
*/
public void setOutputIntents(List<PDOutputIntent> outputIntents) {
COSArray array = new COSArray();
for (PDOutputIntent intent : outputIntents) {
array.add(intent.getCOSObject());
}
root.setItem(COSName.OUTPUT_INTENTS, array);
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent in project pdfbox by apache.
the class TestPDDocumentCatalog method handleOutputIntents.
/**
* Test OutputIntents functionality.
*
* Test case for
* <a https://issues.apache.org/jira/browse/PDFBOX-2687">PDFBOX-2687</a>
* ClassCastException when trying to get OutputIntents or add to it.
*
* @throws IOException in case the document can not be parsed.
*/
@Test
public void handleOutputIntents() throws IOException {
PDDocument doc = null;
InputStream colorProfile = null;
try {
doc = PDDocument.load(TestPDDocumentCatalog.class.getResourceAsStream("test.unc.pdf"));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
// retrieve OutputIntents
List<PDOutputIntent> outputIntents = catalog.getOutputIntents();
assertTrue(outputIntents.isEmpty());
// add an OutputIntent
colorProfile = TestPDDocumentCatalog.class.getResourceAsStream("sRGB.icc");
// create output intent
PDOutputIntent oi = new PDOutputIntent(doc, colorProfile);
oi.setInfo("sRGB IEC61966-2.1");
oi.setOutputCondition("sRGB IEC61966-2.1");
oi.setOutputConditionIdentifier("sRGB IEC61966-2.1");
oi.setRegistryName("http://www.color.org");
doc.getDocumentCatalog().addOutputIntent(oi);
// retrieve OutputIntents
outputIntents = catalog.getOutputIntents();
assertEquals(1, outputIntents.size());
// set OutputIntents
catalog.setOutputIntents(outputIntents);
outputIntents = catalog.getOutputIntents();
assertEquals(1, outputIntents.size());
} finally {
if (doc != null) {
doc.close();
}
if (colorProfile != null) {
colorProfile.close();
}
}
}
Aggregations