Create Hyperlinks in Aspose.PDF.Table


Hi,

I have a System.Data.DataTable of contents, that I have inserted into a PDF document using the Aspose.PDF.Table.ImportDataTable method.

Is it possible to convert or replace the contents entries in my table with hyperlinks so that the user can click on an item to be automatically redirected there?

Thanks.

Shane

Hi Shane,

The data imported to a table using ImportDataTable would be treated and rendered as simple text. However, if you’re interested to add Hyperlinks to the PDF file then you’ll have to Hyperlink class. You can find the details about setting a hyperlink to a web location in the following article:
Hyperlink to a web location

You can also find details on other variation of hyperlinks at the following link:
Working with hyperlinks

I hope this helps. If you have any further questions, please do let us know.
Regards,

Thanks for the info Shahzad - how about if I do not use ImportDataTable, but build my data table manually instead?
Is there any way to use hyperlinks in an Aspose.Table at all?

Thanks,

Shane

Hello Shane,

Thanks for your interest in our products.

As far as I can see from your first query, you seem to be interested in creating Table Of Contents. If so is the case, then please take a look over the following link for required information Manipulating List of Contents

Or, in case you're interested in having Hyperlinks in table columns, please try using the following code snippet.

[C#]

//Create a Pdf instance
Pdf pdf1 = new Pdf();
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

//Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
//Set with column widths of the table
tab1.ColumnWidths = "150 150";
//Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
//Create MarginInfo object and set its left, bottom, right and top margin
MarginInfo margin = new MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;
//Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding = margin;

//Add the table in paragraphs collection of the desired section
sec1.Paragraphs.Add(tab1);

// create a table with 10 rows
for (int i = 1; i <= 10; i++)
{
//Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();
row1.Cells.Add("Simple Text at line" + i.ToString());
//Create text paragraph with the reference of a section
Text text1 = new Text();
//Add the text paragraph in the paragraphs collection of the section
Aspose.Pdf.Cell cell1 = row1.Cells.Add();
cell1.Paragraphs.Add(text1);
//Add a text segment in the text paragraph
Segment segment1 = text1.Segments.Add("Local link to page " + (i+1).ToString());
//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 = HyperlinkType.Local;
//Assign the id of the desired paragraph as a target id for the text segment
segment1.Hyperlink.TargetID = "product"+i.ToString();
//Create a text paragraph to be linked with the text segment
Text text3 = new Text(sec1, "product info at page..." +(i+1).ToString());
//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 = "product"+i.ToString();
}

pdf1.Save(@"d:/pdftest/TableTemplate.pdf");

The resultant PDF generated with above code is in attachment, please take a look. In case it does not resolve your problem or you've any further query, please feel free to contact.

Thanks for the example Nayyer.
Is there a way to set the TargetID property of a Hyperlink object to a page number rather than a paragraph ID? (I am creating a contents page for a larger document that has been concatenated using the Pdf.Kit.)

Thanks,

Shane



Hello Shane,

Thanks for considering Aspose.

Please try using LinkPageNumber property of Hyperlink class.

In case it does not satisfy your requirement or you've any further query, please feel free to contact.


Sorry Nayyer, I really should have noticed that one myself.

I have set the LinkPageNumber property as you advised, but when I click on the hyperlinks in the finished document, it always links to Page 1.

I suspect that this is because when I create the document, it is only 1 page long, so the Aspose dll automatically corrects the page number that I provide, so it is not more that the total number of pages in the document.

However, I want to refer to pages that will exist when all the PDFs are concatenated together. Is it possible to achieve this goal using the LinkPageNumber property?

Thanks,

Shane

Hello Shane,

Thanks for your interest in our products.

LinkPageNumber property is present in HyperLink class of Aspose.Pdf for .NET and it only contains the information of page numbers when the PDF document is being generated. But, as per your scenario, your final document is generated as a resultant of concatenation performed using Aspose.Pdf.Kit for .NET. In that case the page numbers information is updated. In this case, I would suggest you to use CreateLocalLink(..) method of PdfContentEditor class of Aspose.Pdf.Kit. For more information, please visit the following link Create Links.

In case it does not satisfy your requirements or you've any further query, please feel free to contact.

Hi Nayyer,

This would work well, but I would need a method that allows me to search for text in my “table of contents” page and return it’s location (as a point), so I can then specify the rectangle for active click.
Is there such a method in the PDF.Kit namespace?

Thanks,

Shane

Hi Shane,

I would like to inform you that this feature is currently not supported in Aspose.Pdf.Kit. We have already logged this feature requirement as PDFKITNET-9186 in our issue tracking system. Our team is looking into this requirement and you’ll be updated once this feature is supported in the component.

We’re sorry for the inconvenience.
Regards,


Ok, thanks Shahzad.

Hi,

Since now it’s 10 years past, any progress on this issue?
I’ve exactly same scenario, ToC is generated seperately and then PDFs are concatenated together to generate final PDF, any way of linking content from ToC to respective pages same as MS Word ToC?

Thanks,
Waqar

@waqarazeem1982

Thanks for reviving this thread.

Aspose.PDF now offers a feature to add TOC in existing PDF document. You may please check the documentation link which has been shared as well. In case you face any issue, please feel free to let us know.

Hello,

even though it is now a little bit :wink: later, this piece of code is exactly what I need. Unfortunately, I seem to miss a detail here.

First things first, I’m in no position to use any update or upgrades, I can only use version 6.8 of Aspose.Pdf for .net.

Is it anyhow possible to aim at a target in a table?

Thanks
Casey

@CaseyMcCall

We are afraid that you are using quite older version of the API and regretfully, you will not be able to use updated and newly added feature to the API unless you upgrade to the latest version. Also, there are a lot of classes and method in the older version of the API which have been discontinued and we are not providing support against them.

@asad.ali
Alright, as already mentioned, I am in no position to buy an upgrade. And even if I did, there’s absolutely no chance for me using it. Unfortunately, I’m stuck to version 6.8.

But be it as it may, can you at least provide me the version specific manual of Aspose.Pdf version 6.8?

Thanks,
Casey

@CaseyMcCall

We are afraid that we cannot do so as we are not maintaining this version. Please note that a lot of things have been changed in the API since 6.8 version. All Classes and Methods in it have been discontinued and replaced with DOM API approach. We cannot change anything in such older versions and also, the support is provided on the basis of the latest version of the API.