Example usage for org.apache.pdfbox.cos COSDictionary setString

List of usage examples for org.apache.pdfbox.cos COSDictionary setString

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSDictionary setString.

Prototype

public void setString(COSName key, String value) 

Source Link

Document

This is a convenience method that will convert the value to a COSString object.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createAcroFormDictionary(PDAcroForm acroForm, PDSignatureField signatureField) throws IOException {
    @SuppressWarnings("unchecked")
    List<PDField> acroFormFields = acroForm.getFields();
    COSDictionary acroFormDict = acroForm.getDictionary();
    acroFormDict.setDirect(true);//ww w. j  av a  2  s .  c om
    acroFormDict.setInt(COSName.SIG_FLAGS, 3);
    acroFormFields.add(signatureField);
    acroFormDict.setString(COSName.DA, "/sylfaen 0 Tf 0 g");
    getStructure().setAcroFormFields(acroFormFields);
    getStructure().setAcroFormDictionary(acroFormDict);
    logger.debug("AcroForm dictionary has been created");
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createAcroFormDictionary(PDAcroForm acroForm, PDSignatureField signatureField) throws IOException {
    @SuppressWarnings("unchecked")
    List<PDField> acroFormFields = acroForm.getFields();
    COSDictionary acroFormDict = acroForm.getCOSObject();
    acroFormDict.setDirect(true);//from w  ww. ja v  a 2s  . co m
    acroFormDict.setInt(COSName.SIG_FLAGS, 3);
    acroFormFields.add(signatureField);
    acroFormDict.setString(COSName.DA, "/sylfaen 0 Tf 0 g");
    getStructure().setAcroFormFields(acroFormFields);
    getStructure().setAcroFormDictionary(acroFormDict);
    logger.debug("AcroForm dictionary has been created");
}

From source file:ddf.catalog.transformer.input.pdf.GeoPdfDocumentGenerator.java

License:Open Source License

private static COSDictionary generateProjectionDictionary(String projectionType) {
    COSDictionary projectionDictionary = new COSDictionary();
    projectionDictionary.setString(GeoPdfParserImpl.PROJECTION_TYPE, projectionType);
    return projectionDictionary;
}

From source file:io.konik.carriage.pdfbox.PDFBoxInvoiceAppender.java

License:Open Source License

private static void attachZugferdFile(PDDocument doc, InputStream zugferdFile) throws IOException {
    PDEmbeddedFilesNameTreeNode fileNameTreeNode = new PDEmbeddedFilesNameTreeNode();

    PDEmbeddedFile embeddedFile = createEmbeddedFile(doc, zugferdFile);
    PDComplexFileSpecification fileSpecification = createFileSpecification(embeddedFile);

    COSDictionary dict = fileSpecification.getCOSDictionary();
    dict.setName("AFRelationship", "Alternative");
    dict.setString("UF", ZF_FILE_NAME);

    fileNameTreeNode.setNames(singletonMap(ZF_FILE_NAME, fileSpecification));

    setNamesDictionary(doc, fileNameTreeNode);

    COSArray cosArray = new COSArray();
    cosArray.add(fileSpecification);/*from   ww w  .  j  ava2  s  .c o  m*/
    doc.getDocumentCatalog().getCOSDictionary().setItem("AF", cosArray);
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporter.java

License:Open Source License

/**
 * Embeds an external file (generic - any type allowed) in the PDF.
 *
 * @param doc          PDDocument to attach the file to.
 * @param filename     name of the file that will become attachment name in the PDF
 * @param relationship how the file relates to the content, e.g. "Alternative"
 * @param description  Human-readable description of the file content
 * @param subType      type of the data e.g. could be "text/xml" - mime like
 * @param data         the binary data of the file/attachment
 * @throws java.io.IOException//from w w w  .  j ava  2 s. c o  m
 */
public void PDFAttachGenericFile(PDDocument doc, String filename, String relationship, String description,
        String subType, byte[] data) throws IOException {
    fileAttached = true;

    PDComplexFileSpecification fs = new PDComplexFileSpecification();
    fs.setFile(filename);

    COSDictionary dict = fs.getCOSObject();
    dict.setName("AFRelationship", relationship);
    dict.setString("UF", filename);
    dict.setString("Desc", description);

    ByteArrayInputStream fakeFile = new ByteArrayInputStream(data);
    PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile);
    ef.setSubtype(subType);
    ef.setSize(data.length);
    ef.setCreationDate(new GregorianCalendar());

    ef.setModDate(GregorianCalendar.getInstance());

    fs.setEmbeddedFile(ef);

    // In addition make sure the embedded file is set under /UF
    dict = fs.getCOSObject();
    COSDictionary efDict = (COSDictionary) dict.getDictionaryObject(COSName.EF);
    COSBase lowerLevelFile = efDict.getItem(COSName.F);
    efDict.setItem(COSName.UF, lowerLevelFile);

    // now add the entry to the embedded file tree and set in the document.
    PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
    PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
    if (efTree == null) {
        efTree = new PDEmbeddedFilesNameTreeNode();
    }

    Map<String, PDComplexFileSpecification> namesMap = new HashMap<>();

    Map<String, PDComplexFileSpecification> oldNamesMap = efTree.getNames();
    if (oldNamesMap != null) {
        for (String key : oldNamesMap.keySet()) {
            namesMap.put(key, oldNamesMap.get(key));
        }
    }
    namesMap.put(filename, fs);
    efTree.setNames(namesMap);

    names.setEmbeddedFiles(efTree);
    doc.getDocumentCatalog().setNames(names);

    // AF entry (Array) in catalog with the FileSpec
    COSArray cosArray = (COSArray) doc.getDocumentCatalog().getCOSObject().getItem("AF");
    if (cosArray == null) {
        cosArray = new COSArray();
    }
    cosArray.add(fs);
    COSDictionary dict2 = doc.getDocumentCatalog().getCOSObject();
    COSArray array = new COSArray();
    array.add(fs.getCOSObject()); // see below
    dict2.setItem("AF", array);
    doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);
}

From source file:pdfbox.PDFA3FileAttachment.java

License:Apache License

private void attachSampleFile(PDDocument doc) throws IOException {
    PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();

    // first create the file specification, which holds the embedded file

    PDComplexFileSpecification fs = new PDComplexFileSpecification();
    fs.setFile("Test.txt");
    COSDictionary dict = fs.getCOSDictionary();
    // Relation "Source" for linking with eg. catalog
    dict.setName("AFRelationship", "Alternative");
    // dict.setName("AFRelationship", "Source");

    dict.setString("UF", "Test.txt");
    // fs.put(new PdfName("AFRelationship"), new PdfName("Source"));

    String payload = "This is a test";

    InputStream is = new ByteArrayInputStream(payload.getBytes());

    PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);
    // set some of the attributes of the embedded file

    ef.setSubtype("text/plain");
    // ef.setFile(fs);
    // ef.getStream().setItem(COSName.UF, fs);

    ef.setModDate(GregorianCalendar.getInstance());

    // PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer,
    // src.getAbsolutePath(), src.getName(), null, false, "image/jpeg",
    // fileParameter);

    // fs.put(new PdfName("AFRelationship"), new PdfName("Source"));

    ef.setSize(payload.length());/*from w  w w  . jav a 2  s . c  om*/
    ef.setCreationDate(new GregorianCalendar());
    fs.setEmbeddedFile(ef);

    // now add the entry to the embedded file tree and set in the document.
    efTree.setNames(Collections.singletonMap("My first attachment", fs));

    /**
     * Validating file "RE-20131206_22.pdf" for conformance level pdfa-3a The
     * key UF is required but missing. The key AFRelationship is required but
     * missing. File specification 'Test.txt' not associated with an object.
     */
    // attachments are stored as part of the "names" dictionary in the document
    // catalog
    PDDocumentCatalog catalog = doc.getDocumentCatalog();

    PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
    names.setEmbeddedFiles(efTree);
    catalog.setNames(names);

    // // AF entry (Array) in catalog with the FileSpec
    // PDAcroForm pdAcroForm = new PDAcroForm(doc);
    // COSArray cosArray = new COSArray();
    // cosArray.add(fs);
    // catalog.setItem("AF", cosArray);

}