Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
The following code snippet also work with Aspose.PDF.Drawing library.
A Link Annotation represents a hyperlink, a destination elsewhere, and a document. According to the PDF Standard, link annotation can be used in three cases: open page view, open file, and open web page.
Several additional steps were performed to create the annotation. We used 2 TextFragmentAbsorbers to find fragments to demo. The first one is for the link annotation text, and the second one indicates some places in the document.
// 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");
}
}
To create the annotation we have followed certain steps:
LinkAnnotation
and pass the page object and the rectangle of the text fragment which is corresponded with annotation.Action
as GoToAction
and pass XYZExplicitDestination
as desired destination. We created XYZExplicitDestination
based on page, left and top coordinates and zoom.When processing various documents, a situation arises when you are typing and do not know where the annotation will point. In this case you can use a special (named) destination and code will be look like here:
var destinationName = "Link2";
var linkAnnotation2 = new LinkAnnotation(page, linkFragments[2].Rectangle)
{
Action = new GoToAction(document, destinationName)
};
In another place you can create a Named Destination.
document.NamedDestinations.Add(destinationName,
new XYZExplicitDestination(
sampleTextFragments[2].Page,
sampleTextFragments[2].Rectangle.LLX,
sampleTextFragments[2].Rectangle.URX, 1));
The same approach is used when creating an annotation to open a file, as in the examples above.
var linkAnnotation3 = new LinkAnnotation(page, linkFragments[3].Rectangle)
{
Action = new GoToRemoteAction("another.pdf", 2)
};
The difference is that we will use GoToRemoteAction
instead GoToAction
. The constructor of GoToRemoteAction gets two parameters: file name and page number.
You can also use another form and pass file name and some destination. Obviously, you need to create such a destination before using it.
To open web page just set Action
with GoToURIAction
object.
You can pass a hyperlink as a constructor parameter or any other kind of URI. For example, you can use callto
to implement action with calling phone number.
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
};
You can customize Link Annotation using borders. In the example below we will create a blue dashed border with 3pt of width.
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
};
Yet another example shows how to simulate browser style and use Underline for links.
var linkAnnotation5 = new LinkAnnotation(page, linkFragments[5].Rectangle)
{
Color = Color.Blue
};
linkAnnotation5.Border = new Border(linkAnnotation5)
{
Style = BorderStyle.Underline,
Width = 3
};
Please try using the following code snippet to Get LinkAnnotation from PDF document.
// 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);
}
}
}
The following code snippet shows how to Delete Link Annotation from PDF file. For this we need to find and and remove all link annotations on the 1st page. After this we will save document with removed annotation.
// 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");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.