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);
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);
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.
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
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");
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.
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”.