Add external hyperlink to text in a table cell inside PDF document with Aspose.PDF for .NET

            Table tab = new Table();
	Row row = tab.getRows().add();
	Cell cell2 = row.getCells().add();
            cell.getParagraphs().add(textFragment);

@amitchakravarthy

Thanks for contacting support.

When you say external hyperlink, would you please elaborate a bit more about it. Would you kindly share a sample PDF document for us which contains external hyperlinks so that we can test the scenario in our environment and address it accordingly.

We are creating new table in a new document where we have 2 columns so while creating the table we want to add hyperlinks for all the column 2 text. I m attaching the sample pdf which we are trying to create by below code.
private static void createDocument() {
Document doc = new Document();
Page page = doc.getPages().add();
Table table = new Table();
for (int row_count = 1; row_count < 200; row_count++) { // add row to table
Row row = table.getRows().add(); // add table cel
row.getCells().add(“Section 1” + row_count);
Cell cell2 = row.getCells().add();
if (row_count % 2 == 0) {
createTextWithHyperlink(cell2);
} else {
createWebHyperlink(cell2);
}
}
}

 private static void createTextWithHyperlink(Cell cell) {
	TextFragment textFragment = new TextFragment("This is link text");
	textFragment.getTextState().setUnderline(true);
	textFragment.getTextState().setForegroundColor(Color.getBlue());
	textFragment.setHyperlink(new FileHyperlink("multiheader2.pdf"));
	cell.getParagraphs().add(textFragment);
}

private static void createWebHyperlink(Cell cell) {
	TextFragment textFragment = new TextFragment("This is link text");
	textFragment.getTextState().setUnderline(true);
	textFragment.getTextState().setForegroundColor(Color.getBlue());
	textFragment.setHyperlink(new WebHyperlink("https://google.com"));
	cell.getParagraphs().add(textFragment);
}

multiheader1.pdf (84.1 KB)

we tried to achieve with LinkAnnotation but we are not able to get rectangle details for the newly creating textfragment. As for newly creating text the x & y cordinates are returning as 0

private static void createLinkAnnotation(Page page,Cell cell) {
	TextFragment textFragment = new TextFragment("This is GotoR hyperlink");
	cell.getParagraphs().add(textFragment);		
	LinkAnnotation link = new LinkAnnotation(page, textFragment.getRectangle());
	Border border = new Border(link);
	border.setWidth(1);
	link.setBorder(border);
	link.setAction(new GoToRemoteAction("C:\\outputFolder\\dest33.pdf",1));
	page.getAnnotations().add(link);
}

multiheader.pdf (168.9 KB)

@amitchakravarthy

Your understandings are correct and we are afraid that adding GoToRemoteAction functionality is not available at the time of PDF Generation. For the sake of implementation, we have logged an enhancement ticket as PDFNET-48109 in our issue tracking system. We will surely keep you posted with the status of its implementation. Please spare us some time.

As a workaround, you may please add the action after the PDF file is saved. You can search for the targeted text and add LinkAnnotation around it with desired action. Please consider using following code snippet:

Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Pages[1].Paragraphs.Add(new TextFragment("Click Here to Get Attachment."));
pdfDocument.Save(new MemoryStream());
TextFragmentAbsorber absorber = new TextFragmentAbsorber("Click Here to Get Attachment.");
pdfDocument.Pages[1].Accept(absorber);
foreach (TextFragment tf in absorber.TextFragments)
{
 LinkAnnotation link = new LinkAnnotation(tf.Page, tf.Rectangle);
 link.Action = new GoToRemoteAction(dataDir + "output3.pdf", 1);
 tf.Page.Annotations.Add(link);
}
pdfDocument.Save(dataDir + "output.pdf");