A navigation button in a PDF document can redirect users from one page to another. For instance, when a navigation button is clicked, we can jump to the next/last page or return to the previous/first page. This article will introduce how to create such a navigation button in PDF using Spire.PDF.

Step 1: Initialize an instance of PdfDocument class and load a PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sales Report.pdf");

Step 2: Enable creation of form fields in the document.

doc.AllowCreateForm = true;

Step 3: Get the last page.

PdfPageBase page = doc.Pages[doc.Pages.Count-1];

Step 4: Create a button field and specify the button name, tooltip and border style.

PdfButtonField button = new PdfButtonField(page, "Return to First Page");
button.Bounds = new RectangleF(page.ActualSize.Width/2-50, 400, 100, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ToolTip = "First Page";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 7f,PdfFontStyle.Bold);

Step 5: Create a PdfNamedAction that goes to the named destination (previous, next, first or last page) and add the action to the button field.

PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
button.Actions.GotFocus = namedAction;

Step 6: Add the button to the document.

doc.Form.Fields.Add(button);
doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF);

Output:

How to Create Navigation Buttons in PDF in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace NavigationButtons
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sales Report.pdf");
            doc.AllowCreateForm = true;
            PdfPageBase page = doc.Pages[doc.Pages.Count - 1];

            PdfButtonField button = new PdfButtonField(page, "Return to First Page");
            button.Bounds = new RectangleF(page.ActualSize.Width / 2 - 50, 400, 100, 20);
            button.BorderColor = new PdfRGBColor(Color.AliceBlue);
            button.BorderStyle = PdfBorderStyle.Solid;
            button.ToolTip = "First Page";
            button.Font = new PdfFont(PdfFontFamily.Helvetica, 7f, PdfFontStyle.Bold);

            PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
            button.Actions.GotFocus = namedAction;

            doc.Form.Fields.Add(button);
            doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Fields
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace NavigationButtons
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\Sales Report.pdf")
			doc.AllowCreateForm = True
			Dim page As PdfPageBase = doc.Pages(doc.Pages.Count - 1)

			Dim button As New PdfButtonField(page, "Return to First Page")
			button.Bounds = New RectangleF(page.ActualSize.Width / 2 - 50, 400, 100, 20)
			button.BorderColor = New PdfRGBColor(Color.AliceBlue)
			button.BorderStyle = PdfBorderStyle.Solid
			button.ToolTip = "First Page"
			button.Font = New PdfFont(PdfFontFamily.Helvetica, 7F, PdfFontStyle.Bold)

			Dim namedAction As New PdfNamedAction(PdfActionDestination.FirstPage)
			button.Actions.GotFocus = namedAction

			doc.Form.Fields.Add(button)
			doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace
Published in Action

Adding print button to PDF document is possible in Spire.PDF, when the button is clicked, the print dialog will be opened. In this tutorial, I'm going to show you how to add a print button to an existing pdf document using Spire.PDF.

To accomplish the task, first you need to create an instance of the PdfButtonField class. This class also allows you to define the appearance of the button. For instance, you can set text, color of text, background color, border color etc. of the button. Second you need to add the print action to the button by calling the AddPrintAction() method. At last, add the button to the document and save the document to file. The following code example explains the same.

Code snippets:

Step 1: Load the PDF document and enable form creation.

PdfDocument doc = new PdfDocument("Input.pdf");
doc.AllowCreateForm = true;

Step 2: Get the first page.

PdfPageBase page = doc.Pages[0];

Step 3: Create a PdfButtonField instance and set properties for the button.

PdfButtonField button = new PdfButtonField(page, "Print");
button.Bounds = new RectangleF(280, 600, 50, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ForeColor = new PdfRGBColor(Color.White);
button.BackColor = new PdfRGBColor(Color.Blue);
button.ToolTip = "Print";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);

Step 4: Add print action to the button.

button.AddPrintAction();

Step 5: Add the button to the document.

doc.Form.Fields.Add(button);

Step 6: Save the document.

doc.SaveToFile("Output.pdf");

The resultant document looks as follows:

Add a Print Button to a PDF Document in C#

Full code:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;

namespace Add_print_button
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PDF document
            PdfDocument doc = new PdfDocument("Input.pdf");
            //Enable form creation
            doc.AllowCreateForm = true;
            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Create a PdfButtonField instance
            PdfButtonField button = new PdfButtonField(page, "Print");
            //Set button properties
            button.Bounds = new RectangleF(280, 600, 50, 20);
            button.BorderColor = new PdfRGBColor(Color.AliceBlue);
            button.BorderStyle = PdfBorderStyle.Solid;
            button.ForeColor = new PdfRGBColor(Color.White);
            button.BackColor = new PdfRGBColor(Color.Blue);
            button.ToolTip = "Print";
            button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);

            //Add print action to the button
            button.AddPrintAction();

            //Add the button to document
            doc.Form.Fields.Add(button);            

            //Save the document
            doc.SaveToFile("Output.pdf");
        }
    }
}
Published in Action
Wednesday, 29 March 2017 08:03

Reset the values of PDF form fields in C#

Spire.PDF allows us to reset the values of PDF form fields using the PdfResetAction. The following code example demonstrates how we can use Spire.PDF to implement this function.

Code snippet:

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

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

Step 2: Create a text box field, set properties for the text box field and add it to the document.

PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name");
textBoxField.BorderColor = new PdfRGBColor(Color.AliceBlue);
textBoxField.BorderStyle = PdfBorderStyle.Solid;
textBoxField.Bounds = new RectangleF(50, 50, 100, 20);
textBoxField.Text = "Shawn Smith";
document.Form.Fields.Add(textBoxField);

Step 3: Create a button field, set properties for the button field and add it to the document.

PdfButtonField button = new PdfButtonField(page, "Reset");
button.Bounds = new RectangleF(80, 100, 50, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ToolTip = "Reset";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
document.Form.Fields.Add(button);

Step 4: Create a reset action for the button field using PdfResetAction class.

PdfResetAction resetAction = new PdfResetAction();
button.Actions.GotFocus = resetAction;

Step 5: Save and close the PDF document.

document.SaveToFile("Output.pdf");
document.Close();

Screenshot before and after resetting value:

Reset the values of PDF form fields in C#

Full code:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;

namespace Reset_form_fields_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document
            PdfDocument document = new PdfDocument();

            //Add a new page
            PdfPageBase page = document.Pages.Add();

            //Create a text box field.
            PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name");

            //Set properties for the text box field.
            textBoxField.BorderColor = new PdfRGBColor(Color.AliceBlue);
            textBoxField.BorderStyle = PdfBorderStyle.Solid;
            textBoxField.Bounds = new RectangleF(50, 50, 100, 20);
            textBoxField.Text = "Shawn Smith";

            //Add the text box field to the document
            document.Form.Fields.Add(textBoxField);

            //Create a button field.
            PdfButtonField button = new PdfButtonField(page, "Reset");
             
            //Set properties for the button field
            button.Bounds = new RectangleF(80, 100, 50, 20);
            button.BorderColor = new PdfRGBColor(Color.AliceBlue);
            button.BorderStyle = PdfBorderStyle.Solid;
            button.ToolTip = "Reset";
            button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);

            //Add the button field to the document
            document.Form.Fields.Add(button);

            //Create a reset action for the button field
            PdfResetAction resetAction = new PdfResetAction();
            button.Actions.GotFocus = resetAction;

            //Save and close the PDF document
            document.SaveToFile("Output.pdf");
            document.Close();
        }
    }
}
Published in Action

A Sound action is used to embed and play a sound file in PDF document. In Spire.PDF, we can create a sound action by using the PdfSoundAction class. Attributes like sound, volume and repeat can be specified for the sound action.

Refer below code example:

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

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

Step 2: Create a sound action and set its attributes.

PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
soundAction.Sound.Bits = 15;
soundAction.Sound.Channels = PdfSoundChannels.Stereo;
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
soundAction.Volume = 0.8f;
soundAction.Repeat = true;

Step 3: Set the sound action to be executed when the PDF document is opened.

document.AfterOpenAction = soundAction;

Step 4: Save and close the PDF document.

document.SaveToFile("Output.pdf");
document.Close();

Full code:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;

namespace PDF_Sound_Action
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PDF document
            PdfDocument document = new PdfDocument();
            //Add a page
            PdfPageBase page = document.Pages.Add();

            //Create a sound action
            PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
            soundAction.Sound.Bits = 15;
            soundAction.Sound.Channels = PdfSoundChannels.Stereo;
            soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
            soundAction.Volume = 0.8f;
            soundAction.Repeat = true;

            // Set the sound action to be executed when the PDF document is opened
            document.AfterOpenAction = soundAction;

            //Save and close the PDF document
            document.SaveToFile("Output.pdf");
            document.Close();
        }
    }
}
Published in Action

PDF actions can refer to external files by using either absolute or relative file paths. While using the relative path in action, the file can be moved into a different location. In Spire.PDF, we can use the PdfLaunchAction to link to files with relative paths.

Code snippet:

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

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

Step 2: Create a PDF launch action that refers to an external file with relative path.

PdfLaunchAction launchAction = new PdfLaunchAction(@"..\..\test.txt", PdfFilePathType.Relative);

Step 3: Create a PDF action annotation with the launch action. Add the annotation to the page.

string text = "Click here to open file with relative path";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
RectangleF rect = new RectangleF(50, 50, 230, 15);
page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rect);
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
(page as PdfNewPage).Annotations.Add(annotation);     

Step 4: Save the document.

document.SaveToFile("RelativeFilePath.pdf");

Screenshot:

Link to files with relative paths using PdfLaunchAction

Full code:

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

namespace Link_to_files_with_relative_paths
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PDF document and add a page to it
            PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            //Create a PDF Launch Action       
            PdfLaunchAction launchAction = new PdfLaunchAction(@"..\..\test.txt", PdfFilePathType.Relative);
            
            //Create a PDF Action Annotation with the PDF Launch Action
            string text = "Click here to open file with relative path";
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
            RectangleF rect = new RectangleF(50, 50, 230, 15);
            page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rect);
            PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction); 
             //Add the PDF Action Annotation to page
            (page as PdfNewPage).Annotations.Add(annotation);           

            //Save and close the document
            document.SaveToFile("RelativeFilePath.pdf");
            document.Close();
        }
    }
}
Published in Action
Thursday, 29 December 2016 07:59

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");            
        }
    }		 
}
Published in Action

Generally, when we open a PDF document from a PDF viewer, it displays the first page instead of others. For some reasons, we may want to skip the first few pages and start on another page. This article will introduce how to specify a particular page when viewing a PDF document in C# and VB.NET.

Code Snippet:

Step 1: Initialize an instance of PdfDocument class and a load a sample PDF file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 2: Create a PdfGoToAction which will redirect to a destination (page 2 in this case) in the current document.

PdfDestination destination = new PdfDestination(doc.Pages[1]);
PdfGoToAction action = new PdfGoToAction(destination);

Step 3: Sets the action to execute after the document is opened.

doc.AfterOpenAction = action;

Step 4: Save the file.

doc.SaveToFile("GoTo2Page.pdf",FileFormat.PDF);

Output:

The document opens at the second page.

How to open a PDF document at a specific page in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;

namespace OpenPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            PdfDestination destination = new PdfDestination(doc.Pages[1]);
            PdfGoToAction action = new PdfGoToAction(destination);
            action.Destination.Zoom = 0.8F;
            doc.AfterOpenAction = action;

            doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.General

Namespace OpenPDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

			Dim destination As New PdfDestination(doc.Pages(1))
			Dim action As New PdfGoToAction(destination)
			action.Destination.Zoom = 0.8F
			doc.AfterOpenAction = action

			doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace
Published in Action
Wednesday, 08 January 2014 09:11

Add Action Chain to PDF in C#

PDF supports actions. And Spire.PDF, a very powerful .NET component enables developers to add action chain to PDF file. Action chain means an action get executed automatically after another one.

In this article, a solution is introduced to add action chain to PDF file.

Step 1: Set the action that gets performed right after PDF document is opened

[C#]
String script
                = "app.alert({"
                + "    cMsg: \"I'll lead; you must follow me.\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
PdfJavaScriptAction action1 = new PdfJavaScriptAction(script);
document.AfterOpenAction = action1;

Spire.PDF provides you a class called PdfJavaScriptAction that executes JavaScript code. Create a PdfJavaScriptAction instance “action1” using JavaScript code. And set the property AfterOpenAction of “document” to action1.

Step 2: Set the action that gets performed after “action1” using the property NextAction

[C#]
script
                = "app.alert({"
                + "    cMsg: \"The firt page!\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
PdfJavaScriptAction action2 = new PdfJavaScriptAction(script);
action1.NextAction = action2;

Step 3: Set the action that gets performed after “action2”

[C#]
PdfDestination dest = new PdfDestination(pagetwo);
dest.Zoom = 1;
PdfGoToAction action3 = new PdfGoToAction(dest);
action2.NextAction = action3;

PdfDestination can mark a specified page or location in PDF. Create a PdfDestination instance “dest” using “pagetwo”. Then create a PdfGoToAction instance “action3” using “dest”.

Step 4: Set the action that gets performed after “action3”

[C#]
script
                = "app.alert({"
                + "    cMsg: \"Oh sorry, it's the last page. I'm missing!\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);
action3.NextAction = action4;

Step 5: Save the file

[C#]
document.SaveToFile("result.pdf");

Full code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;


namespace AddActionChain
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();

            PdfPageBase pageone = document.Pages.Add();
            pageone.Canvas.DrawString("This is Page One.",
                                   new PdfFont(PdfFontFamily.Helvetica, 20f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);
            PdfPageBase pagetwo = document.Pages.Add();
            pagetwo.Canvas.DrawString("This is Page Two.",
                                   new PdfFont(PdfFontFamily.Helvetica, 20f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);

            String script
                = "app.alert({"
                + "    cMsg: \"I'll lead; you must follow me.\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action1 = new PdfJavaScriptAction(script);
            document.AfterOpenAction = action1;

            script
                = "app.alert({"
                + "    cMsg: \"The first page!\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action2 = new PdfJavaScriptAction(script);
            action1.NextAction = action2;

            PdfDestination dest = new PdfDestination(pagetwo);
            dest.Zoom = 1;
            PdfGoToAction action3 = new PdfGoToAction(dest);
            action2.NextAction = action3;

            script
                = "app.alert({"
                + "    cMsg: \"Oh sorry, it's the last page. I'm missing!\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);
            action3.NextAction = action4;

            document.SaveToFile("result.pdf");

            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

Screenshot:

add action chain in pdf

add action chain in pdf

Published in Action