Pdf Links |
Select.Pdf offers support to create clickable links in a pdf document. A link can be used to navigate to another location within the same pdf document, open a web url or a local file. Here are the objects that can be used to create links:
PdfInternalLinkElement - can be used to create a link that will help you navigate to another location within the current pdf document.
PdfExternalLinkElement - can be used to create a link that will open an external web url.
PdfExternalFileElement - can be used to create a link that will open a local file from the disk.
All these elements use a common property (LinkRectangle) to define the clickable area of the link. This area can be placed over a piece of text, image or any other pdf element. If that specific element was previously added to the pdf page, the rendering result object can be used to obtain its exact rendering rectangle specified by the PdfPageLastRectangle property of the PdfRenderingResult object.
PdfInternalLinkElement defines the LinkDestination property to specify the location that will be displayed when the link is clicked.
PdfExternalLinkElement defines the LinkURL property to specify the external url that will be opened when the link is clicked.
PdfExternalFileElement defines the FileName property to specify the external file that will be opened when the link is clicked.
This sample code shows how to create a new PDF document using Select.Pdf, how to add 2 pages to the document and several internal and external links:
// create a new pdf document PdfDocument doc = new PdfDocument(); // add a new page to the document PdfPage page1 = doc.AddPage(); // add a second page to the document PdfPage page2 = doc.AddPage(); // define a rendering result object PdfRenderingResult result; // create a new pdf font PdfFont font = doc.AddFont(PdfStandardFont.Helvetica); font.Size = 14; font.IsUnderline = true; // create a new text element and add it to the first page PdfTextElement text1 = new PdfTextElement(0, 0, "First page (no link on this text)", font); page1.Add(text1); // create a new text element and add it to the second page PdfTextElement text2 = new PdfTextElement(0, 0, "Second page (no link on this text)", font); page2.Add(text2); // create external link in 2 steps // 1 - create the link text PdfTextElement linkText1 = new PdfTextElement(0, 50, "External link (click to go to selectpdf.com)", font); result = page1.Add(linkText1); // 2 - add the link using the text rendering rectangle PdfExternalLinkElement extLink1 = new PdfExternalLinkElement( result.PdfPageLastRectangle, "http://selectpdf.com"); page1.Add(extLink1); // create internal link in 2 steps // 1 - create the link text PdfTextElement linkText2 = new PdfTextElement(0, 100, "Internal link (click to go to the second page)", font); result = page1.Add(linkText2); // 2 - add the link using the text rendering rectangle PdfInternalLinkElement intLink1 = new PdfInternalLinkElement( result.PdfPageLastRectangle, new PdfDestination(page2)); page1.Add(intLink1); // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close();