Skip to content

Commit e5ad6d2

Browse files
authored
Merge pull request #2450 from syncfusion-content/1016957-ugh
1016957: Added this topic: Removing page-level action for PDF
2 parents 7e8d2c0 + 2cfe21b commit e5ad6d2

1 file changed

Lines changed: 138 additions & 25 deletions

File tree

Document-Processing/PDF/PDF-Library/NET/Working-with-Pages.md

Lines changed: 138 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ Refer to the following code example to define custom behavior for PDF page‑lev
15991599

16001600
{% tabs %}
16011601

1602-
{% highlight c# tabtitle="C# [Cross-platform]" %}
1602+
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/PDF-Examples/master/Pages/Add-Page-Level-Actions-in-PDF/.NET/Add-Page-Level-Actions-in-PDF/Program.cs" %}
16031603

16041604
using Syncfusion.Pdf;
16051605
using Syncfusion.Pdf.Interactive;
@@ -1610,9 +1610,9 @@ using (PdfDocument document = new PdfDocument())
16101610
// Add a page to the document.
16111611
PdfPage page1 = document.Pages.Add();
16121612
// Create and add new JavaScript action to execute when the first page opens
1613-
Page1.Actions.OnOpen = new PdfJavaScriptAction("app.alert(\"Welcome! This page has just been opened.\");");
1613+
page1.Actions.OnOpen = new PdfJavaScriptAction("app.alert(\"Welcome! This page has just been opened.\");");
16141614
// Create and add new URI action to execute when the first page closes
1615-
Page1.Actions.OnClose = new PdfUriAction("http://www.google.com");
1615+
page1.Actions.OnClose = new PdfUriAction("http://www.google.com");
16161616
// Add second page to the document.
16171617
PdfPage page2 = document.Pages.Add();
16181618
// Create a sound action
@@ -1622,13 +1622,16 @@ using (PdfDocument document = new PdfDocument())
16221622
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
16231623
soundAction.Volume = 0.9f;
16241624
// Set the sound action to execute when the second page opens
1625-
Page2.Actions.OnOpen = soundAction;
1625+
page2.Actions.OnOpen = soundAction;
16261626
// Create and add new Launch action to execute when the second page closes
1627-
Page2.Actions.OnClose = new PdfLaunchAction("logo.png");
1628-
// Removing the close action on first page
1629-
Page1.Actions.OnClose = null;
1630-
// Removing both open and close actions on second page
1631-
Page2.Actions.Clear(true);
1627+
page2.Actions.OnClose = new PdfLaunchAction("logo.png");
1628+
// Add third page to the document
1629+
PdfPage page3 = document.Pages.Add();
1630+
// Create and add new JavaScript action to execute when the third page opens
1631+
PdfAction jsAction = new PdfJavaScriptAction("app.alert(\"Welcome! Third page has just been opened.\");");
1632+
jsAction.Next = new PdfJavaScriptAction("app.alert(\"This is the second action.\");");
1633+
jsAction.Next.Next = new PdfJavaScriptAction("app.alert(\"This is the third action.\");");
1634+
page3.Actions.OnOpen = jsAction;
16321635
//Save the document
16331636
document.Save("Output.pdf");
16341637
}
@@ -1644,11 +1647,11 @@ using Syncfusion.Pdf.Interactive;
16441647
using (PdfDocument document = new PdfDocument())
16451648
{
16461649
// Add a page to the document.
1647-
PdfPage page1 = document.Pages.Add();
1650+
PdfPage page1 = document.Pages.Add();
16481651
// Create and add new JavaScript action to execute when the first page opens
1649-
Page1.Actions.OnOpen = new PdfJavaScriptAction("app.alert(\"Welcome! This page has just been opened.\");");
1652+
page1.Actions.OnOpen = new PdfJavaScriptAction("app.alert(\"Welcome! This page has just been opened.\");");
16501653
// Create and add new URI action to execute when the first page closes
1651-
Page1.Actions.OnClose = new PdfUriAction("http://www.google.com");
1654+
page1.Actions.OnClose = new PdfUriAction("http://www.google.com");
16521655
// Add second page to the document.
16531656
PdfPage page2 = document.Pages.Add();
16541657
// Create a sound action
@@ -1658,13 +1661,16 @@ using (PdfDocument document = new PdfDocument())
16581661
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
16591662
soundAction.Volume = 0.9f;
16601663
// Set the sound action to execute when the second page opens
1661-
Page2.Actions.OnOpen = soundAction;
1664+
page2.Actions.OnOpen = soundAction;
16621665
// Create and add new Launch action to execute when the second page closes
1663-
Page2.Actions.OnClose = new PdfLaunchAction("logo.png");
1664-
// Removing the close action on first page
1665-
Page1.Actions.OnClose = null;
1666-
// Removing both open and close actions on second page
1667-
Page2.Actions.Clear(true);
1666+
page2.Actions.OnClose = new PdfLaunchAction("logo.png");
1667+
// Add third page to the document
1668+
PdfPage page3 = document.Pages.Add();
1669+
// Create and add new JavaScript action to execute when the third page opens
1670+
PdfAction jsAction = new PdfJavaScriptAction("app.alert(\"Welcome! Third page has just been opened.\");");
1671+
jsAction.Next = new PdfJavaScriptAction("app.alert(\"This is the second action.\");");
1672+
jsAction.Next.Next = new PdfJavaScriptAction("app.alert(\"This is the third action.\");");
1673+
page3.Actions.OnOpen = jsAction;
16681674
//Save the document
16691675
document.Save("Output.pdf");
16701676
}
@@ -1678,15 +1684,18 @@ Imports Syncfusion.Pdf.Interactive
16781684

16791685
' Create a new PDF document.
16801686
Using document As New PdfDocument()
1687+
16811688
' Add a page to the document.
16821689
Dim page1 As PdfPage = document.Pages.Add()
16831690
' Create and add new JavaScript action to execute when the first page opens
1684-
page1.Actions.OnOpen = New PdfJavaScriptAction("app.alert(""Welcome! This page has just been opened."");")
1691+
page1.Actions.OnOpen = New PdfJavaScriptAction(
1692+
"app.alert(""Welcome! This page has just been opened."");"
1693+
)
16851694
' Create and add new URI action to execute when the first page closes
16861695
page1.Actions.OnClose = New PdfUriAction("http://www.google.com")
16871696
' Add second page to the document.
16881697
Dim page2 As PdfPage = document.Pages.Add()
1689-
' Create a sound action
1698+
' Create a sound action
16901699
Dim soundAction As New PdfSoundAction("Startup.wav")
16911700
soundAction.Sound.Bits = 16
16921701
soundAction.Sound.Channels = PdfSoundChannels.Stereo
@@ -1696,14 +1705,118 @@ Using document As New PdfDocument()
16961705
page2.Actions.OnOpen = soundAction
16971706
' Create and add new Launch action to execute when the second page closes
16981707
page2.Actions.OnClose = New PdfLaunchAction("logo.png")
1699-
' Removing the close action on first page
1700-
page1.Actions.OnClose = Nothing
1701-
' Removing both open and close actions on second page
1702-
page2.Actions.Clear(True)
1708+
' Add third page to the document
1709+
Dim page3 As PdfPage = document.Pages.Add()
1710+
' Create and add new JavaScript action to execute when the third page opens
1711+
Dim jsAction As PdfAction = New PdfJavaScriptAction(
1712+
"app.alert(""Welcome! Third page has just been opened."");"
1713+
)
1714+
jsAction.Next = New PdfJavaScriptAction(
1715+
"app.alert(""This is the second action."");"
1716+
)
1717+
jsAction.Next.Next = New PdfJavaScriptAction(
1718+
"app.alert(""This is the third action."");"
1719+
)
1720+
page3.Actions.OnOpen = jsAction
17031721
' Save the document
17041722
document.Save("Output.pdf")
17051723
End Using
17061724

17071725
{% endhighlight %}
17081726

1709-
{% endtabs %}
1727+
{% endtabs %}
1728+
1729+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Pages/Add-Page-Level-Actions-in-PDF/.NET).
1730+
1731+
## Removing page-level actions from PDF
1732+
1733+
Removes page‑level actions from PDF documents by disabling actions triggered on page open and page close events, ensuring that pages no longer execute automatic behaviors while preserving existing annotation actions where required.
1734+
1735+
Refer to the following code example for removing PDF page‑level actions.
1736+
1737+
{% tabs %}
1738+
1739+
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/PDF-Examples/master/Pages/Removing-page-level-actions-from-PDF/.NET/Removing-page-level-actions-from-PDF/Program.cs" %}
1740+
1741+
using Syncfusion.Pdf;
1742+
using Syncfusion.Pdf.Interactive;
1743+
1744+
// Load the existing PDF document
1745+
using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
1746+
{
1747+
// Iterate through all pages in the document
1748+
foreach (PdfLoadedPage page in document.Pages)
1749+
{
1750+
// Remove any JavaScript or actions that execute
1751+
// when the page is opened
1752+
page.Actions.OnOpen = null;
1753+
1754+
// Remove any JavaScript or actions that execute
1755+
// when the page is closed
1756+
page.Actions.OnClose = null;
1757+
}
1758+
// Save the modified PDF document
1759+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
1760+
// Close the document
1761+
document.Close(true);
1762+
}
1763+
1764+
{% endhighlight %}
1765+
1766+
{% highlight c# tabtitle="C# [Windows-specific]" %}
1767+
1768+
using Syncfusion.Pdf;
1769+
using Syncfusion.Pdf.Interactive;
1770+
1771+
// Load the existing PDF document
1772+
using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
1773+
{
1774+
// Iterate through all pages in the document
1775+
foreach (PdfLoadedPage page in document.Pages)
1776+
{
1777+
// Remove any JavaScript or actions that execute
1778+
// when the page is opened
1779+
page.Actions.OnOpen = null;
1780+
// Remove any JavaScript or actions that execute
1781+
// when the page is closed
1782+
page.Actions.OnClose = null;
1783+
}
1784+
// Save the modified PDF document
1785+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
1786+
// Close the document
1787+
document.Close(true);
1788+
}
1789+
1790+
{% endhighlight %}
1791+
1792+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
1793+
1794+
Imports Syncfusion.Pdf
1795+
Imports Syncfusion.Pdf.Interactive
1796+
1797+
' Load the existing PDF document
1798+
Using document As New PdfLoadedDocument(
1799+
Path.GetFullPath("Data/Input.pdf"))
1800+
' Iterate through all pages in the document
1801+
For Each page As PdfLoadedPage In document.Pages
1802+
' Remove any JavaScript or actions that execute
1803+
' when the page is opened
1804+
page.Actions.OnOpen = Nothing
1805+
' Remove any JavaScript or actions that execute
1806+
' when the page is closed
1807+
page.Actions.OnClose = Nothing
1808+
1809+
Next
1810+
' Save the modified PDF document
1811+
document.Save(Path.GetFullPath("Output/Output.pdf"))
1812+
1813+
' Close the document
1814+
document.Close(True)
1815+
1816+
End Using
1817+
1818+
{% endhighlight %}
1819+
1820+
{% endtabs %}
1821+
1822+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Pages/Removing-page-level-actions-from-PDF/.NET).

0 commit comments

Comments
 (0)