Use of GoToAction

Hi Team!

Can you give an example on how to use GoToAction(Document doc, string name).
My need is to navigate user on clicking on the text to it’s existence on the document in other pages. So what name should need to pass in GoToAction?
And also how can I add GoToAction on TextFragment?

Thanks in Advance.

@Jaibir

Here is one possible example:

using Aspose.Pdf.Annotations;
using Aspose.Pdf;
using Aspose.Pdf.Text;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main()
        {
            // Load the PDF file
            Document doc = new(@"Sample-Document.pdf");

            Page page1 = doc.Pages[1];
            TextFragmentAbsorber textFragmentAbsorber1 = new("Some text 1");
            textFragmentAbsorber1.Visit(page1);
            var tf1 = textFragmentAbsorber1.TextFragments.First();
            var linkAnnotation = new LinkAnnotation(page1, tf1.Rectangle);


            Page page2 = doc.Pages[2];
            TextFragmentAbsorber textFragmentAbsorber2 = new("Some text 2");
            textFragmentAbsorber2.Visit(page2);
            var tf2 = textFragmentAbsorber2.TextFragments.First();

            // Create the variable to set the zoom factor of the target page
            double zoom = 2.5;
            // Create GoToAction instance
            GoToAction action = new(page2)
            {
                // Go to 2 page
                Destination = new XYZExplicitDestination(page2, tf2.Rectangle.LLX, tf2.Rectangle.URY, zoom)
            };

            linkAnnotation.Action = action;
            page1.Annotations.Add(linkAnnotation);
            // Save updated document
            doc.Save("goto2page_out.pdf");
        }
    }
}

You can also find some helpful information in the sections Working with Actions in PDF|Aspose.PDF for .NET and Create Links in PDF file with C#|Aspose.PDF for .NET

1 Like

Hi @andriy.andrukhovski ,

Thank you for your quick support. Can you please also tell, how to accomplish same you did in above code using GoToAction(Document doc, string name).
Because We are merging two pdfs into one. First pdf contains content with keywords and second pdf contains keyword’s stats like how many keywords found on which page number.
While making stats pdf we need to add GoToAction by name and then in content pdf also we should use same to make those keywords focused when we click on stats keyword’s page number.
For reference attaching image.

image.png (2.2 KB)

Thanks in advance

Please try this approach:

string destinationName = "KeywordPage1";
var licensePath = @"Aspose.PDF.NET.lic";
new Aspose.Pdf.License().SetLicense(licensePath);

// Simulation of the document with keywords
Document document1 = new(@"Sample-Document-01.pdf");
Page pageDoc1 = document1.Pages[1];
TextFragmentAbsorber textFragmentAbsorber1 = new("Keyword");
textFragmentAbsorber1.Visit(pageDoc1);
var tf1 = textFragmentAbsorber1.TextFragments.First();
double zoom = 1;
document1.NamedDestinations.Add(destinationName,
    new XYZExplicitDestination(pageDoc1, tf1.Rectangle.LLX, tf1.Rectangle.URY, zoom));
document1.Save(@"Sample-Test-01.pdf");

// Simulation of the document with stat
var document2 = new Document();
var pageDoc2 = document2.Pages.Add();
pageDoc2.Paragraphs.Add(new TextFragment("Keyword on Page 1"));
document2.Save(@"Sample-Test-02.pdf");

TextFragmentAbsorber textFragmentAbsorber2 = new("Page 1");
textFragmentAbsorber2.Visit(pageDoc2);
var tf2 = textFragmentAbsorber2.TextFragments.First();

var linkAnnotation = new LinkAnnotation(pageDoc2, tf2.Rectangle)
{
    Action = new GoToAction(pageDoc2)
    {
        Destination = new NamedDestination(document2, destinationName)
    }
};
pageDoc2.Annotations.Add(linkAnnotation);
document2.Save(@"Sample-Test-02.pdf");

// Merging
document1.Pages.Add(document2.Pages);
document1.Save(@"Sample-Test-03.pdf");

The Sample-Test-03.pdf document will contain pages from the 1st document with an attached page with stats from the second document.

1 Like

Hi @andriy.andrukhovski
Thank you for your quick response. It is very helpful.
As per our requirement, page numbers with keywords page should appear first and then the complete document. So to do same when I switched merging code. The navigation stopped.
I only did following change:

// Merging
document2.Pages.Add(document1.Pages);
document2.Save(@"Sample-Test-03.pdf");

Can you please take a look and provide solution.
Thank you in advance.

Hi @andriy.andrukhovski
Thank you for your support. Figure out solution by using insert in the beginning of the document.
I used following line of code:

// Merging
document1.Pages.Insert(1, document2.Pages);
document1.Save(savePdfMerge);

Thank you for sharing. Yes, you are right because we recalculate references of Named Destination in the case of insert.

1 Like