Skip to content

Commit a20e23b

Browse files
Merge pull request #1 from SyncfusionExamples/828251
828251 Added sample project and Readme file in attachment blog.
2 parents 1a66d96 + 505e18a commit a20e23b

File tree

17 files changed

+311
-2
lines changed

17 files changed

+311
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33516.290
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddAttachmentPDF", "AddAttachmentPDF\AddAttachmentPDF.csproj", "{B11BEB39-0721-4E8E-B093-1451A0353C7D}"
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+
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FFB6BC3F-9061-4C56-AF62-94F281D0A865}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.1.41" />
12+
</ItemGroup>
13+
14+
</Project>
16.7 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Syncfusion.Pdf.Interactive;
2+
using Syncfusion.Pdf.Parsing;
3+
4+
//Load the PDF document
5+
FileStream docStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.Read);
6+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
7+
8+
//Creates an attachment
9+
Stream fileStream = new FileStream("../../../Input.txt", FileMode.Open, FileAccess.Read);
10+
PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream);
11+
attachment.ModificationDate = DateTime.Now;
12+
attachment.Description = "Input.txt";
13+
attachment.MimeType = "application/txt";
14+
15+
if (loadedDocument.Attachments == null)
16+
loadedDocument.CreateAttachment();
17+
//Add the attachment to the document
18+
loadedDocument.Attachments.Add(attachment);
19+
20+
//Save the document into stream
21+
MemoryStream stream = new MemoryStream();
22+
loadedDocument.Save(stream);
23+
File.WriteAllBytes("../../../Output.pdf",stream.ToArray());
24+
loadedDocument.Close(true);
25+
stream.Close();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33516.290
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractAttachmentPDF", "ExtractAttachmentPDF\ExtractAttachmentPDF.csproj", "{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}"
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+
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {CD1A86FC-9315-4940-83CE-F5D00C67FB57}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.2.3" />
12+
</ItemGroup>
13+
14+
</Project>
17.5 KB
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//Load the PDF document
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Interactive;
4+
using Syncfusion.Pdf.Parsing;
5+
using System.IO;
6+
7+
//Load the PDF document
8+
FileStream docStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.Read);
9+
//Load an existing PDF document
10+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
11+
12+
//Iterates the attachments
13+
foreach (PdfAttachment attachment in loadedDocument.Attachments)
14+
{
15+
//Extracts the attachment and saves it to the disk
16+
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
17+
s.Write(attachment.Data, 0, attachment.Data.Length);
18+
s.Dispose();
19+
}
20+
21+
//Close the PDF document.
22+
loadedDocument.Close(true);

README.md

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,128 @@
1-
# create-pdf-attachments-csharp
2-
This repository contains examples to create attachments in PDF using C#
1+
# Add and Remove Attachments in PDF using C#
2+
3+
The [Syncfusion .NET PDF Library](https://www.syncfusion.com/document-processing/pdf-framework/net/pdf-library) provides support for file attachments in PDF documents, allowing developers to add attachments to a PDF document from within their application code. The library also provides APIs for extracting attachments from PDF documents, as well as for modifying and deleting existing attachments.
4+
5+
This article will cover the process to add, extract and remove file attachments from a PDF document using the Syncfusion .NET PDF library. The topics related to this will be discussed in the following sections of this post:
6+
7+
Name | Description
8+
--- | ---
9+
[Add an attachment](https://github.com/SyncfusionExamples/create-pdf-attachments-csharp/tree/master/AddAttachmentPDF) | It refers to the process of embedding a separate file, such as a text document, image, or audio file, into a PDF document.
10+
[Extracte and save an attachment](https://github.com/SyncfusionExamples/create-pdf-attachments-csharp/tree/master/ExtractAttachmentPDF) | It refers to the process of removing or separating a file or document that is embedded or attached within a PDF document and saving it as a separate file on your computer or device.
11+
[Remove an attachment](https://github.com/SyncfusionExamples/create-pdf-attachments-csharp/tree/master/RemoveAttachmentPDF) | It useful in cases where the attachment is no longer needed, or when the PDF file needs to be shared without the attached file.
12+
13+
## Add an attachment in a PDF using C#
14+
15+
Adding a file attachment in a PDF document refers to the process of embedding a separate file, such as a text document, image, or audio file, into a PDF document.
16+
17+
The following code example shows how to add an attachment to a PDF file using C#.
18+
19+
```csharp
20+
//Load the PDF document
21+
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
22+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
23+
24+
//Creates an attachment
25+
Stream fileStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
26+
PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream);
27+
attachment.ModificationDate = DateTime.Now;
28+
attachment.Description = "Input.txt";
29+
attachment.MimeType = "application/txt";
30+
31+
if (loadedDocument.Attachments == null)
32+
loadedDocument.CreateAttachment();
33+
//Add the attachment to the document
34+
loadedDocument.Attachments.Add(attachment);
35+
36+
//Save the document into stream
37+
using(MemoryStream stream = new MemoryStream())
38+
{
39+
loadedDocument.Save(stream);
40+
}
41+
//Close the document.
42+
loadedDocument.Close(true);
43+
```
44+
By executing this code example, you will get a PDF document like the following screenshot.
45+
46+
<img src="Screenshot/Image1.png" alt="Add attachment output" width="100%" Height="Auto"/>
47+
48+
## Extracting and saving an attachment to the disk
49+
50+
Extracting and saving attachment in PDF refers to the process of removing or separating a file or document that is embedded or attached within a PDF document and saving it as a separate file on your computer or device.
51+
52+
The following code example shows how to extract attachments from an existing PDF document using C#.
53+
54+
```csharp
55+
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
56+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
57+
58+
//Iterates the attachments
59+
foreach (PdfAttachment attachment in loadedDocument.Attachments)
60+
{
61+
//Extracts the attachment and saves it to the disk
62+
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
63+
s.Write(attachment.Data, 0, attachment.Data.Length);
64+
s.Dispose();
65+
}
66+
//Close the document.
67+
loadedDocument.Close(true);
68+
69+
```
70+
71+
By executing this code example, you will get an extracted text file like the following screenshot.
72+
73+
74+
<img src="Screenshot/Image2.png" alt="Extract attachment output" width="100%" Height="Auto"/>
75+
76+
## Removing attachment from an existing PDF
77+
Removing a file attachment from a PDF can be useful in cases where the attachment is no longer needed, or when the PDF file needs to be shared without the attached file.
78+
79+
The following code example shows how to remove an attachment from an existing PDF document using C#.
80+
81+
```csharp
82+
/Load the PDF document
83+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
84+
85+
//Removes an attachment
86+
PdfAttachmentCollection attachments = loadedDocument.Attachments;
87+
attachments.RemoveAt(0);
88+
attachments.Remove(attachments[0]);
89+
attachments.Clear();
90+
91+
FileStream stream = new FileStream("output.pdf", FileMode.Create);
92+
//Save the modified document to file.
93+
loadedDocument.Save(stream);
94+
//Close the PDF document.
95+
loadedDocument.Close(true);
96+
stream.Close();
97+
```
98+
99+
By executing this code example, you will get a PDF document like the following screenshot.
100+
101+
<img src="Screenshot/Image3.png" alt="Remove attachment output" width="100%" Height="Auto"/>
102+
103+
# How to run the examples
104+
* Download this project to a location in your disk.
105+
* Open the solution file using Visual Studio.
106+
* Rebuild the solution to install the required NuGet package.
107+
* Run the application.
108+
109+
# Resources
110+
* **Product page:** [Syncfusion PDF Framework](https://www.syncfusion.com/document-processing/pdf-framework/net)
111+
* **Documentation page:** [Syncfusion .NET PDF library](https://help.syncfusion.com/file-formats/pdf/overview)
112+
* **Online demo:** [Syncfusion .NET PDF library - Online demos](https://ej2.syncfusion.com/aspnetcore/PDF/CompressExistingPDF#/bootstrap5)
113+
* **Blog:** [Syncfusion .NET PDF library - Blog](https://www.syncfusion.com/blogs/category/pdf)
114+
* **Knowledge Base:** [Syncfusion .NET PDF library - Knowledge Base](https://www.syncfusion.com/kb/windowsforms/pdf)
115+
* **EBooks:** [Syncfusion .NET PDF library - EBooks](https://www.syncfusion.com/succinctly-free-ebooks)
116+
* **FAQ:** [Syncfusion .NET PDF library - FAQ](https://www.syncfusion.com/faq/)
117+
118+
# Support and feedback
119+
* For any other queries, reach our [Syncfusion support team](https://www.syncfusion.com/support/directtrac/incidents/newincident?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or post the queries through the [community forums](https://www.syncfusion.com/forums?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
120+
* Request new feature through [Syncfusion feedback portal](https://www.syncfusion.com/feedback?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
121+
122+
# License
123+
This is a commercial product and requires a paid license for possession or use. Syncfusion’s licensed software, including this component, is subject to the terms and conditions of [Syncfusion's EULA](https://www.syncfusion.com/eula/es/?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). You can purchase a licnense [here](https://www.syncfusion.com/sales/products?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or start a free 30-day trial [here](https://www.syncfusion.com/account/manage-trials/start-trials?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
124+
125+
# About Syncfusion
126+
Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 26,000+ customers and more than 1 million users, including large financial institutions, Fortune 500 companies, and global IT consultancies.
127+
128+
Today, we provide 1600+ components and frameworks for web ([Blazor](https://www.syncfusion.com/blazor-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET Core](https://www.syncfusion.com/aspnet-core-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET MVC](https://www.syncfusion.com/aspnet-mvc-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET WebForms](https://www.syncfusion.com/jquery/aspnet-webforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Angular](https://www.syncfusion.com/angular-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [React](https://www.syncfusion.com/react-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Vue](https://www.syncfusion.com/vue-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), mobile ([Xamarin](https://www.syncfusion.com/xamarin-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), and desktop development ([WinForms](https://www.syncfusion.com/winforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WPF](https://www.syncfusion.com/wpf-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WinUI(Preview)](https://www.syncfusion.com/winui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)). We provide ready-to-deploy enterprise software for dashboards, reports, data integration, and big data processing. Many customers have saved millions in licensing fees by deploying our software.

0 commit comments

Comments
 (0)