Skip to content

This repository contains examples to create attachments in PDF using C#

Notifications You must be signed in to change notification settings

SyncfusionExamples/create-pdf-attachments-csharp

Repository files navigation

Add and Remove Attachments in PDF using C#

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

//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.

Add attachment output

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

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.

Extract attachment output

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

/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.

Remove attachment output

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

Support and feedback

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. You can purchase a licnense here or start a free 30-day trial here.

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, ASP.NET Core, ASP.NET MVC, ASP.NET WebForms, JavaScript, Angular, React, Vue, and Flutter), mobile (Xamarin, Flutter, UWP, and JavaScript), and desktop development (WinForms, WPF, WinUI(Preview), Flutter and UWP). 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.

About

This repository contains examples to create attachments in PDF using C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages