Is there an API to determine if a PDF has file attachments?

I was trying to use EmbeddedFiles.Count (as below) to determine if a PDF file has attachments, but
the APi returns zero for a PDF with attachments.

Is there an (efficient) API to query if a PDF has file attachments?
The only thing I have found is to try and extract them as described at:
https://docs.aspose.com/pages/viewpage.action?pageId=7119730
but that seems inefficient

Sample code

// Create Blank Page Pdf
string pdfFileName = “CreateFileAnnotation.pdf”;
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Save(pdfFileName);

        // Modify the PDF to add a file attachment
        PdfContentEditor editor = new PdfContentEditor();
        editor.BindPdf(pdfFileName);
        editor.CreateFileAttachment(new System.Drawing.Rectangle(50, 50, 10, 10), "here", attachedPdf, 1, "Paperclip", 0.005);
        editor.Save(pdfFileName+ ".out.pdf");

        // Inspect PDF to determine if it has file attachments
        pdfDocument = new Document(pdfFileName + ".out.pdf");
        int embeddedFilesCount = pdfDocument.EmbeddedFiles.Count;
        Console.WriteLine("The resulting PDF has EmbeddedFiles Count: " + embeddedFilesCount);

@PeteLee

Thanks for contacting support.

Would you please share your sample PDF document with us so that we can test the scenario in our environment and address it accordingly.

Asad,

The program above generates a file with attachment, but I also enclose it here:

Note it is just a blank page with an annotation linking to another blank page.

When processing the file, I would like to know if it has attachments (which in this case is true)

@PeteLee

Thanks for sharing sample PDF document.

We have tested the scenario using following code snippet with Aspose.PDF for .NET 18.11 and were unable to notice any issue. The code attached the file within the document and returned correct count of embedded files. For your kind reference, an output document is also attached.

Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Save(dataDir + "FileWithAttachment.pdf");
pdfDocument = new Document(dataDir + "FileWithAttachment.pdf");
FileSpecification fileSpecification = new FileSpecification(dataDir + "ConcatenatedFile.pdf", "Sample Attachment");
pdfDocument.EmbeddedFiles.Add(fileSpecification);
dataDir = dataDir + "AddAttachment_out.pdf";
pdfDocument.Save(dataDir);
pdfDocument = new Document(dataDir);
int embeddedFilesCount = pdfDocument.EmbeddedFiles.Count;
Console.WriteLine("The resulting PDF has EmbeddedFiles Count: " + embeddedFilesCount); 

AddAttachment_out.pdf (1.8 KB)

Would you please use above code snippet with latest version of the API and in case you still face any issue, please feel free to let us know.

Thanks. but the EmbeddedFiles.Count is ZERO for the Pdf I sent you (Attached above).

This is the code:

        Document pdfDocument = new Document(@"FileWithAttachment.pdf");
        int embeddedFilesCount = pdfDocument.EmbeddedFiles.Count;
        Console.WriteLine("The resulting PDF has EmbeddedFiles Count: " + embeddedFilesCount);

This is the output:

The resulting PDF has EmbeddedFiles Count: 0

The file I sent is generated differently from the one you created … but still if you open it with acrobat
shows to have an attachment

We process arbitrary PDF and need to determine if they have attachments. The file I sent you was just
created to reproduce the problem, but I have other files received from customers that exhibit the same
issue. I cannot send them as they contain confidential information.

@PeteLee

Thanks for writing back.

We have tested the scenario and were able to reproduce the issue you have mentioned. Therefore, we have logged it as PDFNET-45724 in our issue tracking system for the sake of further investigation. We will let you know in case we have further updates regarding its correction.

Furthermore, please note that your PDF file was created using Facades API which will be going to obsolete sooner or later and the DOM approach we shared in our earlier message is recommended one. With DOM approach, file is being attached within PDF and attachment count retrieval was also fine. We request you to please use DOM approach in order to attach files and in case issue still persists, please let us know.

I think embedded files and annotations file attachments are two ways a PDF can end up including a ‘child’ PDF.
and my issue is just with annotations file attachments

@PeteLee

Thanks for writing back.

In order to add attachment annotation using DOM model, we would recommend you following sample code snippet. Please use it in your environment and in case it does not resolve your original issue, please let us know.

Document pdfDocument = new Document();
Aspose.Pdf.Page pdfPage = pdfDocument.Pages.Add();
FileSpecification fileSpecification = new FileSpecification(dataDir + "Sample PDF.pdf", "This is attachment.");
pdfDocument.EmbeddedFiles.Add(fileSpecification);
Aspose.Pdf.Annotations.FileAttachmentAnnotation fileAttachment = new Aspose.Pdf.Annotations.FileAttachmentAnnotation(pdfPage, new Aspose.Pdf.Rectangle(0, 0, 16, 16), pdfDocument.EmbeddedFiles[1]);
fileAttachment.Rect = new Aspose.Pdf.Rectangle(100, 100, 120, 120);
fileAttachment.Icon = Aspose.Pdf.Annotations.FileIcon.Paperclip;
fileAttachment.Title = "Attached file";
fileAttachment.Contents = "Attached file";
pdfDocument.Pages[1].Annotations.Add(fileAttachment);
pdfDocument.Save(dataDir + "LinktoFileWithAttachment.pdf");

Asad,

My question is:
Is there an API to determine if a PDF has file attachments?

Given an arbitrary file received by a 3rd party … (not created by me) … Can I use Aspose Pdf to
determine if the file has an attachment or not?

For example, I sent you a sample file that has an attachment:

I would like an API that tells me that file has an attachment.

The embeddedfiles.count API return zero on the sample file.
I have other files that exhibit this same problem.

I am not trying to create files with attachments.

@PeteLee

Thanks for further elaboration.

Initially, we had an understanding that you were creating PDF with attachments using Aspose.PDF which was why we shared those code snippets with you.

Nevertheless, we have also logged an issue in our issue tracking system related to your original requirements and will definitely share updates with you as soon as we have some regarding its investigation. Please spare us little time.

We are sorry for the inconvenience.

@PeteLee

When opening files in Adobe Acrobat, the following Attachments are shown (see picture “Attachments showed by Acrobat.png”). Attachments showed by Acrobat.png (10.6 KB)

By viewing the file, it is determined that these are FileAttachmentAnnotation objects.
You can use the following code to verify this.

Document pdfDocument = new Document(@"FileWithAttachment.pdf");

foreach (Annotation annotation in pdfDocument.Pages[1].Annotations)
    if (annotation is FileAttachmentAnnotation)
    {
        FileSpecification file = (annotation as FileAttachmentAnnotation).File;
        if (file != null && file.Name != null)
        {
            Debug.WriteLine($"Name: {file.Name} on page 1");
        }
    }

which will show the presence of a FileAnnotation with the file name “BlankPage.pdf”.