Back to Devexpress

PdfLinkAnnotationFacade Class

officefileapi-devexpress-dot-pdf-dbd9f924.md

latest13.8 KB
Original Source

PdfLinkAnnotationFacade Class

Contains members used to manage link annotations without access to their inner structure.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Core.dll

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public class PdfLinkAnnotationFacade :
    PdfAnnotationFacade
vb
Public Class PdfLinkAnnotationFacade
    Inherits PdfAnnotationFacade

The following members return PdfLinkAnnotationFacade objects:

Remarks

You can create a link annotation associated with a URI string or a destination (a reference to a page with specific view parameters).

Create a Link to a URI

The following code snippet creates an annotation linked to a URI string:

View Example

csharp
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    // Load a document
    processor.LoadDocument("..\\..\\Document.pdf");

    // Access the first page properties
    PdfPageFacade page = processor.DocumentFacade.Pages[0];

    // Find the target phrase in the document
    string linkText = "Evaluation";
    PdfTextSearchResults linkSearchResults = processor.FindText(linkText);

    if (linkSearchResults.Status == PdfTextSearchStatus.Found)
    {
        PdfRectangle linkRectangle = linkSearchResults.Rectangles[0].BoundingRectangle;
        string linkUri = "https://community.devexpress.com/blogs/";

        // Add a link annotation to the found text
        PdfLinkAnnotationFacade uriAnnotation = page.AddLinkAnnotation(linkRectangle, linkUri);
        uriAnnotation.Name = "link1";
        uriAnnotation.HighlightMode = PdfAnnotationHighlightingMode.Push;
    }
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
Using processor As New PdfDocumentProcessor()
  ' Load a document
  processor.LoadDocument("..\..\Document.pdf")

  ' Access the first page properties
  Dim page As PdfPageFacade = processor.DocumentFacade.Pages(0)

  ' Find the target phrase in the document
  Dim linkText As String = "Evaluation"
  Dim linkSearchResults As PdfTextSearchResults = processor.FindText(linkText)

  If linkSearchResults.Status = PdfTextSearchStatus.Found Then
    Dim linkRectangle As PdfRectangle = linkSearchResults.Rectangles(0).BoundingRectangle
    Dim linkUri As String = "https://community.devexpress.com/blogs/"

    ' Add a link annotation to the found text
    Dim uriAnnotation As PdfLinkAnnotationFacade = page.AddLinkAnnotation(linkRectangle, linkUri)
    uriAnnotation.Name = "link1"
    uriAnnotation.HighlightMode = PdfAnnotationHighlightingMode.Push
  End If
  processor.SaveDocument("..\..\Result.pdf")
End Using

Create a Link to a Destination

A destination includes the following view parameters:

  • The displayed document page
  • View parameters: a view mode or a specific point/region on the page to which to navigate
  • The magnification (zoom factor)

Call one of the following methods to create a destination:

View ParametersMethods
Fit the page’s bounding box to the document window both horizontally and vertically.PdfPageFacade.CreateFitBBoxDestination
Fit the page’s bounding box to the document window horizontally.PdfPageFacade.CreateFitBBoxHorizontallyDestination
Fit the page’s bounding box to the document window vertically.PdfPageFacade.CreateFitBBoxVerticallyDestination
Fit the entire page to the document window both horizontally and vertically ( Zoom to Page Level view).PdfPageFacade.CreateFitDestination
Fit the entire page to the document window horizontally.PdfPageFacade.CreateFitHorizontallyDestination
Fit the entire page to the document window vertically.PdfPageFacade.CreateFitVerticallyDestination
Display the specified page area in the document window.PdfPageFacade.CreateFitRectangleDestination
Position the specified page coordinate at the top left document window corner, and specify the zoom factor.PdfPageFacade.CreateXYZDestination

The following code snippet creates a link annotation with a destination that displays the third page with the Zoom to Page Level view:

View Example

csharp
using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
    // Load a document
    pdfDocumentProcessor.LoadDocument("Demo.pdf");

    // Access third page properties
    PdfPageFacade pageFacade = pdfDocumentProcessor.DocumentFacade.Pages[2];

    // Create a Fit destination that refers to the third page
    PdfFitDestination destination = pageFacade.CreateFitDestination();

    // Find a specific phrase
    string linkText = "JBIG2 images";
    PdfTextSearchResults linkSearchResults = pdfDocumentProcessor.FindText(linkText);

    // If the phrase is found, obtain its bounding rectangle
    if (linkSearchResults.Status == PdfTextSearchStatus.Found)
    {
        PdfRectangle linkRectangle = linkSearchResults.Rectangles[0].BoundingRectangle;

        // Access first page properties
        PdfPageFacade linkPageFacade =
           pdfDocumentProcessor.DocumentFacade.Pages[linkSearchResults.PageNumber -1];

        // Create a link annotation associated with the bounding rectangle
        // and destination
        PdfLinkAnnotationFacade linkAnnotation =
            linkPageFacade.AddLinkAnnotation(linkRectangle, destination);
        linkAnnotation.HighlightMode = PdfAnnotationHighlightingMode.Push;
    }
    // Save the result
    pdfDocumentProcessor.SaveDocument("out.pdf");
}
vb
Using pdfDocumentProcessor As New PdfDocumentProcessor()
  ' Load a document
  pdfDocumentProcessor.LoadDocument("Demo.pdf")

  ' Access third page properties
  Dim pageFacade As PdfPageFacade = pdfDocumentProcessor.DocumentFacade.Pages(2)

  ' Create a Fit destination that refers to the third page
    Dim destination As PdfFitDestination = pageFacade.CreateFitDestination()

  ' Find a specific phrase
  Dim linkText As String = "JBIG2 images"
  Dim linkSearchResults As PdfTextSearchResults = pdfDocumentProcessor.FindText(linkText)

  ' If the phrase is found, obtain its bounding rectangle
  If linkSearchResults.Status = PdfTextSearchStatus.Found Then
    Dim linkRectangle As PdfRectangle = linkSearchResults.Rectangles(0).BoundingRectangle

    ' Access first page properties
    Dim linkPageFacade As PdfPageFacade = pdfDocumentProcessor.DocumentFacade.Pages(linkSearchResults.PageNumber -1)

    'Create a link annotation associated with the bounding rectangle
    ' and destination
    Dim linkAnnotation As PdfLinkAnnotationFacade =
       linkPageFacade.AddLinkAnnotation(linkRectangle, destination)
    linkAnnotation.HighlightMode = PdfAnnotationHighlightingMode.Push
  End If
  ' Save the result
  pdfDocumentProcessor.SaveDocument("out.pdf")
End Using

The PdfPageFacade.Annotations property returns all page annotation properties. You can filter link annotation properties, cast them to the PdfLinkAnnotationFacade class, and use class properties to modify annotation parameters.

The code sample below changes parameters of a link annotation with the link1 name:

csharp
using DevExpress.Pdf;
using System.Linq;

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    //Load a document
    processor.LoadDocument("..\\..\\Demo.pdf");

    // Access the first page properties
    PdfPageFacade page = processor.DocumentFacade.Pages[0];

    // Retrieve all link annotations
    var linkAnnotations = page.Annotations.Where(annotation => annotation.Type == PdfAnnotationType.Link);
    foreach (PdfLinkAnnotationFacade linkAnnotation in linkAnnotations)
    {
        // Change specific annotation's parameters
        if (linkAnnotation.Name == "link1")
        {
            linkAnnotation.BorderStyle = PdfBorderStyle.Dot;
            linkAnnotation.Color = new PdfRGBColor(0.36, 0.54, 0.66);
            linkAnnotation.BorderWidth = 2;
            linkAnnotation.HighlightMode = PdfAnnotationHighlightingMode.Toggle;
        }
    }
    processor.SaveDocument("..\\..\\Result.pdf");
}
vb
Imports DevExpress.Pdf
Imports System.Linq

Using processor As New PdfDocumentProcessor()
  ' Load a document
  processor.LoadDocument("..\..\Demo.pdf")

  ' Access the first page properties
  Dim facade As PdfDocumentFacade = processor.DocumentFacade
  Dim page As PdfPageFacade = facade.Pages(0)

  ' Retrieve all link annotations
  Dim linkAnnotations = page.Annotations.Where(Function(annotation) annotation.Type = PdfAnnotationType.Link)
  For Each linkAnnotation As PdfLinkAnnotationFacade In linkAnnotations
    ' Change specific annotation's parameters
    If linkAnnotation.Name = "link1" Then
      linkAnnotation.BorderStyle = PdfBorderStyle.Dot
      linkAnnotation.Color = New PdfRGBColor(0.36, 0.54, 0.66)
      linkAnnotation.BorderWidth = 2
      linkAnnotation.HighlightMode = PdfAnnotationHighlightingMode.Toggle
    End If
  Next linkAnnotation
  processor.SaveDocument("..\..\Result.pdf")
End Using

Call one of the following methods to flatten an annotation:

MethodDescription
PdfDocumentFacade.FlattenAnnotationsFlattens document annotations.
PdfPageFacade.FlattenAnnotationsFlattens annotations of a specific page.
PdfAnnotationFacade.Flatten()Flattens a specific annotation.

The code sample below flattens all link annotations on the first page:

csharp
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    // Load a document
    processor.LoadDocument("..\\..\\Document.pdf");

    // Access the first page properties
    PdfPageFacade page = processor.DocumentFacade.Pages[0];

    // Flatten all link annotations
    page.FlattenAnnotations(PdfAnnotationType.Link);
}
vb
Using processor As New PdfDocumentProcessor()

  'Load a document:
  processor.LoadDocument("..\..\Document.pdf")

  ' Access the first page properties
  Dim page As PdfPageFacade = processor.DocumentFacade.Pages(0)

  ' Flatten all link annotations
  page.FlattenAnnotations(PdfAnnotationType.Link)
End Using

Call the PdfAnnotationFacade.Remove() method to remove an annotation. The code sample below removes all link annotations from the first page:

csharp
using DevExpress.Pdf;
using System.Linq;

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{

  // Load a document
  processor.LoadDocument("..\\..\\Document.pdf");

  // Access the first page properties
  PdfPageFacade page = processor.DocumentFacade.Pages[0];

  // Retrieve all link annotations
  var links = page.Annotations.Where
    (annotation => annotation.Type == PdfAnnotationType.Link).ToList();

  // Remove all links
  foreach (PdfLinkAnnotationFacade link in links)
  {
      link.Remove();
  }
}
vb
Imports DevExpress.Pdf
Imports System.Linq

Using processor As New PdfDocumentProcessor()
processor

  'Load a document:
  processor.LoadDocument("..\..\Document.pdf")

  ' Access the first page properties
  Dim page As PdfPageFacade = processor.DocumentFacade.Pages(0)

  ' Retrieve all link annotations
  Dim links = page.Annotations.Where
    (Function(annotation) annotation.Type = PdfAnnotationType.Link).ToList()

  ' Remove all links
  For Each link As PdfLinkAnnotationFacade In links
    link.Remove()
  Next link
End Using

Inheritance

Object PdfAnnotationFacade PdfLinkAnnotationFacade

See Also

PdfLinkAnnotationFacade Members

DevExpress.Pdf Namespace