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

Free Support Forum - aspose.com

How to create new hyperlinks in pdf documents

Hi,

Could you please help me with creating hyperlinks in pdf document.

I tried with sample document posted, but i am not getting the expected result.

java.io.File file = new java.io.File(“D:/Dharma/Aspose/word-hypderlink-issue/output_Text_Updated.pdf”);

java.io.FileInputStream fis;
try {
fis = new java.io.FileInputStream(file);
Pdf pdf = new Pdf(fis);
Section sec1 = new Section(pdf);

Text text1 = new Text(sec1);
//Add the text paragraph in the paragraphs collection of the section
sec1.getParagraphs().add(text1);

//Add a text segment in the text paragraph
Segment segment1 = text1.getSegments().add(“this is a web link”);

//Set the text in the segment to be underlined
segment1.getTextInfo().isUnderline(true);

//Set the link type of the text segment to Pdf
segment1.getHyperlink().setLinkType(HyperlinkType.Web);
segment1.getHyperlink().setUrl(“http://www.google.com/”);
pdf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Please help me

Hi Dharmender,


The aspose.pdf package supports the feature to create PDF files from scratch and it does not support the feature to manipulate existing PDF files. As per your above code snippet, instead of using Direct-to-File mode, I recommend using simple/conventional approach of generating PDF files. Please try using the following code snippet to add hyperlinks while generating PDF files.

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

[Java]

aspose.pdf.Pdf
pdf =
new
aspose.pdf.Pdf();<o:p></o:p>

aspose.pdf.Section sec1 = pdf.getSections().add();

aspose.pdf.Text text1 = new aspose.pdf.Text(sec1);

//Add the text paragraph in the paragraphs collection of the section

sec1.getParagraphs().add(text1);

//Add a text segment in the text paragraph

aspose.pdf.Segment segment1 = text1.getSegments().add("this is a web link");

//Set the text in the segment to be underlined

segment1.getTextInfo().isUnderline(true);

//Set the link type of the text segment to Pdf

segment1.getHyperlink().setLinkType(aspose.pdf.HyperlinkType.Web);

segment1.getHyperlink().setUrl("http://www.google.com/");

pdf.save("c:/pdftest/Hyperlinkadded.pdf");

Hi,

Do you have any other class/package in aspose pdf which can be used to create the hyperlinks in existing pdf. or change the text in the existing pdf document as hyerlinks.

Hi Dharmender,


Please notice my reply 564649 in your other forum thread. In case of any further query, please feel free to contact.