Skip to content

Commit 6680c49

Browse files
committed
920560 Added PDF Attachments with Interactive Launch Buttons
1 parent f4dc5bf commit 6680c49

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2BAE2C1D-2436-4DE8-A255-F0CD3CE3BFE4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Handle_attachments_in_PDF</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

Attachment/Adding-PDF-Attachments-with-Interactive-Launch-Buttons/.NET/Adding-PDF-Attachments-with-Interactive-Launch-Buttons/Output/gitkeep.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Syncfusion.Pdf.Graphics;
2+
using Syncfusion.Pdf.Interactive;
3+
using Syncfusion.Pdf.Parsing;
4+
using Syncfusion.Pdf;
5+
using Syncfusion.Drawing;
6+
using System.Reflection.Metadata;
7+
8+
//Create FileStream object to read the input PDF file
9+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read))
10+
{
11+
// Load the existing PDF file
12+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream);
13+
14+
// Get the first page of the PDF
15+
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;
16+
17+
// Create a PDF attachment
18+
PdfAttachment attachment = new PdfAttachment("Attachment.pdf", File.ReadAllBytes(Path.GetFullPath(@"Data/Attachment.pdf")));
19+
attachment.Description = "PDF Attachment";
20+
21+
// Create attachments section if it doesn't exist
22+
if (loadedDocument.Attachments == null)
23+
loadedDocument.CreateAttachment();
24+
25+
// Add the attachment to the document
26+
loadedDocument.Attachments.Add(attachment);
27+
28+
// Create a button field on the page
29+
PdfButtonField buttonField = new PdfButtonField(lpage, "Button");
30+
buttonField.Bounds = new RectangleF(100, 100, 100, 20);
31+
buttonField.BorderColor = new PdfColor(Color.Black);
32+
buttonField.BackColor = new PdfColor(Color.LightGray);
33+
buttonField.Text = "Click Me";
34+
buttonField.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
35+
36+
// Add JavaScript action to open the attachment
37+
buttonField.Actions.MouseUp = new PdfJavaScriptAction("this.exportDataObject({ cName: \"Attachment.pdf\", nLaunch: 2 });");
38+
39+
// Create a form if it doesn't exist
40+
if (loadedDocument.Form == null)
41+
loadedDocument.CreateForm();
42+
43+
// Add the button field to the form
44+
loadedDocument.Form.Fields.Add(buttonField);
45+
46+
// Set default appearance for form fields
47+
loadedDocument.Form.SetDefaultAppearance(false);
48+
49+
//Create file stream.
50+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
51+
{
52+
//Save the PDF document to file stream.
53+
loadedDocument.Save(outputFileStream);
54+
}
55+
56+
//Close the document.
57+
loadedDocument.Close(true);
58+
}

0 commit comments

Comments
 (0)