We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF attachments have empty fields "Modified" and "Size"

Hi
I’m adding attachments to PDF using the following code:

Public Shared Sub AddAttachmentToDocument(ByVal document As IO.Stream,
                                          ByVal attachment As IO.Stream,
                                          ByVal attachmentName As String,
                                          ByVal attachmentDescription As String,
                                          ByRef output As IO.Stream)
    If document Is Nothing Then Return

    ' register Aspose license
    Dim PdfLicense As New Aspose.Pdf.License
    PdfLicense.SetLicense("Aspose.Total.lic")

    Dim PdfDocument As New Aspose.Pdf.Document(document)

    Dim PdfAttachment As New Aspose.Pdf.FileSpecification(attachment, attachmentName)
    PdfAttachment.Description = attachmentDescription
    PdfDocument.EmbeddedFiles.Add(PdfAttachment)

    'save output document
    PdfDocument.Save(output)
End Sub

Attachments are added with given name and description but “Modified” and “Size” are not filled and there are no such properties in FileSpecification.

@tak

Thanks for contacting support.

You may please use Params property (FileParams Class) of FileSpecification as follows, in order to set Modified Date field of an attachment. FileParams also provide a property of Size, which can be used to determine the file size of an attachment (you cannot set this because its value is specified by the file size of the attachment).

[C#]

FileSpecification fs = new FileSpecification("attachment.txt", "This is attachment.");
fs.Params = new FileParams(fs);
fs.Params.ModDate = DateTime.Now; // Set modified date

In case you still face any issue while setting modified date of the attachment, please share your sample PDF document along with the attachment you want to add inside it. We will test the scenario in our environment and address it accordingly.

Unfortunately, suggested code is not able to fill empty property “size” (in FileParams it’s readOnly) and trows an exception “Value cannot be null. Parameter name: trailerable”
DEMO project that shows the problem:
https://drive.google.com/file/d/1CysjT-cSZ_ePFYyWgnsW0c7rclc_enOE/view?usp=sharing

@tak

Thanks for getting back to us.

We have tested the scenario by running shared project and were able to notice the exception while specifying the FileParams. We have logged an investigation ticket as PDFNET-44598 in our issue tracking system for further investigation. We will look into the details of the scenario and keep you posted in case of any further progress.

Furthermore, we have tried to add File Attachment using FileAttachmentAnnotation and in this case the FileParams were added successfully. Please change your AddAttachmentToDocument() method definition as following and run the project:

Public Sub AddAttachmentToDocument(ByVal document As IO.Stream,
                                       ByVal attachment As IO.Stream,
                                       ByVal attachmentName As String,
                                       ByVal attachmentDescription As String,
                                       ByRef output As IO.Stream)
If document Is Nothing Then Return

 Dim PdfDocument As New Aspose.Pdf.Document(document)
 Dim PdfAttachment As New Aspose.Pdf.FileSpecification(attachment, attachmentName)
 Dim file As New Aspose.Pdf.Annotations.FileAttachmentAnnotation(PdfDocument.Pages(1), New Aspose.Pdf.Rectangle(40, 800, 60, 820), PdfAttachment)
 PdfAttachment.Params = New Aspose.Pdf.FileParams(PdfAttachment)
 PdfAttachment.Params.CreationDate = DateTime.Now
 PdfAttachment.Params.ModDate = DateTime.Now

 file.Color = Aspose.Pdf.Color.Transparent
 file.Contents = "This is Attachment."
 file.Icon = Aspose.Pdf.Annotations.FileIcon.Paperclip
 PdfDocument.Pages(1).Annotations.Add(file)
 file.Name = "AttachedFile"

 'save output document
 PdfDocument.Save(output)
End Sub

As shared earlier, the Property Size is Read-Only because it is specified by the file size of original file added as attachment and you cannot set this with some custom value.

Hi

I checked - your version of addAttachmentToDocument works without exceptions but it does not solve problem with empty Size of attachment and it causes one new problem - now on the first page of main document there is quite big clip icon near left top corner.
Is it possible to add attachment which will have property “Size” filled?
Is it possible to add attachments with filled dates but without clip icon?

@tak

You may set AnnotationFlags before adding annotation to the page, in order to hide paper clip icon.

''''' 
file.Icon = Aspose.Pdf.Annotations.FileIcon.Paperclip
file.Flags = Aspose.Pdf.Annotations.AnnotationFlags.Hidden & Aspose.Pdf.Annotations.AnnotationFlags.NoView
PdfDocument.Pages(1).Annotations.Add(file)
'''''''''' 

We have observed that file size was not displaying correctly in Adobe Reader DC 18.011 version, whereas it was displayed when we opened file with Adobe Reader 9 Pro. FileSize.png (64.3 KB). Therefore, we have logged an issue as PDFNET-44604 in our issue tracking system. We will further investigate the reasons behind this issue and keep you posted with the status of its resolution. Please spare us little time.

We are sorry for the inconvenience.