This samples shows how to add a link to a document page using PdfPage.AddLinkToPage(..) method.
AddLinkToPage method accepts bounds of the link area and the target page to open when action area is activated and returns an instance of PdfActionArea class.
You can also create a link to a document page using GoToAction class (see "GoTo Action" sample in "Actions" group).
using System.Diagnostics; using System.Drawing; namespace BitMiracle.Docotic.Pdf.Samples { public static class LinkToPage { public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. PdfDocument pdf = new PdfDocument(); pdf.AddPage(); pdf.AddPage(); PdfPage firstPage = pdf.Pages[0]; PdfCanvas canvas = firstPage.Canvas; RectangleF rectForLinkToSecondPage = new RectangleF(10, 50, 100, 60); canvas.DrawRectangle(rectForLinkToSecondPage, PdfDrawMode.Stroke); canvas.DrawString("Go to 2nd page", rectForLinkToSecondPage, PdfTextAlign.Center, PdfVerticalAlign.Center); firstPage.AddLinkToPage(rectForLinkToSecondPage, 1); RectangleF rectForLinkToThirdPage = new RectangleF(150, 50, 100, 60); canvas.DrawRectangle(rectForLinkToThirdPage, PdfDrawMode.Stroke); canvas.DrawString("Go to 3rd page", rectForLinkToThirdPage, PdfTextAlign.Center, PdfVerticalAlign.Center); firstPage.AddLinkToPage(rectForLinkToThirdPage, pdf.Pages[2]); string pathToFile = "LinkToPage.pdf"; pdf.Save(pathToFile); pdf.Dispose(); Process.Start(pathToFile); } } }
Imports System.Diagnostics Imports System.Drawing Imports BitMiracle.Docotic.Pdf Namespace BitMiracle.Docotic.Pdf.Samples Public NotInheritable Class LinkToPage Public Shared Sub Main() ' NOTE: ' When used in trial mode, the library imposes some restrictions. ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx ' for more information. Dim pdf As New PdfDocument() pdf.AddPage() pdf.AddPage() Dim firstPage As PdfPage = pdf.Pages(0) Dim canvas As PdfCanvas = firstPage.Canvas Dim rectForLinkToSecondPage As New RectangleF(10, 50, 100, 60) canvas.DrawRectangle(rectForLinkToSecondPage, PdfDrawMode.Stroke) canvas.DrawString("Go to 2nd page", rectForLinkToSecondPage, PdfTextAlign.Center, PdfVerticalAlign.Center) firstPage.AddLinkToPage(rectForLinkToSecondPage, 1) Dim rectForLinkToThirdPage As New RectangleF(150, 50, 100, 60) canvas.DrawRectangle(rectForLinkToThirdPage, PdfDrawMode.Stroke) canvas.DrawString("Go to 3rd page", rectForLinkToThirdPage, PdfTextAlign.Center, PdfVerticalAlign.Center) firstPage.AddLinkToPage(rectForLinkToThirdPage, pdf.Pages(2)) Dim pathToFile As String = "LinkToPage.pdf" pdf.Save(pathToFile) pdf.Dispose() Process.Start(pathToFile) End Sub End Class End Namespace
