Crear enlaces en archivos PDF con C#
El siguiente fragmento de código también funciona con la biblioteca Aspose.PDF.Drawing .
Crear enlaces
Al agregar un enlace a una aplicación en un documento, es posible vincular a aplicaciones desde un documento. Esto es útil cuando desea que los lectores realicen una acción determinada en un punto específico de un tutorial, por ejemplo, o para crear un documento rico en funciones. Para crear un enlace de aplicación:
Crear un objeto Document .
Obtenga la Page a la que desea agregar el enlace.
Cree un objeto LinkAnnotation utilizando los objetos Page y Rectangle .
Establezca los atributos del enlace utilizando el objeto LinkAnnotation .
Además, establezca la propiedad Action del objeto LaunchAction .
Al crear el objeto LaunchAction , especifique la aplicación que desea iniciar.
Agregue el enlace a la propiedad Annotations del objeto Page.
Finalmente, guarde el PDF actualizado utilizando el método Save del objeto Document.
El siguiente fragmento de código muestra cómo crear un enlace a una aplicación en un archivo PDF.
.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
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF 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 PDF 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
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF 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 PDF document
document . Save ( dataDir + "CreateApplicationLink_out.pdf" );
}
Crear enlace de documento PDF en un archivo PDF
Aspose.PDF for .NET le permite agregar un enlace a un archivo PDF externo para que pueda vincular varios documentos juntos. Para crear un enlace de documento PDF:
Primero, cree un objeto Document .
Luego, obtenga la Page particular a la que desea agregar el enlace.
Cree un objeto LinkAnnotation utilizando los objetos Page y Rectangle .
Establezca los atributos del enlace utilizando el objeto LinkAnnotation .
Establezca la propiedad Action en el objeto GoToRemoteAction .
Al crear el objeto GoToRemoteAction, especifique el archivo PDF que debe iniciarse, así como el número de página que debe abrirse.
Agregue el enlace a la colección Annotations del objeto Page.
Guarde el PDF actualizado utilizando el método Save del objeto Document.
El siguiente fragmento de código muestra cómo crear un enlace de documento PDF en un archivo PDF.
.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
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF 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 PDF 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
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF 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 PDF document
document . Save ( dataDir + "CreateDocumentLink_out.pdf" );
}