In itext7,how to change attach files display order

2019-08-29 09:30发布

问题:

I want to change my attach file order in created pdf,attachment are displayed default by name, how to change them displayed by add time?

this is my implement method:

@Override
public boolean attachFile(String src, String dest, List<SysItemfile> attachmentpaths) {
    try {
        PdfName name = new PdfName(src);
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
        List<String> descs = new ArrayList<String>();
        int i = 0;
        int j = 1;
        for (SysItemfile attachmentpath : attachmentpaths) {
            String filename = attachmentpath.getFilename();
            //test for the file name
            System.out.println("filename:"+filename);

            if (descs.contains(attachmentpath.getFilename())) {
                //get the file suffix 
                String suffix = filename.substring(filename.lastIndexOf(".") + 1);
                String realname = filename.substring(0,filename.lastIndexOf("."));
                filename = realname+i+"."+suffix;
                i++;
            } else {
                descs.add(attachmentpath.getFilename());
            }
            PdfFileSpec spec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc, attachmentpath.getFileurl(),
                    filename, filename, name, name);
            // the first parameter is discription
            pdfDoc.addFileAttachment(filename, spec);

        }
        pdfDoc.close();
    } catch (IOException e) {
        logger.error("attachFile unsuccess!");
        logger.error(e.getLocalizedMessage());
        return false;
    }
    return true;
}

After that , when i add attachment to my pdf,the cann't change the order of attachment display.
what should I do?

回答1:

As long as you only add attachments, the PDF standard does not allow for prescribing the sort order a PDF viewer uses when displaying the attachments.

If, on the other hand, you make the PDF a portable collection (aka a Portfolio), you can prescribe a schema (i.e. the fields in the detail list) and the sort order (by one or a combination of those fields).

You can quite easily make your PDF with attachments a portable collection with the name and modification date sorted by the latter like this:

try (   PdfReader reader = new PdfReader(...);
        PdfWriter writer = new PdfWriter(...);
        PdfDocument document = new PdfDocument(reader, writer)) {
    PdfCollection collection = new PdfCollection();
    document.getCatalog().setCollection(collection);
    PdfCollectionSchema schema = new PdfCollectionSchema();
    PdfCollectionField field = new PdfCollectionField("File Name", PdfCollectionField.FILENAME);
    field.setOrder(0);
    schema.addField("Name", field);
    field = new PdfCollectionField("Modification Date", PdfCollectionField.MODDATE);
    field.setOrder(1);
    schema.addField("Modified", field);
    collection.setSchema(schema);
    PdfCollectionSort sort = new PdfCollectionSort("Modified");
    collection.setSort(sort);
}

(SortAttachments test testAttachLikeGeologistedWithCollection)

You actually even can define a custom field using a type PdfCollectionField.TEXT, PdfCollectionField.DATE, or PdfCollectionField.NUMBER by which to sort. You can set the value of such a custom field on a PdfFileSpec via its setCollectionItem method.