Create Links in PDF file with C#
The following code snippet also work with Aspose.PDF.Drawing library.
Create Links
By adding a link to an application into a document, it is possible to link to applications from a document. This is useful when you want readers to take a certain action at a specific point in a tutorial, for example, or to create a feature-rich document. To create an application link:
Create a Document object.
Get the Page you want to add link to.
Create a LinkAnnotation object using the Page and Rectangle objects.
Set the link attributes using the LinkAnnotation object.
Also, set the to LaunchAction object’s Action property.
When creating the LaunchAction object, specify the application you want to launch.
Add the link to the Page object’s Annotations property.
Finally, save the updated PDF using the Document object’s Save method.
The following code snippet shows how to create a link to an application in a PDF file.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open document
using ( var document = new Aspose . Pdf . Document ( dataDir + "CreateApplicationLink.pdf" ))
{
// Create link
var page = document . Pages [ 1 ];
var link = new Aspose . Pdf . Annotations . LinkAnnotation ( page , new Aspose . Pdf . Rectangle ( 100 , 100 , 300 , 300 ));
link . Color = Aspose . Pdf . Color . FromRgb ( System . Drawing . Color . Green );
link . Action = new Aspose . Pdf . Annotations . LaunchAction ( document , dataDir + "CreateApplicationLink.pdf" );
page . Annotations . Add ( link );
// Save updated document
document . Save ( dataDir + "CreateApplicationLink_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open document
using var document = new Aspose . Pdf . Document ( dataDir + "CreateApplicationLink.pdf" );
// Create link
var page = document . Pages [ 1 ];
var link = new Aspose . Pdf . Annotations . LinkAnnotation ( page , new Aspose . Pdf . Rectangle ( 100 , 100 , 300 , 300 ));
link . Color = Aspose . Pdf . Color . FromRgb ( System . Drawing . Color . Green );
link . Action = new Aspose . Pdf . Annotations . LaunchAction ( document , dataDir + "CreateApplicationLink.pdf" );
page . Annotations . Add ( link );
// Save updated document
document . Save ( dataDir + "CreateApplicationLink_out.pdf" );
}
Create PDF Document Link in a PDF File
Aspose.PDF for .NET allows you to add a link to an external PDF file so that you can link several documents together. To create a PDF document link:
First, create a Document object.
Then, get the particular Page you want to add the link to.
Create a LinkAnnotation object using the Page and Rectangle objects.
Set the link attributes using the LinkAnnotation object.
Set the Action property to the GoToRemoteAction object.
While creating the GoToRemoteAction object, specify the PDF file that should launch, as well as the page number it should open on.
Add the link to the Page object’s Annotations collection.
Save the updated PDF using the Document object’s Save method.
The following code snippet shows how to create PDF document link in a PDF file.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open document
using ( var document = new Aspose . Pdf . Document ( dataDir + "CreateDocumentLink.pdf" ))
{
// Create link
var page = document . Pages [ 1 ];
var link = new Aspose . Pdf . Annotations . LinkAnnotation ( page , new Aspose . Pdf . Rectangle ( 100 , 100 , 300 , 300 ));
link . Color = Aspose . Pdf . Color . FromRgb ( System . Drawing . Color . Green );
link . Action = new Aspose . Pdf . Annotations . GoToRemoteAction ( dataDir + "RemoveOpenAction.pdf" , 1 );
page . Annotations . Add ( link );
// Save updated document
document . Save ( dataDir + "CreateDocumentLink_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open document
using var document = new Aspose . Pdf . Document ( dataDir + "CreateDocumentLink.pdf" );
// Create link
var page = document . Pages [ 1 ];
var link = new Aspose . Pdf . Annotations . LinkAnnotation ( page , new Aspose . Pdf . Rectangle ( 100 , 100 , 300 , 300 ));
link . Color = Aspose . Pdf . Color . FromRgb ( System . Drawing . Color . Green );
link . Action = new Aspose . Pdf . Annotations . GoToRemoteAction ( dataDir + "RemoveOpenAction.pdf" , 1 );
page . Annotations . Add ( link );
// Save updated document
document . Save ( dataDir + "CreateDocumentLink_out.pdf" );
}