Create a GoToE Action to an Embedded PDF File

The GoToE (or embedded go-to) action is similar to a remote go-to action but allows jumping to a PDF file that is embedded in another PDF file. With Spire.PDF, it’s possible to create a GoToE action to direct from the current PDF file to the embedded PDF file. This article demonstrates how to accomplish this goal programmatically with C#.

At first, it's necessary to introduce you the parameters of public PdfEmbeddedGoToAction(string fileName, PdfDestination dest, bool newWindow);

Parameters:

  • fileName - the name of the target file.
  • dest - the destination inside the target file.
  • newWindow - if true open the file in a new window, if false the current file is replaced by the new file.

Now Follow below Detail steps:

Step 1: Create a PDF file and add a page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 2: Attach a PDF file to the newly created file.

PdfAttachment attachment = new PdfAttachment("New Zealand.pdf");
pdf.Attachments.Add(attachment);

Step 3: Draw text on the page.

string text = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
float width = 490f;
float height = font.Height * 2.2f;
RectangleF rect = new RectangleF(0, 100, width, height);
page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);

Step 4: Create an embedded go-to action (/GoToE) which allows jumping to the attached PDF file and open it in a new window at the 2nd page and 200% zoom factor.

PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f);
PdfEmbeddedGoToAction action = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);

Step 5: Create an action annotation with the embedded go-to action and then add it to the page.

PdfActionAnnotation annotation = new PdfActionAnnotation(rect, action);
(page as PdfNewPage).Annotations.Add(annotation);

Step 6: Save the document.

pdf.SaveToFile("result.pdf");

Effective Screenshot:

Create a GoToE Action to an Embedded PDF File

Full code:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Attachments;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;

namespace Create_A_GoToE_Action
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();
            PdfAttachment attachment = new PdfAttachment("New Zealand.pdf");
            pdf.Attachments.Add(attachment);
            string text = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
            float width = 490f;
            float height = font.Height * 2.2f;
            RectangleF rect = new RectangleF(0, 100, width, height);
            page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);
            PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f);
            PdfEmbeddedGoToAction action = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);
            PdfActionAnnotation annotation = new PdfActionAnnotation(rect, action);
            (page as PdfNewPage).Annotations.Add(annotation);
            pdf.SaveToFile("result.pdf");            
        }
    }		 
}