I am using Aspose.pdf to replace text in a pdf with file attachments. I want the attachments to show up in 2 places, someplace in the doc and at the end of the doc. The code that I have written add the file twice but the second one is corrupt for some reason. I would like to only add the file once and just point to the correct file. Why is the second file corrupt?
//find the text to replace the with the icon and label
var textToFind = new Aspose.Pdf.Text.TextFragmentAbsorber(file.FileGuid.ToString()); pdfDocument.Pages.Accept(textToFind); var textFragmentCollection = textToFind.TextFragments; foreach (Aspose.Pdf.Text.TextFragment guidText in textFragmentCollection) { //find the first instance of the GUID and replace it with the icon and label guidText.Text = string.Empty; var textRect = guidText.Rectangle; var page = guidText.Page; var x = (float)textRect.LLX; var y = (float)textRect.LLY; //6x12 is the size of the icon var fileAttachment = new Aspose.Pdf.InteractiveFeatures.Annotations.FileAttachmentAnnotation( page, new Aspose.Pdf.Rectangle(x, y, x + 6, y + 12), new Aspose.Pdf.FileSpecification(newFile, file.FileName, labelName)) { Icon = Aspose.Pdf.InteractiveFeatures.Annotations.FileIcon.Paperclip }; page.Annotations.Add(fileAttachment); //add the appendix name near the icon //the 20 is to add bit of space next to the icon var textFragment = new Aspose.Pdf.Text.TextFragment() { Position = new Aspose.Pdf.Text.Position(x + 20, y), Text = labelName }; textFragment.TextState.FontSize = 12; textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial"); textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Color.Black); var textBuilder = new Aspose.Pdf.Text.TextBuilder(page); textBuilder.AppendText(textFragment); }