Solution to Remove PDF Attachment in C# and Visual Basic

PDF attachment can be taken as assistant or supplement of document contents and help readers to learn more useful additional information. For example, one PDF document presents a famous novel and the attachment may be authors’ biography or the poster and review of film which is adapted from this novel. Of course, some attachments have nothing to do with document contents and these attachments can be removed in case readers will be confused about why the attachment appears. In this post, I want to introduce solutions to remove PDF attachment in C# and Visual Basic.

At the beginning, I prepare a PDF document with four attachments and a .NET PDF component, Spire.PDF for .NET to realize the function much easier and faster. So, before coding, I add its dll file as reference in my project.

Load PDF

Initialize a new PdfDocument instance and invoke LoadFromFile method of PdfDocument class to load the prepared PDF.


            PdfDocument document = new PdfDocument();

            document.LoadFromFile(@”E:\Work\Documents\PDF\Attachment Sample.pdf”);

Visual Basic

            Dim document As New PdfDocument()

            document.LoadFromFile(“E:\Work\Documents\PDF\Attachment Sample.pdf”)

Remove Attachment

We can choose to remove specified one or all attachments of PDF.

Remove One: invoke RemoveAt(int index) method of PdfAttachmentCollection class to remove. The following code presents to remove the first attachment.



Visual Basic


Remove All: invoke Clear() method of PdfAttachmentCollection class to remove all PDF attachments.



Visual Basic


Save PDF

Invoke SaveToFile method of PdfDocument class to save the document whose attachment has been removed and launch it.




Visual Basic




Download Spire.PDF for .NET

P.S The .NET Office component, Spire.Office for .NET can be used to realize this function as well.


One thought on “Solution to Remove PDF Attachment in C# and Visual Basic

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s