Create hyperlink in existing pdf file

Hi,

I have a pdf file which has some text like “HYPER_Google_1”(attached input.png file), I would like to replace this text with “Google”. However I dont want to display the anchor tag in the pdf file. My required output should be something like the attached output(expected_output file) pdf file.

Could you please help me with this requirement.

I tried with below sample code.

String key = “http://www.google.com/”;
key = “"”+key+“"”;
key = “Google”;
pdfContentEditor.replaceText(“HYPER_Google_1”, key);
//save output PDF
pdfContentEditor.save(“D:/Dharma/Aspose/word-hypderlink-issue/ReplaceTextOnAllPages.pdf”);
createHyperlink();
System.out.println(“Text replaced successfully!”);


Hi Dharmender,


Thanks for contacting support.

In order to accomplish your requirement, please try using following code snippet. For your reference, I have also attached the resultant PDF generated over my end.

[Java]

//open document<o:p></o:p>

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("C:\\pdftest\\output.pdf");

//create TextAbsorber object to find all instances of the input search phrase

com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("Google");

//accept the absorber for first page of document

pdfDocument.getPages().accept(textFragmentAbsorber);

//get the extracted text fragments into collection

com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

//get first occurrence of text and replace

com.aspose.pdf.TextFragment textFragment = textFragmentCollection.get_Item(1);

//update text and other properties

textFragment.setText("GOOGLE");

textFragment.getTextState().setFont(com.aspose.pdf.FontRepository.findFont("Verdana"));

textFragment.getTextState().setFontSize(12);

// save updated PDF file

pdfDocument.save(“C:\pdftest\output_Text_Updated.pdf”);

Hi,

When I tried from my side, I am getting the same issue. The hyperlink from the text is getting disapppear.

I have attached both input and output snapshots.

Also is there any method for creating new hyperlinks in the existing pdf documents.

Hi Dharmender,


During my testing, I have used the latest release of Aspose.Pdf for Java 9.3.1 in Eclipse Juno application running over Windows 7 (x64) with JDK 1.7. Can you please share some details regarding your working environment i.e. API version, Operating System version, JDK version etc.

Now concerning to the requirement of adding hyperlinks in existing PDF file, the code snippet shared over following link can be used Add Hyperlink in PDF file. However I have observed that our API having some issues while adding hyperlink (the title and content of hyperlink are not visible in resultant file). For
the sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34365. We will
investigate this issue in details and will keep you updated on the status of a
correction. We
apologize for your inconvenience.

Hi,

Even i am using the environment what you are using. but still dont know how i am not able to achieve the output.


Please find the attached files.

Hi Dharmender,


I have further investigated the issue and have found that when replacing URL inside PDF, the hyperlink is also removed. However in order to accomplish your requirement, you need to perform two steps. First you need to replace the URL inside PDF file and then add the hyperlink over same location (where replaced text is inserted). Please take a look over following code snippet.

For your reference, I have also attached the output generated over my end.

[Java]

//open
document
<o:p></o:p>

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("C:\\pdftest\\output_pdf_from_word.pdf");

//create TextAbsorber object to find all instances of the input search phrase

com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("Google");

//accept the absorber for first page of document

pdfDocument.getPages().accept(textFragmentAbsorber);

//get the extracted text fragments into collection

com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

//get first occurrence of text and replace

com.aspose.pdf.TextFragment textFragment = textFragmentCollection.get_Item(1);

//update text and other properties

textFragment.setText("GOOGLE");

textFragment.getTextState().setFontSize(12);

//Create Link annotation object over same position where replaced text is inserted

com.aspose.pdf.LinkAnnotation link = new com.aspose.pdf.LinkAnnotation(pdfDocument.getPages().get_Item(1), new com.aspose.pdf.Rectangle(textFragment.getRectangle().getLLX(), textFragment.getRectangle().getLLY(), textFragment.getRectangle().getURX(), textFragment.getRectangle().getURY()));

//Create border object for LinkAnnotation

com.aspose.pdf.Border border = new com.aspose.pdf.Border(link);

//Set the border width value as 0

border.setWidth(0);

//Set the border for LinkAnnotation

link.setBorder(border);

//Specify the link type as remote URI

link.setAction(new com.aspose.pdf.GoToURIAction("www.google.com"));

//Add link annotation to annotations collection of first page of PDF file

pdfDocument.getPages().get_Item(1).getAnnotations().add(link);

//save updated PDF file

pdfDocument.save("C:\\pdftest\\output_Updated.pdf");

Hi,

Thanx for your respone.

I have used the above code sample and tested on my standalone program it is working. But when i integrate the same code into application, it is giving Null pointer exception at pdfDoc.getPages().accept(textFragmentAbsorber);

Could you please help me in troubleshooting the issue.

adding to the above post, I would like to know if is there any method to change the font colour and font size of the hyperlink text using the above code sample.

reddydharmender:
Hi,

Thanx for your respone.

I have used the above code sample and tested on my standalone program it is working. But when i integrate the same code into application, it is giving Null pointer exception at pdfDoc.getPages().accept(textFragmentAbsorber);

Could you please help me in troubleshooting the issue.
Hi Dharmender,

During our testing with one of our sample PDF files, We did not notice any issue and it seems to be related to the source PDF file which you are using. Can you please share the resource file so that we can test the scenario at our end. We are sorry for this inconvenience.

reddydharmender:
adding to the above post, I would like to know if is there any method to change the font colour and font size of the hyperlink text using the above code sample.
Hi Dharmender,

In order to change the Font Size/Color of hyperlink, we in fact need to change the replaced text and as the hyperlink is placed over same coordinates as the replaced text, so it will appear that hyperlink Size/Color is changed. Please try using following code lines.

For your reference, I have also attached the resultant PDF file.

[Java]

//update
text and other properties
<o:p></o:p>

textFragment.setText("GOOGLE");

textFragment.getTextState().setFontSize(22);

textFragment.getTextState().setForegroundColor(com.aspose.pdf.Color.getYellowGreen());

Hi,

I am using the same pdf which i attached to the post. when I debugged furthur I observed that the textFragmentCollection is getting as zero, even where there is content in the pdf.

I am again attaching the same pdf for your reference.

is there any alternate approach to achieve this requirement.

Hi,

I am using the same pdf which i attached to the post. when I
debugged furthur I observed that the textFragmentCollection is getting
as zero, even where there is content in the pdf.

I am again attaching the same pdf for your reference.

is there any alternate approach to achieve this requirement.

I am facing this issue on Unix machine but not facing this on windows machine.

i am using the latest aspose-pdf-9.3.1-jdk16 jar file for this.

Hi Dharmender,


As per my understanding, you are facing an issue while searching text inside PDF document because TextFragmentCollection being returned is 0 over Unix platform. I have logged this problem
as PDFNEWJAVA-34373 in our issue tracking system. We will further
investigate this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.

Hi Team,

Could you please update me on the status of the ticket PDFNEWJAVA-34373 as I need to update the same to my client.

Regards,
Dharmender`

Hi Dharmender,


Thanks for your inquiry. I am afraid your issue is not resolved as we have noticed it recently. It is pending for investigation in the queue with other priority task. As soon as its investigation is completed then we will share our findings or ETA with you.

We are sorry for the inconvenience caused.

Best Regards,

The issues you have found earlier (filed as PDFNEWJAVA-34365) have been fixed in Aspose.Pdf for Java 10.3.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

The issues you have found earlier (filed as PDFNEWJAVA-34373) have been fixed in Aspose.Pdf for Java 12.0.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.