Add hyperlinks to Table Cells in PDF using C# with Aspose.PDF - Tooltip displays complete URL

Hi

I'm using Aspose.Pdf.Generator.Pdf class to create a new page with a table of values.

I want to be able to specify a hyperlink (local) for those values - yet I can find no means of being able to do this.

Please advise how I add hyperlinks to cell values.

Regards

Mark

Ps. A prompt response on this would be appreicated. I'm going round and round in circles trying to use this product - and every corner I turn I seem to end in big old wall.

I think I've got round the immediate problem. I would still be interested in the answer for futre use.

For those interested; I've now:

  • Loading an existing (blank) Pdf
  • using PdfFileMend.AddText to add text (using positioning to get a table like structure)
  • then using PdfContentEditor.CreateLocalLink to add the hyperlink

Mark

Hi Mark,

Thanks for using our products.

Kindly use the below code for adding hyperlink in a table cell using Aspose.Pdf.Generator. Resultant PDF document is attached for your reference.

[C#]

//Create a section in the pdf document<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Aspose.Pdf.Generator.Pdf pdfConv = new Aspose.Pdf.Generator.Pdf();

//Create a section in the pdf document

Aspose.Pdf.Generator.Section sec1 = pdfConv.Sections.Add();

//Instantiate a table object

Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();

//Add the table in paragraphs collection of the desired section

sec1.Paragraphs.Add(tab1);

//Set default cell border using BorderInfo object

tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All, 0.1F);

//Set with column widths of the table

tab1.ColumnWidths = "100 100 120";

//Create text paragraph with the reference of a section

Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text();

//Create a text segment

Aspose.Pdf.Generator.Segment segment1 = new Aspose.Pdf.Generator.Segment();

//Add a text segment in the text paragraph

segment1 = text1.Segments.Add("this is a local link");

//Set the text in the text segment to be underlined

segment1.TextInfo.IsUnderline = true;

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

segment1.Hyperlink.LinkType = Aspose.Pdf.Generator.HyperlinkType.Local;

//Assign the id of the desired paragraph as a target id for the text segment

segment1.Hyperlink.TargetID = "product1";

//Create a text paragraph to be linked with the text segment

Aspose.Pdf.Generator.Text text3 = new Aspose.Pdf.Generator.Text(sec1, "product 1 info ...");

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

sec1.Paragraphs.Add(text3);

//Set this paragraph to be the first so that it can be displayed in a separate

//page in the document

text3.IsFirstParagraph = true;

//Set the id of this text paragraph to "product1"

text3.ID = "product1";

//Create rows in the table and then cells in the rows

Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add();

row1.Cells.Add("Sample text in cell");

// Add the cell which holds the image

Aspose.Pdf.Generator.Cell cell2 = row1.Cells.Add();

//Add the image to the table cell

cell2.Paragraphs.Add(text1);

// save the Pdf file

pdfConv.Save(@"d:\pdffiles\Hyperlink_in_Cell.pdf");

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Does this work only for HyperLink type ‘Local’? When I tried the same for external URL like Google, it didn’t work! Please suggest the code to hyperlink to external URLs.

@sandhyacp

Thanks for your inquiry.

Please note that the code shared above is related to discontinued Aspose.Pdf.Generator approach and quite old. However, new Aspose.Pdf (DOM) approach is now recommended which is more efficient and improved. In order to add hyperlink (external web URL) in a table cell, please use following code snippet with Aspose.Pdf (DOM) model:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// Create a page
Aspose.Pdf.Page page = doc.Pages.Add();
// Create Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Create new Row
Row row = table.Rows.Add();
// Add Cell to Row
Cell cell = row.Cells.Add();
// Create a TextFragment
TextFragment textFragment = new TextFragment("This is link text");
textFragment.TextState.Underline = true;
textFragment.TextState.ForegroundColor = Color.Blue;
textFragment.Hyperlink = new Aspose.Pdf.WebHyperlink("https://google.com");
// Add text to cell
cell.Paragraphs.Add(textFragment);
// Add table to PDF page
page.Paragraphs.Add(table);
// Save the Pdf file
doc.Save(dataDir + @"Hyperlink_in_Cell.pdf"); 

Hyperlink_in_Cell.pdf (2.2 KB)

Please use Aspose.Pdf for .NET 18.7 while trying above code snippet and in case of any further assistance, please feel free to let us know.

Hi Asad Ali,

Thanks for the response.
With Aspose.Pdf.Generator, I was able to achieve the hyperlink to external site. Below is my code:

Aspose.Pdf.Generator.Pdf pdfConv = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section sec1 = pdfConv.Sections.Add();
Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();
sec1.Paragraphs.Add(tab1);
tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All, 0.1F);
tab1.ColumnWidths = “100 100 120”;
Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text();

                        Aspose.Pdf.Generator.Segment segment1 = new Aspose.Pdf.Generator.Segment();
                        segment1 = text1.Segments.Add("This a Web link");
                        segment1.TextInfo.IsUnderline = true;
                        //Aspose.Pdf.Generator.Color color = new Aspose.Pdf.Generator.Color("blue");
                        segment1.TextInfo.Color = new Aspose.Pdf.Generator.Color("blue"); //color;
                        segment1.Hyperlink.Url = "https://www.google.com/";
                        segment1.Hyperlink.LinkType = Aspose.Pdf.Generator.HyperlinkType.Web;

                        Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add();
                        row1.Cells.Add("Sample text in cell");
                        Aspose.Pdf.Generator.Cell cell2 = row1.Cells.Add();
                        cell2.Paragraphs.Add(text1);
                        pdfConv.Save("D:\\SCP003\\POC\\Output1.pdf");

The issue now is, when i hover on the hyperlink in the pdf, the screen tip displays the complete URL. How do I change this?

The same issue even with using Aspose.Pdf. Below is my Code:
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
Aspose.Pdf.Row row = table.Rows.Add();
Aspose.Pdf.Cell cell = row.Cells.Add();
Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment(“This is link text”);
textFragment.TextState.Underline = true;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
textFragment.Hyperlink = new Aspose.Pdf.WebHyperlink(“https://google.com”);
cell.Paragraphs.Add(textFragment);
page.Paragraphs.Add(table);
doc.Save(@“D:\SCP\Test.pdf”);

Can you please check how can I change the screen tip text for hyperlinks?

@sandhyacp

Thanks for getting back to us.

We have also observed the same behavior in output PDF generated by Aspose.Pdf DOM approach. We have logged an investigation ticket as PDFNET-45249 in our issue tracking system to investigate how can this behavior be handled using API. As soon as we have some definite updates regarding ticket resolution, we will let you know. Please be patient and spare us little time.

We are sorry for the inconvenience.

Hi Team,

Any update on this issue?

@sandhyacp

Thanks for your inquiry.

As we recently have logged this issue in our issue tracking system, I am afraid it is still pending for review. Please note that the issue has been logged under free/normal support model and has low priority. It would definitely be investigated/resolved but, on first come first serve basis. As soon as we have some definite updates regarding its resolution, we will certainly let you know. Please spare us little time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-45249) have been fixed in Aspose.PDF for .NET 18.10.

How can we create hyperlink for external document?
i have tried ur above mentioned example for localhyperlink,webhyperlink & filehyperlink and it is working. But i need to create an extrnal hyperlink not able to find any example on it.

@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.