Update Hyperlinks in PDF - It is very urgent

Hi


I need to replace a hyperlink (text and url) in a pdf document, I tried the following code

   foreach (Aspose.Pdf.Page page in document.Pages)
{
// Get the link annotations from particular page
Aspose.Pdf.InteractiveFeatures.Annotations.AnnotationSelector selector = new Aspose.Pdf.InteractiveFeatures.Annotations.AnnotationSelector(new Aspose.Pdf.InteractiveFeatures.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));

            page.Accept(selector);
            <span style="color:green;">// Create list holding all the links</span>
            System.Collections.<span style="color:#2b91af;">IList</span> list = selector.Selected;
            <span style="color:green;">// Iterate through invidiaul item inside list</span>
            <span style="color:blue;">foreach</span> (Aspose.Pdf.InteractiveFeatures.Annotations.<span style="color:#2b91af;">LinkAnnotation</span> a <span style="color:blue;">in</span> list)

                {
		string url = (a.Action as Aspose.Pdf.InteractiveFeatures.GoToURIAction).URI;
if (url == “Free Support Forum - aspose.com”)
{
Console.WriteLine(
Destination: “
+ (a.Action as Aspose.Pdf.InteractiveFeatures.GoToURIAction).URI +
);
                        a.Action = new GoToURIAction(@“www.google.com”); 
			break;
			}	
		}
	}		
	   document.Save(filename1);

It changes the URL of the hyperlink but not the text. How to change the display text, I tried two methods
simple text replace which replaced but I can see a long underline after the hyperlink text
then I tried the following…
// a is retrieved from the above code ***
                        TextFragmentAbsorber ta = new TextFragmentAbsorber();
Aspose.Pdf.Rectangle rect = a.Rect;
rect.LLX -= 10;
rect.LLY -= 10;
rect.URX += 10;
rect.URY += 10;
ta.TextSearchOptions = new TextSearchOptions(rect);
ta.Visit(document.page); // page is coming from the above logic

                    <span style="color:green;">// Change color and text.</span>
                    <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">TextFragment</span> tf <span style="color:blue;">in</span> ta.TextFragments)
                    {
                        tf.TextState.ForegroundColor = Aspose.Pdf.<span style="color:#2b91af;">Color</span>.Red;
                        tf.Text = <span style="color:#a31515;">"Click Here"</span>;
                    }
                    <span style="color:blue;">break</span>;</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">but this replaces  the text but many times as attached(after.img), "Click Here" is appeared many times in the same sentence and the hyperlink format (underline and blue colored text are not disappearing from the previous long URL)</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">It would be helpful if you give me some suggestion to resolve this issue ,its bit urgent.</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">otherwise, what is the best way to delete the existing hyperlinks (text and url) and add a new one at the same location? can I have the sample code ?</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">Please check the attached images.</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">Regards</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">Sam.</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre></pre></div>

Hello Sam,

Thanks for contacting support.

As per my understanding, you want to change both text and URL of hyperlink inside PDF. In order to do that, you can first replace the text inside PDF using TextFragmentAbsorber and then add a link annotation with the updated rectangle of the TextFragment. Please check following code snippet where I have implemented the requirement on one of my sample PDFs.

var doc = new Document(dataDir + “abc_out.pdf”);

TextFragmentAbsorber filter = new TextFragmentAbsorber("https://www.aspose.com/community/forums/addpost.aspx?ForumID=20");

filter.TextSearchOptions = new TextSearchOptions(false);

doc.Pages.Accept(filter);

foreach (TextFragment content in filter.TextFragments)

{

    content.Text = "Click Here";

    content.TextState.ForegroundColor = Color.Blue;

    content.TextState.Underline = true;

    LinkAnnotation annot = new LinkAnnotation(content.Page, content.Rectangle); // Link Annotation would be placed upon updated rectangle

    annot.Action = new GoToURIAction("https://www.aspose.com/community/forums/addpost.aspx?ForumID=20");

    Border b = new Border(annot);

    b.Width = 0;

    annot.Border = b;

    content.Page.Annotations.Add(annot);

}

doc.ProcessParagraphs();

doc.Save(dataDir + "abc_out.pdf");

Please try using above code snippet for text and URL replacements and in case if you still face any issue, please share your input document, so that we can try to replicate the issue in our environment and address it accordingly.

Best Regards,

Hi Asad Ali


Many Thanks for your support.

I have already tried the same but not able to get rid of the underline from the existing hyperlink (as attached image). Please find the attached source file (“Airlines.pdf”) and the output file (“abc_out.pdf”) using your code.

The URL value of the hyperlink will not be changed and the proposed display text of the hyperlink is smaller than the existing display text

We need to modify large amount pdf documents and send them out today, Can you please clarify this asap?

Regards
Sam

Hello Sam,

Thanks for sharing input/output files.

I have tested the scenario again with your shared files and observed that existing hyperlink was not removing from the PDF with code snippet, shared earlier. As your document contains link annotation in it, so you need to delete/replace the annotation and replace the text under that annotation after.

sam.karthik:

The URL value of the hyperlink will not be changed and the proposed display text of the hyperlink is smaller than the existing display text

Please check following code snippet where I have removed the existing hyperlink first and after that added new link annotation. For your reference, I have attached an output as well.

var doc = new Document(dataDir + “abc_out.pdf”);

foreach (Aspose.Pdf.Page page in doc.Pages)

{

    // Get the link annotations from particular page

    AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));

    page.Accept(selector);

    // Create list holding all the links

    System.Collections.IList list = selector.Selected;

    // Iterate through invidiaul item inside list

    foreach (LinkAnnotation a in list)

    {

        string url = (a.Action as GoToURIAction).URI;

        if (url == "http://www.bbc.co.uk/news/science-environment-40133319")

        {

            page.Annotations.Delete(a);

            break;

        }

    }

}

TextFragmentAbsorber filter = new TextFragmentAbsorber("https://www.aspose.com/community/forums/addpost.aspx?ForumID=20");

filter.TextSearchOptions = new TextSearchOptions(false);

doc.Pages.Accept(filter);

foreach (TextFragment content in filter.TextFragments)

{

    content.Text = "Click Here";

    content.TextState.ForegroundColor = Color.Blue;

    content.TextState.Underline = true;

    LinkAnnotation annot = new LinkAnnotation(content.Page, content.Rectangle); // Link Annotation would be placed upon updated rectangle

    annot.Action = new GoToURIAction("https://www.aspose.com/community/forums/addpost.aspx?ForumID=20");

    Border b = new Border(annot);

    b.Width = 0;

    annot.Border = b;

    content.Page.Annotations.Add(annot);

}

doc.ProcessParagraphs();

doc.Save(dataDir + "abc_out.pdf");

sam.karthik:

I have already tried the same but not able to get rid of the underline from the existing hyperlink

We already have logged an issue for underline text as PDFNET-39639 in our issue tracking system. I have associated the issue ID with this forum thread as well, so that you will get notification once the issue has been resolved. Please be patient and spare us little time.

We are sorry for the inconvenience.

Best Regards,

Hello I’m trying to update only the Link target to “_blank”. would using this code make sense for me? or is there an easier way? Do i need to get all the LinkAnnotations? do those represent all of the links in the document body? the name is quite confusing.

Hello Shayan,


Thanks for you inquiry.

In case if your PDF document contains LinkAnnotation, you can simply delete it like in the earlier shared code snippet, as this will delete the link target of the annotation. Furthermore in order to get more information regarding manipulating annotations, you may also visit “Add, Delete and Get Annotation” article(s) in the API documentation.

In a case if you still face any issue, please share your input document along with sample code snippet, so that we can test the scenario in our environment and address it accordingly.


Best Regards,

Okay ive done this. Which property contains the Target attribute on the anchor tag. i need to set this to _blank so when it is embedded into a webpage the links open in a new tab. Is this possible?

Hello Shayan,

Thanks for adding more details to the scenario.

Please note the Hyperlinks work differently in the PDF than they do in HTML. In order to open an external link from PDF, into a web browser, you can either use a link annotation or embed some JavaScript inside PDF document. Please check following code snippet where I have added links with two different methods (i.e LinkAnnotation, JavaScript).

Document pdfDocument = new Document();

pdfDocument.Pages.Add();

pdfDocument.Pages[1].Paragraphs.Add(new TextFragment("Click Here to goto link."));

pdfDocument.Pages[1].Paragraphs.Add(new TextFragment("Click Here to goto link."));

pdfDocument.Save(new MemoryStream());

TextFragmentAbsorber absorber = new TextFragmentAbsorber("Click Here to goto link.");

pdfDocument.Pages[1].Accept(absorber);

LinkAnnotation link = new LinkAnnotation(absorber.TextFragments[1].Page, absorber.TextFragments[1].Rectangle);

link.Action = new GoToURIAction("http://www.aspose.com");

absorber.TextFragments[1].Page.Annotations.Add(link);

pdfDocument.Save(new MemoryStream());

Field field = new ButtonField(absorber.TextFragments[2].Page, absorber.TextFragments[2].Rectangle);

pdfDocument.Form.Add(field);

field.Actions.OnPressMouseBtn = new JavascriptAction(@"app.launchURL('http://www.aspose.com', true);");

pdfDocument.Save(dataDir + "external_links.pdf");

For your reference, I have also attached an output, generated by above code. Now concerning to the opening link in new tab (with target “_blank”), the PDF format does not understand the meaning of tabs, as it is not designed for browsing experience. Though many internet browsers offer the feature to view PDF files but most of them do not support execution of embedded JavaScript in PDF and which of them support JavaScript, it is very little.

If you open the shared output with Google Chrome, you may notice that the JavaScript link will not be working as Google Chrome viewer does not support this unless you use some different or updated viewer plugin to view the files in chrome. Whereas for the first link, which is placed using link annotation, if you click it by pressing and holding Ctrl key, it will open in new tab.

As a workaround, you can convert PDF into HTML and render resultant HTML into your webpage, as per your requirement. To convert PDF into HTML, please check “Convert PDF file into HTML” article(s) in API documentation.

Best Regards,