Add a link to PDF with an external destination
Add a link to PDF with an external destination
This code sample helps adding an external link to your PDF.
If your PDF refers to a webpage, or to another external document it is important to be able to add an external links to your PDF. This article shows how you can add a link with an external destination; this can be any document or an URL.
using ( FileStream fileIn = new FileStream("SellingYourHome.pdf", FileMode.Open, FileAccess.Read ) )
{
Document document = new Document( fileIn );
RemoteDestination destination = new RemoteDestination();
destination.Path = "PackingLightBrochure.pdf";
destination.PageIndex = 2; // third page
destination.PageDisplay = PageDisplay.FitEntire;
destination.WindowBehavior = WindowBehavior.NewWindow;
GoToAction action = new GoToAction( destination );
Link link = new Link( 100, 100, 300, 200 );
link.BorderStyle = BorderStyle.Solid;
link.BorderWidth = 1;
link.BorderColor = RgbColor.Red;
link.MouseUpActions.Add( action );
document.Pages[0].Links.Add( link );
using ( FileStream fileOut = new FileStream("remotedestination.pdf", FileMode.Create, FileAccess.Write ) )
{
document.Write( fileOut );
}
}
Using fileIn As New FileStream("SellingYourHome.pdf", FileMode.Open, FileAccess.Read)
Dim document As New Document(fileIn)
Dim destination As New RemoteDestination()
destination.Path = "PackingLightBrochure.pdf"
destination.PageIndex = 2
' third page
destination.PageDisplay = PageDisplay.FitEntire
destination.WindowBehavior = WindowBehavior.NewWindow
Dim action As New GoToAction(destination)
Dim link As New Link(100, 100, 300, 200)
link.BorderStyle = BorderStyle.Solid
link.BorderWidth = 1
link.BorderColor = RgbColor.Red
link.MouseUpActions.Add(action)
document.Pages(0).Links.Add(link)
Using fileOut As New FileStream("remotedestination.pdf", FileMode.Create, FileAccess.Write)
document.Write(fileOut)
End Using
End Using