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

Free Support Forum - aspose.com

Create a Pdf attachment using a filestream

Hello,

I have create an app that uses Aspose.Pdf to add documents to an output pdf document as attachments and replacing it using FileAttachmentAnnotation. The resulting pdf file has the icon but it’s stretched and garbled (showing background text). How do I replace the text I searched for resulting in TextFragments. I have included the code below that produces the FileAttachment Annotation.

Thank You,

Michael W. Kelley

CODE:

public void AddLinkAsAttachmentLink(TextFragment textFragmentToContainLink) {
// Add attachment to document’s attachment collection
FileSpecification attachmentFileSpecification = new FileSpecification(this.OriginalDocumentStream, $"{this.Name}.pdf", this.Name);
this.SelectAttachmentDocument.EmbeddedFiles.Add(this.Name, attachmentFileSpecification);

        // Create attachment annotation
        FileAttachmentAnnotation fileAttachmentAnnotation = new FileAttachmentAnnotation(
            textFragmentToContainLink.Page,
            textFragmentToContainLink.Rectangle,
            this.SelectAttachmentDocument.EmbeddedFiles[this.Name]);

        // Add attachment to the page referenced by the TextFragment
        textFragmentToContainLink.Page.Annotations.Add(fileAttachmentAnnotation);
        // Set title & icon for attachment
        fileAttachmentAnnotation.Title = fileAttachmentAnnotation.RichText = fileAttachmentAnnotation.Contents = this.Name;
        fileAttachmentAnnotation.Icon = FileIcon.Paperclip;

        textFragmentToContainLink.Text = this.Name;

}

@mkelley

Thank you for contacting support.

Would you please share source and generated files along with an executable code snippet because there are some undefined objects involved currently. So that we may try to reproduce and investigate it in our environment. Before sharing requested data, please ensure using Aspose.PDF for .NET 18.7 in your environment.

// Open document
Document document = new Document();
// add page
Aspose.Pdf.Page page = document.Pages.Add();
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(@“C:\Pdf\test155\test.txt”, “Enter file name”);

// Add attachment to document’s attachment collection
document.EmbeddedFiles.Add(fileSpecification);

// Create attachment annoation
FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(page, new Aspose.Pdf.Rectangle(0, 0, 16, 16), document.EmbeddedFiles[1]);
// Add attachment to first page of PDF
page.Annotations.Add(fileAttachment);
// Set title for attachment
fileAttachment.Title = “details”;

// Save the PDF file
document.Save(@“C:\Pdf\test155\CreateDocumentLink.pdf”);

@mkelley

The code snippet adds a file as attachment and adds a file annotation on a PDF page and the code snippet is working as expected. The icon is appearing with dimension of 16*16 as specified in the code and may appear stretched. You may modify the dimensions as per your requirements. Moreover, you can search and replace the text as explained over Replace Text in a PDF Document.

We hope this will be helpful. Please mention your issues separately if you face any further problem, or you may elaborate your scenario if you still face it. We will be more than happy to assist you.