List of usage examples for org.apache.pdfbox.pdmodel PDDocumentCatalog setNames
public void setNames(PDDocumentNameDictionary names)
From source file:io.konik.carriage.pdfbox.PDFBoxInvoiceAppender.java
License:Open Source License
private static void setNamesDictionary(PDDocument doc, PDEmbeddedFilesNameTreeNode fileNameTreeNode) { PDDocumentCatalog documentCatalog = doc.getDocumentCatalog(); PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(documentCatalog); namesDictionary.setEmbeddedFiles(fileNameTreeNode); documentCatalog.setNames(namesDictionary); }
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 ww w . j av a 2 s . c o m 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); }