Add Collection to Document

Help
2010-07-16
2013-05-28
  • Bård Lanöy

    Bård Lanöy - 2010-07-16

    Hi,

    I am new to JPod and have been trying to find some info on how to create a PdfContainer; i.e. a PDF document holding a set of embedded files. Adding the files work fine. However, to have for instance Adobe reader show the files as a list of objects, I believe I need to add a Collection to the Catalog (at root level) that points to the first file that will be displayed (in Adobe reader).

    PDDocument _document = PDDocument.createNew();
            
            COSDictionary _collection = COSDictionary.create();        
            _collection.setAttribute(COSName.constant("D"), "mainDude.pdf");
            _collection.setAttribute(COSName.constant("Type"), "Collection");
            _collection.setAttribute(COSName.constant("View"), "T");
            _document.getCatalog().cosSetField(COSName.constant("Collection"), _collection);
    

    While doing this seems to do the trick for Adobe reader - I stil cannot find the collection with the attributes when scrutinizing the raw PDF file.

    In my current implementation I use a COSDictionary and add an attribute with name="Type" and value="Collection". But this does not seem to be correct :-). Could someone please help me on how to solve this or direct me to a good documentation on how to solve this ?

    I have provided more code below to better demonstrate what I am trying to do:

    PDDocument _document = PDDocument.createNew();
            
            COSDictionary _collection = COSDictionary.create();        
            _collection.setAttribute(COSName.constant("D"), "mainDude.pdf");
            _collection.setAttribute(COSName.constant("Type"), "Collection");
            _collection.setAttribute(COSName.constant("View"), "T");
            _document.getCatalog().cosSetField(COSName.constant("Collection"), _collection);
            
            // Then create the first page.
            PDPage page = (PDPage) PDPage.META.createNew();
            // add page to the document.
            _document.addPageNode(page);        
            List<EmbeddedFile> _files = new ArrayList<EmbeddedFile>(4);
            _files.add(new EmbeddedFile("mainDude.pdf", "Document PDF", _presentation));
            _files.add(new EmbeddedFile("filenameshouldbehere.roa.edi", "Invoice data", message.getDoc()));
            _files.add(new EmbeddedFile("signedpdf.pdf.pk7", "Signature data", _validated.getDoc()));
            _files.add(new EmbeddedFile("protocol.txt", "Signature protocol", _validated.getSigDetailsDoc()));
            appendFiles(_document, _files);
            FileLocator locator = new FileLocator("C:\\pdfTest\\output.pdf");
        _document.save(locator, null);
    
     
  • Elfi Heck

    Elfi Heck - 2010-07-21

    I don't know what your "appendFiles" method does, but it should add the file to the document's "EmbeddedFiles" name dictionary entry. We don't have convenience methods for this, but with the existing methods you can create a structure like this:

    1 0 obj
    <</Type /Catalog
    /Names 3 0 R
    …
    >>
    endobj
    3 0 obj
    <</EmbeddedFiles 4 0 R
    …
    >>
    endobj
    4 0 obj
    <</Names 5 0 R
    …
    >>
    endobj
    5 0 obj
    <</Names [ (filename1)
               6 0 R
               (filename2)
               7 0 R
               ...
             ]
    >>
    endobj
    6 0 obj
    <</Type /Filespec
    /F (filename1)
    /EF <</F 7 0 R
        >>
    …
    >>
    endobj
    7 0 obj
    <</Type /EmbeddedFile
    …
    >>
    stream
    ...
    endstream
    endobj
    

    The "Collection" entry only controls how compatible viewers display the files. Section 3.10 in the PDF reference is about file specifications and embedding files.

     

Log in to post a comment.