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.
Initialize a new PdfDocument instance and invoke LoadFromFile method of PdfDocument class to load the prepared PDF.
PdfDocument document = new PdfDocument();
Dim document As New PdfDocument()
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.
Remove All: invoke Clear() method of PdfAttachmentCollection class to remove all PDF attachments.
Invoke SaveToFile method of PdfDocument class to save the document whose attachment has been removed and launch it.
P.S The .NET Office component, Spire.Office for .NET can be used to realize this function as well.