|
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