Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions AddAttachmentPDF/AddAttachmentPDF.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddAttachmentPDF", "AddAttachmentPDF\AddAttachmentPDF.csproj", "{B11BEB39-0721-4E8E-B093-1451A0353C7D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B11BEB39-0721-4E8E-B093-1451A0353C7D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FFB6BC3F-9061-4C56-AF62-94F281D0A865}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions AddAttachmentPDF/AddAttachmentPDF/AddAttachmentPDF.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

</Project>
Binary file added AddAttachmentPDF/AddAttachmentPDF/Input.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions AddAttachmentPDF/AddAttachmentPDF/Input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
25 changes: 25 additions & 0 deletions AddAttachmentPDF/AddAttachmentPDF/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

//Load the PDF document
FileStream docStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Creates an attachment
Stream fileStream = new FileStream("../../../Input.txt", FileMode.Open, FileAccess.Read);
PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream);
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "application/txt";

if (loadedDocument.Attachments == null)
loadedDocument.CreateAttachment();
//Add the attachment to the document
loadedDocument.Attachments.Add(attachment);

//Save the document into stream
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
File.WriteAllBytes("../../../Output.pdf",stream.ToArray());
loadedDocument.Close(true);
stream.Close();
25 changes: 25 additions & 0 deletions ExtractAttachmentPDF/ExtractAttachmentPDF.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractAttachmentPDF", "ExtractAttachmentPDF\ExtractAttachmentPDF.csproj", "{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE07073D-3FDC-481E-8F53-E1EDCF5C08B3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CD1A86FC-9315-4940-83CE-F5D00C67FB57}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

</Project>
Binary file not shown.
22 changes: 22 additions & 0 deletions ExtractAttachmentPDF/ExtractAttachmentPDF/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Load the PDF document
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using System.IO;

//Load the PDF document
FileStream docStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.Read);
//Load an existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Iterates the attachments
foreach (PdfAttachment attachment in loadedDocument.Attachments)
{
//Extracts the attachment and saves it to the disk
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
s.Write(attachment.Data, 0, attachment.Data.Length);
s.Dispose();
}

//Close the PDF document.
loadedDocument.Close(true);
130 changes: 128 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,128 @@
# create-pdf-attachments-csharp
This repository contains examples to create attachments in PDF using C#
# Add and Remove Attachments in PDF using C#

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.

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:

Name | Description
--- | ---
[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.
[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.
[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.

## Add an attachment in a PDF using C#

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.

The following code example shows how to add an attachment to a PDF file using C#.

```csharp
//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Creates an attachment
Stream fileStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream);
attachment.ModificationDate = DateTime.Now;
attachment.Description = "Input.txt";
attachment.MimeType = "application/txt";

if (loadedDocument.Attachments == null)
loadedDocument.CreateAttachment();
//Add the attachment to the document
loadedDocument.Attachments.Add(attachment);

//Save the document into stream
using(MemoryStream stream = new MemoryStream())
{
loadedDocument.Save(stream);
}
//Close the document.
loadedDocument.Close(true);
```
By executing this code example, you will get a PDF document like the following screenshot.

<img src="Screenshot/Image1.png" alt="Add attachment output" width="100%" Height="Auto"/>

## Extracting and saving an attachment to the disk

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.

The following code example shows how to extract attachments from an existing PDF document using C#.

```csharp
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Iterates the attachments
foreach (PdfAttachment attachment in loadedDocument.Attachments)
{
//Extracts the attachment and saves it to the disk
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
s.Write(attachment.Data, 0, attachment.Data.Length);
s.Dispose();
}
//Close the document.
loadedDocument.Close(true);

```

By executing this code example, you will get an extracted text file like the following screenshot.


<img src="Screenshot/Image2.png" alt="Extract attachment output" width="100%" Height="Auto"/>

## Removing attachment from an existing PDF
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.

The following code example shows how to remove an attachment from an existing PDF document using C#.

```csharp
/Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

//Removes an attachment
PdfAttachmentCollection attachments = loadedDocument.Attachments;
attachments.RemoveAt(0);
attachments.Remove(attachments[0]);
attachments.Clear();

FileStream stream = new FileStream("output.pdf", FileMode.Create);
//Save the modified document to file.
loadedDocument.Save(stream);
//Close the PDF document.
loadedDocument.Close(true);
stream.Close();
```

By executing this code example, you will get a PDF document like the following screenshot.

<img src="Screenshot/Image3.png" alt="Remove attachment output" width="100%" Height="Auto"/>

# How to run the examples
* Download this project to a location in your disk.
* Open the solution file using Visual Studio.
* Rebuild the solution to install the required NuGet package.
* Run the application.

# Resources
* **Product page:** [Syncfusion PDF Framework](https://www.syncfusion.com/document-processing/pdf-framework/net)
* **Documentation page:** [Syncfusion .NET PDF library](https://help.syncfusion.com/file-formats/pdf/overview)
* **Online demo:** [Syncfusion .NET PDF library - Online demos](https://ej2.syncfusion.com/aspnetcore/PDF/CompressExistingPDF#/bootstrap5)
* **Blog:** [Syncfusion .NET PDF library - Blog](https://www.syncfusion.com/blogs/category/pdf)
* **Knowledge Base:** [Syncfusion .NET PDF library - Knowledge Base](https://www.syncfusion.com/kb/windowsforms/pdf)
* **EBooks:** [Syncfusion .NET PDF library - EBooks](https://www.syncfusion.com/succinctly-free-ebooks)
* **FAQ:** [Syncfusion .NET PDF library - FAQ](https://www.syncfusion.com/faq/)

# Support and feedback
* 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).
* Request new feature through [Syncfusion feedback portal](https://www.syncfusion.com/feedback?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).

# License
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).

# About Syncfusion
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.

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.
25 changes: 25 additions & 0 deletions RemoveAttachmentPDF/RemoveAttachmentPDF.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RemoveAttachmentPDF", "RemoveAttachmentPDF\RemoveAttachmentPDF.csproj", "{ED6E7997-4A76-4751-8393-4D8FB41A3995}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ED6E7997-4A76-4751-8393-4D8FB41A3995}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED6E7997-4A76-4751-8393-4D8FB41A3995}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED6E7997-4A76-4751-8393-4D8FB41A3995}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED6E7997-4A76-4751-8393-4D8FB41A3995}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F5D2CF2-2B59-460F-AB52-196682139A77}
EndGlobalSection
EndGlobal
Binary file added RemoveAttachmentPDF/RemoveAttachmentPDF/Input.pdf
Binary file not shown.
18 changes: 18 additions & 0 deletions RemoveAttachmentPDF/RemoveAttachmentPDF/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

//Load the PDF document
FileStream docStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

PdfAttachmentCollection attachments = loadedDocument.Attachments;
attachments.RemoveAt(0);
attachments.Remove(attachments[0]);
attachments.Clear();

//Save the document into stream
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
File.WriteAllBytes("../../../Output.pdf", stream.ToArray());
loadedDocument.Close();
stream.Close();
14 changes: 14 additions & 0 deletions RemoveAttachmentPDF/RemoveAttachmentPDF/RemoveAttachmentPDF.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

</Project>
Binary file added Screenshot/Image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshot/Image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshot/Image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.