Different from Word and Excel, PDF enables people to attach various files in it. For the convenience of both protecting the confidential files and avoiding the PDF too large, PDF also allows developers to remove any attachment. This section will introduce a solution to remove attachments from PDF via a PDF component in C#, VB.NET.
Spire.PDF for .NET, a .NET component with rich capabilities in manipulating PDF, enables you to quickly remove attachments from your PDF document. Now please see below picture and view the whole attachments remove solution after it.
In the solution, first you need a decision procedure to see whether this PDF file has any attachment, then, remove attachments. There are two situations here. One is to remove all the attachments. You can call this method: Document.Attachments.Clear(). The other is to remove a certain attachment or attachments. You can remove them either by pointing file name for example filename=="e-iceblue.html " or by the file extension such as filename.Contains(".html"). Below solution directly find the PDF via file name. Please remember to download Spire.PDF for .NET before performing the project.
using Spire.Pdf; using Spire.Pdf.Attachments; using System; using System.Windows.Forms; namespace RemoveAttachments { class Program { private void button1_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "PDF document(*.pdf)|*.pdf"; DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile(dialog.FileName); PdfAttachmentCollection attachments = doc.Attachments; PdfAttachment attachment = null; if (doc.Attachments.Count > 0) { foreach (PdfAttachment oneAttachment in doc.Attachments) { string filename = oneAttachment.FileName; if (filename == "excel.xlsx") { attachment = oneAttachment; break; } } } if (attachment != null) { doc.Attachments.Remove(attachment); } doc.SaveToFile("test.pdf"); System.Diagnostics.Process.Start("test.pdf"); } } } }
Imports Spire.Pdf Imports Spire.Pdf.Attachments Imports System.Windows.Forms Namespace RemoveAttachments Class Program Private Sub button1_Click(sender As Object, e As EventArgs) Dim dialog As New OpenFileDialog() dialog.Filter = "PDF document(*.pdf)|*.pdf" Dim result As DialogResult = dialog.ShowDialog() If result = DialogResult.OK Then Dim doc As New PdfDocument() doc.LoadFromFile(dialog.FileName) Dim attachments As PdfAttachmentCollection = doc.Attachments Dim attachment As PdfAttachment = Nothing If doc.Attachments.Count > 0 Then For Each oneAttachment As PdfAttachment In doc.Attachments Dim filename As String = oneAttachment.FileName If filename = "excel.xlsx" Then attachment = oneAttachment Exit For End If Next End If If attachment IsNot Nothing Then doc.Attachments.Remove(attachment) End If doc.SaveToFile("test.pdf") System.Diagnostics.Process.Start("test.pdf") End If End Sub End Class End Namespace
Spire.PDF is a .NET PDF component, which enables users to perform a wide range of PDF processing tasks directly, such as generate, read, write and modify PDF document in WPF, .NET and Silverlight.