Knowledgebase (2330)
Children categories
PDF attachments allow users to see more details on a particular point by visiting attachments inside the PDF. Basically, there are two types of attachments in PDF: document level attachment and annotation attachment. Below are the differences between them.
- Document Level Attachment (represented by PdfAttachment class): A file attached to a PDF at the document level won't appear on a page, but only appear in the PDF reader's "Attachments" panel.
- Annotation Attachment (represented by PdfAttachmentAnnotation class): A file that is attached to a specific position of a page. Annotation attachments are shown as a paper clip icon on the page; reviewers can double-click the icon to open the file.
In this article, you will learn how to extract these two kinds of attachments from a PDF document in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Extract Attachments from PDF in C# and VB.NET
The document level attachments of a PDF document can be obtained through PdfDocument.Attachments property. The following steps illustrate how to extract all document level attachments from a PDF document and save them to a local folder.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get the attachment collection from the document through PdfDocument.Attachments property.
- Get the data of a specific attachment through PdfAttachment.Data property.
- Write the data to a file and save to a specified folder.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Attachments;
using System.Net.Mail;
namespace ExtractAttachments
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file that contains attachments
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");
//Get the attachment collection of the PDF document
PdfAttachmentCollection attachments = doc.Attachments;
//Specific output folder path
string outputFolder = "C:\\Users\\Administrator\\Desktop\\output\\";
//Loop through the collection
for (int i = 0; i < attachments.Count; i++)
{
//Write attachment to a file
File.WriteAllBytes(outputFolder + attachments[i].FileName, attachments[i].Data);
}
}
}
}

Extract Annotation Attachments from PDF in C# and VB.NET
Annotation attachment is a page-based element. To get annotations from a specific page, use PdfPageBase.AnnotationsWidget property. After that, you’ll need to determine if a specific annotation is an annotation attachment. The follows are the steps to extract annotation attachments from a PDF document and save them to a local folder.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get a specific page from the document through PdfDocument.Pages[] property.
- Get the annotation collection from the page through PdfPageBase.AnnotationsWidget property.
- Determine if a specific annotation is an instance of PdfAttachmentAnnotationWidget. If yes, write the annotation attachment to a file and save it to a specified folder.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Annotations;
namespace ExtractAnnotationAttachments
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file that contains attachments
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\AnnotationAttachments.pdf");
//Specific output folder path
string outputFolder = "C:\\Users\\Administrator\\Desktop\\Output\\";
//Loop through the pages
for (int i = 0; i < doc.Pages.Count; i++)
{
//Get the annotation collection
PdfAnnotationCollection collection = doc.Pages[i].Annotations;
//Loop through the annotations
for (int j = 0; j < collection.Count; j++)
{
//Determine if an annotation is an instance of PdfAttachmentAnnotationWidget
if (collection[j] is PdfAttachmentAnnotationWidget)
{
//Write annotation attachment to a file
PdfAttachmentAnnotationWidget attachmentAnnotation = (PdfAttachmentAnnotationWidget)collection[j];
String fileName = Path.GetFileName(attachmentAnnotation.FileName);
File.WriteAllBytes(outputFolder + fileName, attachmentAnnotation.Data);
}
}
}
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Delete Text Boxes in PowerPoint with C# (Including Empty Ones)
2015-02-16 08:27:07 Written by Koohji
Deleting text boxes in a PowerPoint presentation is a crucial step when cleaning up templates or removing unwanted content—but doing it manually can be time-consuming, especially when dealing with multiple slides. If you're looking for how to delete text boxes in PowerPoint using C#, you're in the right place. This guide covers everything from deleting a specific text box to removing empty ones or clearing all text boxes on a slide—providing practical, code-based solutions to streamline your workflow.
- Before We Start: Install PowerPoint Library
- Delete Specific Text Boxes in PowerPoint Slides
- Delete Empty Text Boxes in PowerPoint Slides
- Delete All Text Boxes from a PowerPoint Slide
- Conclusion
- FAQs
Before We Start: Install PowerPoint Library
Before we dive into the main content, let’s first set up the required tools. In this tutorial, we’ll be using Spire.Presentation for .NET to demonstrate how to delete text boxes in a PowerPoint presentation. This is a professional third-party PowerPoint library that allows you to manipulate slide elements—such as adding or deleting text boxes—without relying on Microsoft Office.
To install the library, you have two options:
- Download Spire.Presentation and install it manually from the official website.
- Use NuGet Package Manager, which is the recommended approach for most Visual Studio users. Simply run the following command in the Package Manager Console:
PM> Install-Package Spire.Presentation
This will automatically download and add the library to your project. A free version is available for learning or evaluation, with limited features but no time restrictions.
How to Delete a Specific Text Box in PowerPoint Slides Using C#
When you only need to remove or replace a small part of the slide content, it's best to precisely target the specific text box you want to delete. With the help of Spire.Presentation, you can easily delete a specific text box from a PowerPoint presentation. The basic workflow includes: loading the PowerPoint file, locating the slide, identifying the target text box, and removing it.
We’ll first look at the complete code, then break it down step by step.
Code Example – Remove "Text Box 1" from Slide 2:
using Spire.Presentation;
namespace RemoveTextbox
{
class Program
{
static void Main(string[] args)
{
// Create a Presentation instance and load a PowerPoint file
Presentation ppt = new Presentation("/input/pre1.pptx", FileFormat.Pptx2010);
// Get the second slide
ISlide slide = ppt.Slides[1];
// Loop through all shapes on the slide
for (int i = 0; i < slide.Shapes.Count;)
{
IAutoShape shape = slide.Shapes[i] as IAutoShape;
// Check if the shape is a text box and contains the specified text
if (shape != null && shape.IsTextBox && shape.TextFrame.Text.Equals("Text Box 1"))
{
// Remove the text box
slide.Shapes.Remove(shape);
}
else
{
i++;
}
}
// Save the modified presentation
string outputPath = "/output/Deletespecifictextbox.pptx";
ppt.SaveToFile(outputPath, FileFormat.Pptx2010);
System.Diagnostics.Process.Start(outputPath);
}
}
}
Text boxes removing result preview: 
Key steps explained:
- Create a Presentation class and load a PowerPoint file.
- Get a slide with Presentation.Slides[] property.
- Loop through shapes on the slide and check if they are IAutoShape and contain the target text.
- If it is, delete the text box using ISlide.Shapes.Remove() method.
After cleaning up empty or unwanted text boxes, you may want to add new content dynamically. Learn how to add a paragraph to a PowerPoint slide using C# in this related tutorial.
How to Delete Empty Text Boxes in PowerPoint with C#
When working with PowerPoint presentations, deleting empty text boxes is a common requirement. These unused placeholders can clutter your slides and negatively affect the overall layout. Cleaning them up is an important step in creating a polished and professional presentation.
Code example - Delete all text boxes on the 3rd slide from a Microsoft PowerPoint Presentation:
using Spire.Presentation;
namespace RemoveEmptyTextboxes
{
class Program
{
static void Main(string[] args)
{
// Load the PowerPoint presentation
Presentation ppt = new Presentation("/input/pre1.pptx", FileFormat.Pptx2010);
// Access the third slide (index starts from 0)
ISlide slide = ppt.Slides[2];
// Iterate through all shapes on the slide
for (int i = 0; i < slide.Shapes.Count;)
{
IAutoShape shape = slide.Shapes[i] as IAutoShape;
// Check if the shape is a text box and its text is null, empty, or whitespace
if (shape != null && shape.IsTextBox && string.IsNullOrWhiteSpace(shape.TextFrame.Text))
{
// Remove empty text box
slide.Shapes.Remove(shape);
}
else
{
i++;
}
}
// Save the updated presentation
string outputPath = "/output/RemoveEmptyTextboxes.pptx";
ppt.SaveToFile(outputPath, FileFormat.Pptx2010);
}
}
}
Text boxes removing result preview: 
Key steps explained:
- Load a PowerPoint file and get a slide.
- Iterate through shapes on the slide and check if they are text boxes and empty.
- Delete all empty text boxes through ISlide.Shapes.Remove() method.
Tip: If you want to delete all empty text boxes from the entire PowerPoint presentation, simply loop through each slide instead of targeting a single one. You can do this by iterating through presentation.Slides and checking each shape on every slide.
foreach (ISlide slide in presentation.Slides)
{
for (int i = slide.Shapes.Count - 1; i >= 0; i--)
{
IShape shape = slide.Shapes[i];
if (shape is IAutoShape autoShape && string.IsNullOrWhiteSpace(autoShape.TextFrame.Text))
{
slide.Shapes.Remove(shape);
}
}
}
How to Delete All Text Boxes in PowerPoint Slides Using C#
Now let’s move on to the final section—deleting all text boxes from a slide, including both empty and non-empty ones. This approach is even simpler than the previous examples. You just need to loop through the shapes on a slide, check whether each shape is an IAutoShape, and remove it using the ISlide.Shapes.Remove(shape) method. We won’t break down the steps here, as the code is self-explanatory. Just copy the snippet below, update the file path and other details as needed, and you're good to go.
Code example - Delete all text boxes on the second slide:
namespace RemoveTextboxes
{
internal class Program
{
static void Main(string[] args)
{
// Create a new Presentation object
Presentation ppt = new Presentation("/input/pre1.pptx", FileFormat.Pptx2010);
// Get the second slide and loop through its shapes
ISlide slide = ppt.Slides[1];
for (int i = 0; i < slide.Shapes.Count;)
{
// Check if the shape is an AutoShape and remove it
IAutoShape shape = slide.Shapes[i] as IAutoShape;
slide.Shapes.Remove(shape);
}
// Save the updated presentation
ppt.SaveToFile("/output/deletetextbox.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("Result.pptx");
}
}
}
Text boxes removing result preview: 
The Conclusion
The page explored how to delete text boxes in PowerPoint using C#. Whether you’re removing a specific text box, deleting empty text boxes, or clearing all text boxes, the process becomes simple and straightforward with the help of Spire.Presentation for .NET. If you’re interested in this PowerPoint library, you can request a free 30-day trial license to explore its full capabilities.
FAQs about Deleting Text Boxes in PowerPoint
1. Why can't I delete a text box in PowerPoint?
There are a few possible reasons: the text box might be part of the slide master, grouped with other elements, or accidentally locked. If you're automating PowerPoint using C#, make sure you correctly access the Shapes collection of the target slide and identify the right shape type (e.g., IAutoShape) before attempting to delete it.
2. How do I delete a text box from a PowerPoint slide using C#?
You can access the slide using Presentation.Slides[index], loop through the Shapes collection, find the text box (typically an IAutoShape), and remove it with ISlide.Shapes.Remove(shape). Full code examples are provided in this article for deleting specific, empty, or all text boxes.
How to Convert Embedded Excel Sheet to Word Table in C#, VB.NET
2015-02-11 01:04:49 Written by KoohjiIn our daily work, we may receive Word documents that will sometimes contain embedded Excel object (sheet) and we need to convert embedded Excel sheet to Word table so that we can easily change the date or format the table with style. In this article, you will learn how to convert embedded Excel sheet to Word table using Spire.Doc and Spire.XLS in C#, VB.NET.
Firstly, you need to download Spire.Office because Spire.Doc and Spire.XLS will be used in the same program. Add Spire.Doc.dll and Spire.XLS.dll as references in your VS project. Then follow the program guidance below to finish this work.
Step 1: Create a new Word document, load the sample file. Get the paragraph that contains the Excel object from the section. Initialize a new datatable.
Document doc = new Document("Sample.docx", Spire.Doc.FileFormat.Docx2010);
Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[2];
DataTable dt = new DataTable();
Step 2: Traverse every DocumentObject in the paragraph, use IF statement to test if DocumentObject is OLE object, use another IF statement to test if OLE object type is Excel.Sheet.12. If yes, save the data of OLE object to a workbook through LoadFromStrem(). Then export data from worksheet to datatable.
foreach (DocumentObject obj in para.ChildObjects)
{
if (DocumentObjectType.OleObject == obj.DocumentObjectType)
{
DocOleObject dObj = obj as DocOleObject;
if (dObj.ObjectType == "Excel.Sheet.12")
{
Workbook wb = new Workbook();
wb.LoadFromStream(new MemoryStream(dObj.NativeData));
Worksheet ws = wb.Worksheets[0];
dt = ws.ExportDataTable(ws.AllocatedRange, false);
}
}
}
Step 3: Create a new Word table and set row number and column number according to rows and columns of datatable. Export data from datatable to Word table.
Table table = section.AddTable(true);
table.ResetCells(dt.Rows.Count, dt.Columns.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string text = dt.Rows[i][j] as string;
table.Rows[i].Cells[j].AddParagraph().AppendText(text);
}
}
Step 4: Save the file.
doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010);
Result:

Full Code:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Xls;
using System.Data;
using System.IO;
namespace ApplyTableStyles
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document("Sample.docx", Spire.Doc.FileFormat.Docx2010);
Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[2];
DataTable dt = new DataTable();
foreach (DocumentObject obj in para.ChildObjects)
{
if (DocumentObjectType.OleObject == obj.DocumentObjectType)
{
DocOleObject dObj = obj as DocOleObject;
if (dObj.ObjectType == "Excel.Sheet.12")
{
Workbook wb = new Workbook();
wb.LoadFromStream(new MemoryStream(dObj.NativeData));
Worksheet ws = wb.Worksheets[0];
dt = ws.ExportDataTable(ws.AllocatedRange, false);
}
}
}
Table table = section.AddTable(true);
table.ResetCells(dt.Rows.Count, dt.Columns.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string text = dt.Rows[i][j] as string;
table.Rows[i].Cells[j].AddParagraph().AppendText(text);
}
}
doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Xls
Imports System.Data
Imports System.IO
Namespace ApplyTableStyles
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document("Sample.docx", Spire.Doc.FileFormat.Docx2010)
Dim section As Section = doc.Sections(0)
Dim para As Paragraph = section.Paragraphs(2)
Dim dt As New DataTable()
For Each obj As DocumentObject In para.ChildObjects
If DocumentObjectType.OleObject = obj.DocumentObjectType Then
Dim dObj As DocOleObject = TryCast(obj, DocOleObject)
If dObj.ObjectType = "Excel.Sheet.12" Then
Dim wb As New Workbook()
wb.LoadFromStream(New MemoryStream(dObj.NativeData))
Dim ws As Worksheet = wb.Worksheets(0)
dt = ws.ExportDataTable(ws.AllocatedRange, False)
End If
End If
Next
Dim table As Table = section.AddTable(True)
table.ResetCells(dt.Rows.Count, dt.Columns.Count)
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt.Columns.Count - 1
Dim text As String = TryCast(dt.Rows(i)(j), String)
table.Rows(i).Cells(j).AddParagraph().AppendText(text)
Next
Next
doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010)
End Sub
End Class
End Namespace