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?
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;<span style="color:green;">//6x12 is the size of the icon</span> <span style="color:blue;">var</span> fileAttachment = <span style="color:blue;">new</span> Aspose.Pdf.InteractiveFeatures.Annotations.<span style="color:#2b91af;">FileAttachmentAnnotation</span>( page, <span style="color:blue;">new</span> Aspose.Pdf.<span style="color:#2b91af;">Rectangle</span>(x, y, x + 6, y + 12), <span style="color:blue;">new</span> Aspose.Pdf.<span style="color:#2b91af;">FileSpecification</span>(newFile, file.FileName, labelName)) { Icon = Aspose.Pdf.InteractiveFeatures.Annotations.<span style="color:#2b91af;">FileIcon</span>.Paperclip }; page.Annotations.Add(fileAttachment); <span style="color:green;">//add the appendix name near the icon</span> <span style="color:green;">//the 20 is to add bit of space next to the icon</span> <span style="color:blue;">var</span> textFragment = <span style="color:blue;">new</span> Aspose.Pdf.Text.<span style="color:#2b91af;">TextFragment</span>() { Position = <span style="color:blue;">new</span> Aspose.Pdf.Text.<span style="color:#2b91af;">Position</span>(x + 20, y), Text = labelName }; textFragment.TextState.FontSize = 12; textFragment.TextState.Font = Aspose.Pdf.Text.<span style="color:#2b91af;">FontRepository</span>.FindFont(<span style="color:#a31515;">"Arial"</span>); textFragment.TextState.ForegroundColor = Aspose.Pdf.<span style="color:#2b91af;">Color</span>.FromRgb(Color.Black); <span style="color:blue;">var</span> textBuilder = <span style="color:blue;">new</span> Aspose.Pdf.Text.<span style="color:#2b91af;">TextBuilder</span>(page); textBuilder.AppendText(textFragment); }</pre>