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.
C#
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.
C#
document.Attachments.RemoveAt(0);
Visual Basic
document.Attachments.RemoveAt(0)
Remove All: invoke Clear() method of PdfAttachmentCollection class to remove all PDF attachments.
C#
document.Attachments.Clear();
Visual Basic
document.Attachments.Clear()
Save PDF
Invoke SaveToFile method of PdfDocument class to save the document whose attachment has been removed and launch it.
C#
document.SaveToFile(“RemoveAtta.pdf”);
System.Diagnostics.Process.Start(“RemoveAtta.pdf”);
Visual Basic
document.SaveToFile(“RemoveAtta.pdf”)
System.Diagnostics.Process.Start(“RemoveAtta.pdf”)
P.S The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

