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

Free Support Forum - aspose.com

Change Link Label

With below code I am able to replace to uri address of links but don’t know how to replace link label.

Document document = new Document("E:\\files\\Testing_hyperlink.pdf");

for (Page page : document.getPages()) {

		for (Object object : page.getAnnotations()) {
					
		Annotation annotation = (Annotation) object;
					
		if(annotation.getAnnotationType() == AnnotationType.Link) {
						
			LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
						
			if(linkAnnotation.getAction() instanceof GoToURIAction) {
							
				GoToURIAction gotoAction = (GoToURIAction) linkAnnotation.getAction();
							
				System.out.println(gotoAction.getURI());
							
				gotoAction.setURI("www.gmail.com");
			}
						
		}
					
	}
		
}
document.save("E:\\files\\Testing_hyperlink1.pdf");

@subham115

Thank you for contacting support.

Please try using below code snippet in your environment and then share your kind feedback with us.

Document document = new Document(dataDir + "Test.pdf");
for (Page page : document.getPages()) 
{
    for (Object object : page.getAnnotations()) 
    {
        Annotation annotation = (Annotation) object;
        if(annotation.getAnnotationType() == AnnotationType.Link) 
        {
            LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
            // get hyperlink text
            TextFragmentAbsorber absorber = new TextFragmentAbsorber();
            absorber.getTextSearchOptions().setRectangle(linkAnnotation.getRect());
            absorber.visit(page);
            for (TextFragment tf : absorber.getTextFragments())
            {
                System.out.println(tf.getText());
                tf.setText("MODIFIED TEXT");
                linkAnnotation.setRect(tf.getRectangle());
            }
            if(linkAnnotation.getAction() instanceof GoToURIAction) 
            {
                GoToURIAction gotoAction = (GoToURIAction) linkAnnotation.getAction();
                System.out.println(gotoAction.getURI());
                gotoAction.setURI("www.gmail.com");
            }
        }
    }
}
document.save( dataDir + "Testing_hyperlink1_18.2.pdf");

I hope this will be helpful. Please feel free to contact us, while sharing your environment details and source PDF document, if you need any further assistance.

Thanks for your support.
Link Labels are getting replaced now but there are multiple labels created for single link and they are overlapping each other. Is there a way to get entire link label in single TextFragment or any other approach to solve this?

I am attaching my source and destination pdf files named as Testing_hyperlink.pdf and Testing_hyperlink1.pdf respectively.
Testing_hyperlink.pdf (161.1 KB)
Testing_hyperlink1.pdf (191.2 KB)

@subham115

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID PDFJAVA-37552 has been logged in our issue management system for further investigation and resolution. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.

We are sorry for the inconvenience.

@subham115

Thank you for being patient.

We have investigated and resolved the ticket reported by you and would like to request you to use below code snippet in your environment and then share your kind feedback with us.

Document document = new Document(dataDir + "Hyperlink_17.4.pdf");
    for (Page page : document.getPages()) {
        for (Object object : page.getAnnotations()) {
            Annotation annotation = (Annotation) object;
            if (annotation.getAnnotationType() == AnnotationType.Link) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                TextFragmentAbsorber absorber = new TextFragmentAbsorber();

                TextEditOptions editOptions = new TextEditOptions(TextEditOptions.LanguageTransformation.Default);
                editOptions.setToAttemptGetUnderlineFromSource(true);

                TextSearchOptions textSearchOptions = new TextSearchOptions(true);
                absorber.setTextSearchOptions(textSearchOptions);
                absorber.setTextEditOptions(editOptions);

                absorber.getTextSearchOptions().setRectangle(linkAnnotation.getRect());
                absorber.visit(page);
                TextFragment tf = absorber.getTextFragments().get_Item(1);
                tf.setText("MODIFIED TEXT");
                linkAnnotation.setRect(tf.getRectangle());

                if (linkAnnotation.getAction() instanceof GoToURIAction) {
                    GoToURIAction gotoAction = (GoToURIAction) linkAnnotation.getAction();
                    gotoAction.setURI("www.gmail.com");
                }
            }
        }
    }
    document.save(dataDir + "output_1.pdf");

It replaces the label of hyperlink as per your requirements. We have attached the generated file for your kind reference output_1.pdf. Please feel free to contact us if you need any further assistance.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan