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

Free Support Forum - aspose.com

Add file twice

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;
            <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>

Hi Martin,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 9.7.0 where I have used one of my sample PDF files and image file as attachment and I am unable to notice any issue when viewing/accessing attachment from resultant PDF file. For your reference, I have also attached the resultant PDF generated over my end.

Can you please try using the latest release and in case the problem still persists, please share the resource PDF and attachments file, so that we can further investigate the issue at our end. We are sorry for your inconvenience.

[C#]

Document pdfDocument = new Document(“c:/pdftest/Characters_RenderingIssue.pdf”);<o:p></o:p>

//find the text to replace the with the icon and label

var textToFind = new Aspose.Pdf.Text.TextFragmentAbsorber("Å Ä Ö");

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("c:/pdftest/myImage.png", "iphone 5s"))

{

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(System.Drawing.Color.Black);

var textBuilder = new Aspose.Pdf.Text.TextBuilder(page);

textBuilder.AppendText(textFragment);

}

pdfDocument.Save("c:/pdftest/TwoAttachmentsTest.pdf");