Skip to content

920560 Added PDF Attachments with Interactive Launch Buttons #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adding-PDF-Attachments-with-Interactive-Launch-Buttons", "Adding-PDF-Attachments-with-Interactive-Launch-Buttons\Adding-PDF-Attachments-with-Interactive-Launch-Buttons.csproj", "{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Handle_attachments_in_PDF</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
</ItemGroup>

</Project>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using System.Reflection.Metadata;

//Create FileStream object to read the input PDF file
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read))
{
// Load the existing PDF file
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream);

// Get the first page of the PDF
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;

// Create a PDF attachment
PdfAttachment attachment = new PdfAttachment("Attachment.pdf", File.ReadAllBytes(Path.GetFullPath(@"Data/Attachment.pdf")));
attachment.Description = "PDF Attachment";

// Create attachments section if it doesn't exist
if (loadedDocument.Attachments == null)
loadedDocument.CreateAttachment();

// Add the attachment to the document
loadedDocument.Attachments.Add(attachment);

// Create a button field on the page
PdfButtonField buttonField = new PdfButtonField(lpage, "Button");
buttonField.Bounds = new RectangleF(100, 100, 100, 20);
buttonField.BorderColor = new PdfColor(Color.Black);
buttonField.BackColor = new PdfColor(Color.LightGray);
buttonField.Text = "Click Me";
buttonField.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

// Add JavaScript action to open the attachment
buttonField.Actions.MouseUp = new PdfJavaScriptAction("this.exportDataObject({ cName: \"Attachment.pdf\", nLaunch: 2 });");

// Create a form if it doesn't exist
if (loadedDocument.Form == null)
loadedDocument.CreateForm();

// Add the button field to the form
loadedDocument.Form.Fields.Add(buttonField);

// Set default appearance for form fields
loadedDocument.Form.SetDefaultAppearance(false);

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
loadedDocument.Save(outputFileStream);
}

//Close the document.
loadedDocument.Close(true);
}