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/21 15:41:00 UTC

[jira] [Commented] (PDFBOX-5173) After Create portable collection PDF package and opening it with Acrobat Reader DC, some PDFs in the package will be blank pages after clicking open

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

Tilman Hausherr commented on PDFBOX-5173:
-----------------------------------------

That's because your name tree isn't properly sorted, something goes wrong there. The easiest is to have the HashMap do it for you, here's the modified code (only beginning and end are modified)
{code}
        PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
        
        Map<String, PDComplexFileSpecification> map = new HashMap<>();

        // 创建表格属性
        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("序号")); // "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.N); // type: number类型 field
        fieldDict1.setString(COSName.N, "序号"); // 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, "文件类型");
        fieldDict2.setInt(COSName.O, 2);
        COSDictionary fieldDict3 = new COSDictionary();
        fieldDict3.setItem(COSName.TYPE, COSName.COLLECTION_FIELD);
        fieldDict3.setItem(COSName.SUBTYPE, COSName.S); // type: String field
        fieldDict3.setString(COSName.N, "文件名称");
        fieldDict3.setInt(COSName.O, 3);
        schemaDict.setItem("序号", fieldDict1); // field name (this is a key)
        schemaDict.setItem("文件类型", fieldDict2);
        schemaDict.setItem("文件名称", fieldDict3);
        doc.getDocumentCatalog().getCOSObject().setItem(COSName.COLLECTION, collectionDic);
        doc.getDocumentCatalog().setVersion("1.7");

        List<PDEmbeddedFilesNameTreeNode> kids = new ArrayList<PDEmbeddedFilesNameTreeNode>();
        int count = 0;
        for (String childFile : childFiles) {
            count++;
            PDComplexFileSpecification fs = new PDComplexFileSpecification();
            File attachment = new File(childFile);
            PDEmbeddedFile ef = new PDEmbeddedFile(doc, new FileInputStream(attachment));

            COSDictionary ciDict1 = new COSDictionary();
            ciDict1.setItem(COSName.TYPE, COSName.COLLECTION_ITEM);
            // use the field names from earlier
            ciDict1.setInt("序号", count);
            ciDict1.setString("文件类型", "filetype");
            ciDict1.setString("文件名称",attachment.getName() );
            fs.getCOSObject().setItem(COSName.CI, ciDict1);

            fs.setFileDescription(attachment.getName().replace(".pdf","")); //文件描述
            fs.setFileUnicode(attachment.getName()); //文件名

            ef.setSubtype("application/pdf");  //参考mime文件类型
            ef.setSize((int) attachment.length());
            Calendar c=Calendar.getInstance();
            c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            ef.setCreationDate(c);
            ef.setModDate(c);
            fs.setEmbeddedFile(ef);
            map.put(attachment.getName(), fs);
//            PDEmbeddedFilesNameTreeNode treeNode = new PDEmbeddedFilesNameTreeNode();
//            treeNode.setNames(Collections.singletonMap(attachment.getName(), fs));
//            kids.add(treeNode);
        }
        
        PDEmbeddedFilesNameTreeNode treeNode = new PDEmbeddedFilesNameTreeNode();
        treeNode.setNames(map);
        kids.add(treeNode);

        efTree.setKids(kids);
{code}

> After Create portable collection PDF package and opening it with Acrobat Reader DC, some PDFs in the package will be blank pages after clicking open
> ----------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: PDFBOX-5173
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5173
>             Project: PDFBox
>          Issue Type: Improvement
>          Components: Documentation
>         Environment: Java 1.8  pdfbox 2.0.24-snapshot
>            Reporter: tanqichao
>            Priority: Major
>         Attachments: SortAttachments.java, create_reslut.pdf, image-2021-04-21-17-44-21-334.png, image-2021-04-21-17-44-46-925.png, 卷内目录.pdf, 备考表.pdf, 封面.pdf, 深高顾api.pdf
>
>
>  
> !image-2021-04-21-17-44-46-925.png!
>  
> !image-2021-04-21-17-44-21-334.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