Hi,
Hi There,
Thanks for contacting support.
The links in the PDF document are represented as Annotation. You may use AnnotationSelector class to get all LinkAnnotations in a PDF document. Please check the following code snippet to add links with different actions in the document and access them after.
// Create new document
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Save("AddExtractLink.pdf");
// Load created document
pdfDocument = new Document("AddExtractLink.pdf");
Page page = pdfDocument.Pages[1];
// Add links
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(10, 800, 30, 820));
link.Action = new LaunchAction("test.txt");
page.Annotations.Add(link);
link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(40, 800, 60, 820));
link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
link.Action = new GoToRemoteAction("AddExtractLink.pdf", 1);
page.Annotations.Add(link);
// Save documents with link
pdfDocument.Save("AddExtractLink.pdf");
// Load document to get links
pdfDocument = new Document("AddExtractLink.pdf");
page = pdfDocument.Pages[1];
// Get all links
AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
System.Collections.IList list = selector.Selected;
foreach (var annot in list)
{
LinkAnnotation linkannot = (LinkAnnotation)annot;
Console.WriteLine("link type is : " + linkannot.Action.GetType().Name);
}
I am also attaching the output file generated by the above code. I hope this will help. In case of any further assistance please feel free to contact us.
Best Regards,
Hi,
Hi There,
Thanks for your inquiry and sharing code snippet. There is another type of Action which is GoToAction. With this type of action you can set link target to a particular page of a document within a document. Please check the following code snippet to add and access the GoToAction of LinkAnnotation.
// Create new document
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Pages.Add();
pdfDocument.Save(dataDir + "AddExtractLink.pdf");
// Load document
pdfDocument = new Document("AddExtractLink.pdf");
Page page = pdfDocument.Pages[1];
Page destpage = pdfDocument.Pages[2];
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(70, 800, 90, 820));
link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Yellow);
link.Action = new GoToAction(destpage);
page.Annotations.Add(link);
// Save documents with link
pdfDocument.Save("AddExtractLink.pdf");
// Load document to get links
pdfDocument = new Document("AddExtractLink.pdf");
page = pdfDocument.Pages[1];
// Get all links
AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
System.Collections.IList list = selector.Selected;
foreach (var annot in list)
{
LinkAnnotation linkannot = (LinkAnnotation)annot;
if (linkannot.Action is GoToAction)
{
GoToAction action = (GoToAction)linkannot.Action;
ExplicitDestination destination = (ExplicitDestination)action.Destination;
Console.WriteLine("Page Number is : " + destination.PageNumber);
}
}
I have also attached an output file generated by above code. In case if you need further assistance please feel free to contact us.
Best Regards,