diff --git a/AddAttachmentPDF/AddAttachmentPDF.sln b/AddAttachmentPDF/AddAttachmentPDF.sln new file mode 100644 index 0000000..f298118 --- /dev/null +++ b/AddAttachmentPDF/AddAttachmentPDF.sln @@ -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 diff --git a/AddAttachmentPDF/AddAttachmentPDF/AddAttachmentPDF.csproj b/AddAttachmentPDF/AddAttachmentPDF/AddAttachmentPDF.csproj new file mode 100644 index 0000000..be303dc --- /dev/null +++ b/AddAttachmentPDF/AddAttachmentPDF/AddAttachmentPDF.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/AddAttachmentPDF/AddAttachmentPDF/Input.pdf b/AddAttachmentPDF/AddAttachmentPDF/Input.pdf new file mode 100644 index 0000000..6d10bb5 Binary files /dev/null and b/AddAttachmentPDF/AddAttachmentPDF/Input.pdf differ diff --git a/AddAttachmentPDF/AddAttachmentPDF/Input.txt b/AddAttachmentPDF/AddAttachmentPDF/Input.txt new file mode 100644 index 0000000..70c379b --- /dev/null +++ b/AddAttachmentPDF/AddAttachmentPDF/Input.txt @@ -0,0 +1 @@ +Hello world \ No newline at end of file diff --git a/AddAttachmentPDF/AddAttachmentPDF/Program.cs b/AddAttachmentPDF/AddAttachmentPDF/Program.cs new file mode 100644 index 0000000..30f2054 --- /dev/null +++ b/AddAttachmentPDF/AddAttachmentPDF/Program.cs @@ -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(); \ No newline at end of file diff --git a/ExtractAttachmentPDF/ExtractAttachmentPDF.sln b/ExtractAttachmentPDF/ExtractAttachmentPDF.sln new file mode 100644 index 0000000..42ad22f --- /dev/null +++ b/ExtractAttachmentPDF/ExtractAttachmentPDF.sln @@ -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 diff --git a/ExtractAttachmentPDF/ExtractAttachmentPDF/ExtractAttachmentPDF.csproj b/ExtractAttachmentPDF/ExtractAttachmentPDF/ExtractAttachmentPDF.csproj new file mode 100644 index 0000000..09c6d7f --- /dev/null +++ b/ExtractAttachmentPDF/ExtractAttachmentPDF/ExtractAttachmentPDF.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/ExtractAttachmentPDF/ExtractAttachmentPDF/Input.pdf b/ExtractAttachmentPDF/ExtractAttachmentPDF/Input.pdf new file mode 100644 index 0000000..8afc3f8 Binary files /dev/null and b/ExtractAttachmentPDF/ExtractAttachmentPDF/Input.pdf differ diff --git a/ExtractAttachmentPDF/ExtractAttachmentPDF/Program.cs b/ExtractAttachmentPDF/ExtractAttachmentPDF/Program.cs new file mode 100644 index 0000000..0763ee4 --- /dev/null +++ b/ExtractAttachmentPDF/ExtractAttachmentPDF/Program.cs @@ -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); diff --git a/README.md b/README.md index e5748d1..022c29c 100644 --- a/README.md +++ b/README.md @@ -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. + +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#. + +```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. + + +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#. + +```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. + +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 +* **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. diff --git a/RemoveAttachmentPDF/RemoveAttachmentPDF.sln b/RemoveAttachmentPDF/RemoveAttachmentPDF.sln new file mode 100644 index 0000000..bd58aa6 --- /dev/null +++ b/RemoveAttachmentPDF/RemoveAttachmentPDF.sln @@ -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 diff --git a/RemoveAttachmentPDF/RemoveAttachmentPDF/Input.pdf b/RemoveAttachmentPDF/RemoveAttachmentPDF/Input.pdf new file mode 100644 index 0000000..2d7ce16 Binary files /dev/null and b/RemoveAttachmentPDF/RemoveAttachmentPDF/Input.pdf differ diff --git a/RemoveAttachmentPDF/RemoveAttachmentPDF/Program.cs b/RemoveAttachmentPDF/RemoveAttachmentPDF/Program.cs new file mode 100644 index 0000000..bd36b6e --- /dev/null +++ b/RemoveAttachmentPDF/RemoveAttachmentPDF/Program.cs @@ -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(); \ No newline at end of file diff --git a/RemoveAttachmentPDF/RemoveAttachmentPDF/RemoveAttachmentPDF.csproj b/RemoveAttachmentPDF/RemoveAttachmentPDF/RemoveAttachmentPDF.csproj new file mode 100644 index 0000000..09c6d7f --- /dev/null +++ b/RemoveAttachmentPDF/RemoveAttachmentPDF/RemoveAttachmentPDF.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Screenshot/Image1.png b/Screenshot/Image1.png new file mode 100644 index 0000000..0661bcd Binary files /dev/null and b/Screenshot/Image1.png differ diff --git a/Screenshot/Image2.png b/Screenshot/Image2.png new file mode 100644 index 0000000..ad92577 Binary files /dev/null and b/Screenshot/Image2.png differ diff --git a/Screenshot/Image3.png b/Screenshot/Image3.png new file mode 100644 index 0000000..9d5ccd5 Binary files /dev/null and b/Screenshot/Image3.png differ