Search in sources :

Example 1 with PDOutputIntent

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;
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSObject(org.apache.pdfbox.cos.COSObject) COSArrayList(org.apache.pdfbox.pdmodel.common.COSArrayList) ArrayList(java.util.ArrayList) COSBase(org.apache.pdfbox.cos.COSBase) PDOutputIntent(org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent)

Example 2 with PDOutputIntent

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);
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException) PDPage(org.apache.pdfbox.pdmodel.PDPage) XmpSerializer(org.apache.xmpbox.xml.XmpSerializer) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDOutputIntent(org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent) PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) XMPMetadata(org.apache.xmpbox.XMPMetadata) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDPageContentStream(org.apache.pdfbox.pdmodel.PDPageContentStream) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) File(java.io.File)

Example 3 with PDOutputIntent

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);
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDOutputIntent(org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent)

Example 4 with PDOutputIntent

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);
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) PDOutputIntent(org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent)

Example 5 with PDOutputIntent

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();
        }
    }
}
Also used : InputStream(java.io.InputStream) PDOutputIntent(org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent) Test(org.junit.Test)

Aggregations

PDOutputIntent (org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent)5 InputStream (java.io.InputStream)2 COSArray (org.apache.pdfbox.cos.COSArray)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSObject (org.apache.pdfbox.cos.COSObject)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)1 COSArrayList (org.apache.pdfbox.pdmodel.common.COSArrayList)1 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 XMPMetadata (org.apache.xmpbox.XMPMetadata)1 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)1 PDFAIdentificationSchema (org.apache.xmpbox.schema.PDFAIdentificationSchema)1 BadFieldValueException (org.apache.xmpbox.type.BadFieldValueException)1 XmpSerializer (org.apache.xmpbox.xml.XmpSerializer)1