You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (Jira)" <ji...@apache.org> on 2021/04/16 17:21:00 UTC

[jira] [Comment Edited] (PDFBOX-5164) Create portable collection PDF

    [ https://issues.apache.org/jira/browse/PDFBOX-5164?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17323937#comment-17323937 ] 

Tilman Hausherr edited comment on PDFBOX-5164 at 4/16/21, 5:20 PM:
-------------------------------------------------------------------

Here's some code... there are not yet any collection classes in PDFBox. Get the EmbeddedFiles.java example from the source code and then add this near the end. 

This is about a single file but you can add several ones. For that, create more "ciDict" dictionaries and of course more PDComplexFileSpecification objects.
{code}
COSDictionary collectionDic = new COSDictionary();
COSDictionary schemaDict = new COSDictionary();
schemaDict.setItem(COSName.TYPE, COSName.COLLECTION_SCHEMA);
COSDictionary sortDic = new COSDictionary();
sortDic.setItem(COSName.TYPE, COSName.COLLECTION_SORT);
sortDic.setString(COSName.A, "true"); // sort ascending
sortDic.setItem(COSName.S, COSName.getPDFName("fieldtwo")); // "it identifies a field described in the parent collection dictionary"
collectionDic.setItem(COSName.TYPE, COSName.COLLECTION);
collectionDic.setItem(COSName.SCHEMA, schemaDict);
collectionDic.setItem(COSName.SORT, sortDic);
collectionDic.setItem(COSName.VIEW, COSName.D); // Details mode
COSDictionary fieldDict1 = new COSDictionary();
fieldDict1.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
fieldDict1.setItem(COSName.SUBTYPE, COSName.S); // type: text field
fieldDict1.setString(COSName.N, "field header one"); // header text
fieldDict1.setInt(COSName.O, 1); // order on the screen
COSDictionary fieldDict2 = new COSDictionary();
fieldDict2.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
fieldDict2.setItem(COSName.SUBTYPE, COSName.S); // type: text field
fieldDict2.setString(COSName.N, "field header two");
fieldDict2.setInt(COSName.O, 2);
COSDictionary fieldDict3 = new COSDictionary();
fieldDict3.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
fieldDict3.setItem(COSName.SUBTYPE, COSName.N); // type: number field
fieldDict3.setString(COSName.N, "field header three");
fieldDict3.setInt(COSName.O, 3);
schemaDict.setItem("fieldone", fieldDict1); // field name (this is a key)
schemaDict.setItem("fieldtwo", fieldDict2);
schemaDict.setItem("fieldthree", fieldDict3);
doc.getDocumentCatalog().getCOSObject().setItem(COSName.COLLECTION, collectionDic);
doc.getDocumentCatalog().setVersion("1.7");


COSDictionary ciDict1 = new COSDictionary();
ciDict1.setItem(COSName.TYPE, COSName.COLLECTION_ITEM);
// use the field names from earlier
ciDict1.setString("fieldone", "Very interesting file");
ciDict1.setString("fieldtwo", fs.getFile());
ciDict1.setInt("fieldthree", 333);
fs.getCOSObject().setItem(COSName.CI, ciDict1);
{code}

Use the latest snapshot in 
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/2.0.24-SNAPSHOT/
to get the new constants.

Result file:  [^collection.pdf] 


was (Author: tilman):
Here's some code... there are not yet any collection classes in PDFBox. Get the EmbeddedFiles.java example from the source code and then add this near the end. 

This is about a single file but you can add several ones. For that, create more "ciDict" dictionaries and of course more PDComplexFileSpecification objects.
{code}
COSDictionary collectionDic = new COSDictionary();
COSDictionary schemaDict = new COSDictionary();
schemaDict.setItem(COSName.TYPE, COSName.COLLECTION_SCHEMA);
COSDictionary sortDic = new COSDictionary();
sortDic.setItem(COSName.TYPE, COSName.COLLECTION_SORT);
sortDic.setString(COSName.A, "true"); // sort ascending
sortDic.setItem(COSName.S, COSName.getPDFName("fieldtwo")); // "it identifies a field described in the parent collection dictionary"
collectionDic.setItem(COSName.TYPE, COSName.COLLECTION);
collectionDic.setItem(COSName.SCHEMA, schemaDict);
collectionDic.setItem(COSName.SORT, sortDic);
collectionDic.setItem(COSName.VIEW, COSName.D); // Details mode
COSDictionary fieldDict1 = new COSDictionary();
fieldDict1.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
fieldDict1.setItem(COSName.SUBTYPE, COSName.S); // type: text field
fieldDict1.setString(COSName.N, "field header one"); // header text
fieldDict1.setInt(COSName.O, 1); // order on the screen
COSDictionary fieldDict2 = new COSDictionary();
fieldDict2.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
fieldDict2.setItem(COSName.SUBTYPE, COSName.S); // type: text field
fieldDict2.setString(COSName.N, "field header two");
fieldDict2.setInt(COSName.O, 2);
COSDictionary fieldDict3 = new COSDictionary();
fieldDict3.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
fieldDict3.setItem(COSName.SUBTYPE, COSName.N); // type: number field
fieldDict3.setString(COSName.N, "field header three");
fieldDict3.setInt(COSName.O, 3);
schemaDict.setItem("fieldone", fieldDict1); // field name (this is a key)
schemaDict.setItem("fieldtwo", fieldDict2);
schemaDict.setItem("fieldthree", fieldDict3);
doc.getDocumentCatalog().getCOSObject().setItem(COSName.COLLECTION, collectionDic);
doc.getDocumentCatalog().setVersion("1.7");


COSDictionary ciDict1 = new COSDictionary();
ciDict1.setItem(COSName.TYPE, COSName.COLLECTION_ITEM);
// use the field names from earlier
ciDict1.setString("fieldone", "Very interesting file");
ciDict1.setString("fieldtwo", fs.getFile());
ciDict1.setInt("fieldthree", 333);
fs.getCOSObject().setItem(COSName.CI, ciDict1);
{code}

Use the latest snapshot in 
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/2.0.24-SNAPSHOT/
(it's building right now) to get the new constants.

Result file:  [^collection.pdf] 

> Create portable collection PDF
> ------------------------------
>
>                 Key: PDFBOX-5164
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5164
>             Project: PDFBox
>          Issue Type: Improvement
>          Components: Documentation
>    Affects Versions: 2.0.18
>         Environment: java
>            Reporter: zhouxiaolong
>            Priority: Major
>             Fix For: 2.0.24, 4.0.0
>
>         Attachments: MakePackage.java, collection.pdf, image-2021-04-15-16-02-42-451.png, screenshot-1.png, viewfiles - 副本.pdf
>
>
> !image-2021-04-15-16-02-42-451.png!



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org