Remove Attachments from PDF in C#, VB.NET

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.

Remove PDF Attachments

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.

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();
                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;
                if (attachment !=null)
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim dialog As OpenFileDialog = New OpenFileDialog
        dialog.Filter = "PDF document(*.pdf)|*.pdf"
        Dim result As DialogResult = dialog.ShowDialog
        If (result = DialogResult.OK) Then
            Dim doc As PdfDocument = New PdfDocument
            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
            End If
            If (Not (attachment) Is Nothing) Then
            End If
        End If
    End Sub

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.