Usando Anotaciones de Enlace en PDF

Agregando Anotación de Enlace en un archivo PDF existente

El siguiente fragmento de código también funciona con la biblioteca Aspose.PDF.Drawing.

Una Anotación de Enlace representa un hipervínculo, un destino en otro lugar y un documento. Según el Estándar PDF, la anotación de enlace se puede utilizar en tres casos: vista de página abierta, abrir archivo y abrir página web.

Usando Anotación de Enlace para abrir vista de página

Se realizaron varios pasos adicionales para crear la anotación. Usamos 2 TextFragmentAbsorbers para encontrar fragmentos para la demostración. El primero es para el texto de la anotación de enlace, y el segundo indica algunos lugares en el documento.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddLinkAnnotation()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "Link Annotation Demo.pdf"))
    {
        // Get first page
        var page = document.Pages[1];

        // Define regular expressions for text fragments
        var regEx1 = new Regex(@"Link Annotation Demo \d");
        var regEx2 = new Regex(@"Sample text \d");

        // Create TextFragmentAbsorber for the first regular expression
        var textFragmentAbsorber1 = new Aspose.Pdf.Text.TextFragmentAbsorber(regEx1);
        textFragmentAbsorber1.Visit(document);

        // Create TextFragmentAbsorber for the second regular expression
        var textFragmentAbsorber2 = new Aspose.Pdf.Text.TextFragmentAbsorber(regEx2);
        textFragmentAbsorber2.Visit(document);

        // Get the text fragments for both absorbers
        var linkFragments = textFragmentAbsorber1.TextFragments;
        var sampleTextFragments = textFragmentAbsorber2.TextFragments;

        // Create a LinkAnnotation
        var linkAnnotation1 = new Aspose.Pdf.Annotations.LinkAnnotation(page, linkFragments[1].Rectangle)
        {
            Action = new Aspose.Pdf.Annotations.GoToAction(
                new Aspose.Pdf.Annotations.XYZExplicitDestination(
                    sampleTextFragments[1].Page,
                    sampleTextFragments[1].Rectangle.LLX,
                    sampleTextFragments[1].Rectangle.URX, 1.5))
        };

        // Add the link annotation to the page
        page.Annotations.Add(linkAnnotation1);

        // Save PDF document
        document.Save(dataDir + "AddLinkAnnotation_out.pdf");
    }
}

Para crear la anotación hemos seguido ciertos pasos:

  1. Crear LinkAnnotation y pasar el objeto de página y el rectángulo del fragmento de texto que corresponde con la anotación.
  2. Establecer Action como GoToAction y pasar XYZExplicitDestination como destino deseado. Creamos XYZExplicitDestination basado en la página, las coordenadas izquierda y superior y el zoom.
  3. Agregar la anotación a la colección de anotaciones de la página.
  4. Guardar el documento.

Usando Anotación de Enlace con destino nombrado

Al procesar varios documentos, surge una situación en la que estás escribiendo y no sabes a dónde apuntará la anotación. En este caso, puedes usar un destino especial (nombrado) y el código se verá así:

var destinationName = "Link2";
var linkAnnotation2 = new LinkAnnotation(page, linkFragments[2].Rectangle)
{
    Action = new GoToAction(document, destinationName)
};

En otro lugar puedes crear un Destino Nombrado.

document.NamedDestinations.Add(destinationName,
    new XYZExplicitDestination(
        sampleTextFragments[2].Page,
        sampleTextFragments[2].Rectangle.LLX,
        sampleTextFragments[2].Rectangle.URX, 1));

Usando Anotación de Enlace para abrir archivo

El mismo enfoque se utiliza al crear una anotación para abrir un archivo, como en los ejemplos anteriores.

var linkAnnotation3 = new LinkAnnotation(page, linkFragments[3].Rectangle)
{
    Action = new GoToRemoteAction("another.pdf", 2)
};

La diferencia es que usaremos GoToRemoteAction en lugar de GoToAction. El constructor de GoToRemoteAction recibe dos parámetros: nombre del archivo y número de página. También puedes usar otra forma y pasar el nombre del archivo y algún destino. Obviamente, necesitas crear tal destino antes de usarlo.

Usando Anotación de Enlace para abrir página web

Para abrir una página web, simplemente establece Action con el objeto GoToURIAction. Puedes pasar un hipervínculo como parámetro del constructor o cualquier otro tipo de URI. Por ejemplo, puedes usar callto para implementar una acción con un número de teléfono.

var linkAnnotation4 = new LinkAnnotation(page, linkFragments[4].Rectangle)
{
    Action = new GoToURIAction("https://products.aspose.com/pdf/net"),
    // Create Link Annotation and set the action to call a phone number
    //Action = new GoToURIAction("callto:678-555-0103")
    Color = Color.Blue
};

Agregando Anotación de Enlace decorada

Puedes personalizar la Anotación de Enlace usando bordes. En el ejemplo a continuación, crearemos un borde azul punteado de 3pt de ancho.

var linkAnnotation4 = new LinkAnnotation(page, linkFragments[4].Rectangle)
{
    Action = new GoToURIAction("https://products.aspose.com/pdf/net"),    
    Color = Color.Blue
};

linkAnnotation4.Border = new Border(linkAnnotation4)
{
    Style = BorderStyle.Dashed,
    Dash = new Dash(5, 5),
    Width = 3
};

Otro ejemplo muestra cómo simular el estilo del navegador y usar Subrayado para los enlaces.

var linkAnnotation5 = new LinkAnnotation(page, linkFragments[5].Rectangle)
{
    Color = Color.Blue
};
linkAnnotation5.Border = new Border(linkAnnotation5)
{
    Style = BorderStyle.Underline,
    Width = 3
};

Obtener Anotaciones de Enlace

Por favor, intenta usar el siguiente fragmento de código para Obtener Anotaciones de Enlace del documento PDF.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetLinkAnnotations()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "SimpleResume_mod.pdf"))
    {
        // Get all Link annotations from the first page
        var linkAnnotations = document.Pages[1].Annotations.Where(a => a.AnnotationType == Aspose.Pdf.Annotations.AnnotationType.Link);

        // Iterate through each Link annotation
        foreach (Aspose.Pdf.Annotations.Annotation annot in linkAnnotations)
        {
            // Print the URL of each Link Annotation
            Console.WriteLine("URI: " + ((annot as Aspose.Pdf.Annotations.LinkAnnotation).Action as Aspose.Pdf.Annotations.GoToURIAction).URI);

            // Create a TextAbsorber to extract text within the annotation's rectangle
            var absorber = new Aspose.Pdf.Text.TextAbsorber();
            absorber.TextSearchOptions.LimitToPageBounds = true;
            absorber.TextSearchOptions.Rectangle = annot.Rect;

            // Accept the absorber for the first page
            document.Pages[1].Accept(absorber);

            // Extract and print the text associated with the hyperlink
            string extractedText = absorber.Text;
            Console.WriteLine(extractedText);
        }
    }
}

Eliminar Anotaciones de Enlace

El siguiente fragmento de código muestra cómo Eliminar una Anotación de Enlace de un archivo PDF. Para esto, necesitamos encontrar y eliminar todas las anotaciones de enlace en la primera página. Después de esto, guardaremos el documento con la anotación eliminada.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DeleteLinkAnnotations()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "SimpleResume_mod.pdf"))
    {
        // Find and delete all link annotations on the 1st page
        var linkAnnotations = document.Pages[1].Annotations.Where(a => a.AnnotationType == Aspose.Pdf.Annotations.AnnotationType.Link);

        foreach (var la in linkAnnotations)
        {
            document.Pages[1].Annotations.Delete(la);
        }

        // Save PDF document
        document.Save(dataDir + "DeleteLinkAnnotations_out.pdf");
    }
}