How to Align a Table in C#
Usually there are three kinds of alignment style for a word table: left aligned, centered and right aligned. On Microsoft word, we can go to table properties to set the alignment for the whole table. Spire.Doc also offers a property table.TableFormat.HorizontalAlignment to enable developers to set the table alignment style easily in C#. This article will demonstrate how to align a table in C#.
Firstly, view the how to align a table for Microsoft word:

Here come to the code snippet of how Spire.Doc align a table.
Step 1: Create a word document and load from file.
Document doc = new Document();
doc.LoadFromFile("sample.docx");
Step 2: Get the first section and two tables from the word document.
Section section = doc.Sections[0]; Table table = section.Tables[0] as Table; Table table1 = section.Tables[1] as Table;
Step 3: Set the different alignment properties for each table.
table.Format.HorizontalAlignment = RowAlignment.Right; table.Format.LeftIndent = 34; table1.Format.HorizontalAlignment = RowAlignment.Left; table1.Format.LeftIndent = 34;
Step 4: Save the document to file:
doc.SaveToFile("result.docx", FileFormat.Docx);
Effective screenshots after align the table format:

Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
namespace AlignTable
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("sample.docx");
Section section = doc.Sections[0];
Table table = section.Tables[0] as Table;
Table table1 = section.Tables[1] as Table;
table.Format.HorizontalAlignment = RowAlignment.Right;
table.Format.LeftIndent = 34;
table1.Format.HorizontalAlignment = RowAlignment.Left;
table1.Format.LeftIndent = 34;
doc.SaveToFile("result.docx", FileFormat.Docx);
}
}
}
Set XMP Metadata of a PDF Document in C#/VB.NET
XMP is a file labeling technology that lets you embed metadata into files themselves during the content creation process. With an XMP enabled application, your workgroup can capture meaningful information about a project (such as titles and descriptions, searchable keywords, and up-to-date author and copyright information) in a format that is easily understood by your team as well as by software applications, hardware devices, and even file formats.
In the Spire.PDF Version 3.6.135 and above, we add a new feature to read, set and load an existing XMP data from XML documents. This article presents how to set XMP Metadata while creating a PDF document.
Code Snippet:
Step 1: Initialize a new instance of PdfDocument class.
string input = "..\\..\\..\\..\\..\\..\\Data\\SetXMPMetadata.pdf"; // Open a PDF document. PdfDocument doc = new PdfDocument(); doc.LoadFromFile(input); // Set XMP metadata for the document. doc.DocumentInformation.Author = "E-iceblue"; doc.DocumentInformation.Creator = "Spire.PDF"; doc.DocumentInformation.Keywords = "XMP"; doc.DocumentInformation.Producer = "E-icenlue Co,.Ltd"; doc.DocumentInformation.Subject = "XMP Metadata"; doc.DocumentInformation.Title = "Set XMP Metadata in PDF"; // Specify the output file name for the modified PDF. string output = "SetXMPMetadata.pdf"; // Save the PDF document with the updated XMP metadata. doc.SaveToFile(output);
Output:
To view metadata in a PDF document, open it with Acrobat or Acrobat Reader and select ‘Document Properties’ in the File menu.

Full Code:
using Spire.Pdf;
using Spire.Pdf.Xmp;
using System;
namespace SetXMPMetadata
{
class Program
{
static void Main(string[] args)
{
string input = "..\\..\\..\\..\\..\\..\\Data\\SetXMPMetadata.pdf";
// Open a PDF document.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(input);
// Set XMP metadata for the document.
doc.DocumentInformation.Author = "E-iceblue";
doc.DocumentInformation.Creator = "Spire.PDF";
doc.DocumentInformation.Keywords = "XMP";
doc.DocumentInformation.Producer = "E-icenlue Co,.Ltd";
doc.DocumentInformation.Subject = "XMP Metadata";
doc.DocumentInformation.Title = "Set XMP Metadata in PDF";
// Specify the output file name for the modified PDF.
string output = "SetXMPMetadata.pdf";
// Save the PDF document with the updated XMP metadata.
doc.SaveToFile(output);
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Xmp
Namespace SetXMPMetadata
Class Program
Private Shared Sub Main(args As String())
Load the input PDF file
Dim input As String = "..\..\..\..\..\..\Data\SetXMPMetadata.pdf"
' Create a new PdfDocument object
Dim doc As New PdfDocument()
' Load the PDF document from the input file
doc.LoadFromFile(input)
' Set the author information in the document properties
doc.DocumentInformation.Author = "E-iceblue"
' Set the creator information in the document properties
doc.DocumentInformation.Creator = "Spire.PDF"
' Set the keywords information in the document properties
doc.DocumentInformation.Keywords = "XMP"
' Set the producer information in the document properties
doc.DocumentInformation.Producer = "E-icenlue Co,.Ltd"
' Set the subject information in the document properties
doc.DocumentInformation.Subject = "XMP Metadata"
' Set the title information in the document properties
doc.DocumentInformation.Title = "Set XMP Metadata in PDF"
' Specify the output file name
Dim output As String = "SetXMPMetadata.pdf"
' Save the modified document to the output file
doc.SaveToFile(output)
End Sub
End Class
End Namespace
How to delete layer in PDF
With the help of Spire.PDF, we can add several kinds of layers such as line, image, string, ellipse, rectangle and pie to any page of a new or an existing pdf document. At the same time, it also supports us to delete specific layer from a pdf document.
In this section, we're going to demonstrate how to delete layer in PDF using Spire.PDF for .NET. To add layer to PDF, please check this article: How to add layers to PDF file in C#.
Below is the screenshot of the original PDF document which contains three layers: a red line layer and two image layers.

Before start, download Spire.PDF and install it correctly, next add the corresponding dll file from the installation folder as reference of your project.
Detail steps:
Step 1: Initialize a new instance of PdfDocument class and load the sample document from file.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("AddLayer.pdf");
Step 2: Get its first page and delete the specific layer by name from page one.
PdfPageBase page = doc.Pages[0];
page.PageLayers.DeleteOldLayer("red line");
Step 3: Save and launch the file.
doc.SaveToFile("delete.pdf");
System.Diagnostics.Process.Start("delete.pdf");
Effective screenshot after deleting:

Full codes:
using Spire.Pdf;
namespace Delete_page_layer_in_PDF
{
class Program
{
static void Main(string[] args)
{
// Load the document from disk
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"..\..\..\..\..\..\Data\DeleteLayer.pdf");
// Remove the "red line" layer from the document
doc.Layers.RemoveLayer("red line");
// Save the modified document to a new file
doc.SaveToFile("Output.pdf");
// View the Pdf file
PDFDocumentViewer("Output.pdf");
}
}
}
How to add layers to PDF file in C#
Developers can use PDF layer to set some content to be visible and others to be invisible in the same PDF file. It makes the PDF Layer widely be used to deal with related contents within the same PDF. Now developers can easily add page layers by using class PdfPageLayer offered by Spire.PDF. This article will focus on showing how to add layers to a PDF file in C# with the help of Spire.PDF.
Note: Before Start, please download the latest version of Spire.PDF and add Spire.PDF.dll in the bin folder as the reference of Visual Studio.
Here comes to the details:
Step 1: Create a new PDF document
PdfDocument pdfdoc = new PdfDocument();
Step 2: Add a new page to the PDF document.
PdfPageBase page = pdfdoc.Pages.Add();
Step 3: Add a layer named "red line" to the PDF page.
PdfLayer layer = doc.Layers.AddLayer("red line", PdfVisibility.On);
Step 4: Draw a red line to the added layer.
// Create a graphics context for drawing on the specified page's canvas using the created layer PdfCanvas pcA = layer.CreateGraphics(page.Canvas); // Draw a red line on the graphics context using a pen with thickness 2, starting from (100, 350) to (300, 350) pcA.DrawLine(new PdfPen(PdfBrushes.Red, 2), new PointF(100, 350), new PointF(300, 350));
Step 5: Use the same method above to add the other two layers to the PDF page.
layer = doc.Layers.AddLayer("blue line");
PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);
pcB.DrawLine(new PdfPen(PdfBrushes.Blue, 2), new PointF(100, 400), new PointF(300, 400));
layer = doc.Layers.AddLayer("green line");
PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);
pcC.DrawLine(new PdfPen(PdfBrushes.Green, 2), new PointF(100, 450), new PointF(300, 450));
Step 6: Save the document to file.
pdfdoc.SaveToFile("AddLayers.pdf", FileFormat.PDF);
Effective screenshot:

Full codes:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddLayer
{
class Program
{
static void Main(string[] args)
{
// Create a new PdfDocument object
PdfDocument doc = new PdfDocument();
// Load an existing PDF document from the specified file path
doc.LoadFromFile(@"..\..\..\..\..\..\Data\AddLayers.pdf");
// Get the first page of the loaded document
PdfPageBase page = doc.Pages[0];
// Create a new layer named "red line" with visibility set to "On"
PdfLayer layer = doc.Layers.AddLayer("red line", PdfVisibility.On);
// Create a graphics context for drawing on the specified page's canvas using the created layer
PdfCanvas pcA = layer.CreateGraphics(page.Canvas);
// Draw a red line on the graphics context using a pen with thickness 2, starting from (100, 350) to (300, 350)
pcA.DrawLine(new PdfPen(PdfBrushes.Red, 2), new PointF(100, 350), new PointF(300, 350));
// Create a new layer named "blue line" without specifying visibility (default is "Off")
layer = doc.Layers.AddLayer("blue line");
// Create a graphics context for drawing on the first page's canvas using the newly created layer
PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);
// Draw a blue line on the graphics context using a pen with thickness 2, starting from (100, 400) to (300, 400)
pcB.DrawLine(new PdfPen(PdfBrushes.Blue, 2), new PointF(100, 400), new PointF(300, 400));
// Create a new layer named "green line" without specifying visibility (default is "Off")
layer = doc.Layers.AddLayer("green line");
// Create a graphics context for drawing on the first page's canvas using the newly created layer
PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);
// Draw a green line on the graphics context using a pen with thickness 2, starting from (100, 450) to (300, 450)
pcC.DrawLine(new PdfPen(PdfBrushes.Green, 2), new PointF(100, 450), new PointF(300, 450));
// Specify the output file name for the modified PDF
string output = "AddLayers.pdf";
// Save the modified PDF document to the specified output file
doc.SaveToFile(output);
}
}
}
How to set internal margin for Word text box in C#
In our tutorials, there are articles introducing the method to insert, remove, position text box and extract text from text box. This article is going to give the documentation of how to set the internal margin for textbox with the position and line style settings included using Spire.Doc.
Note: before start, please download the latest version of Spire.Doc and add the .dll in the bin folder as the reference of Visual Studio.
Step 1: Create a Word document and add a section.
Document document = new Document(); Section sec = document.AddSection();
Step 2: Add a text box and append sample text.
TextBox TB = document.Sections[0].AddParagraph().AppendTextBox(310, 90);
Paragraph para = TB.Body.AddParagraph();
TextRange TR = para.AppendText("Using Spire.Doc, developers will find a simple and effective method to endow their applications with rich MS Word features. ");
TR.CharacterFormat.FontName = "Cambria ";
TR.CharacterFormat.FontSize = 13;
Step 3: Set exact position for the text box.
TB.Format.HorizontalOrigin = HorizontalOrigin.Page; TB.Format.HorizontalPosition = 80; TB.Format.VerticalOrigin = VerticalOrigin.Page; TB.Format.VerticalPosition = 100;
Step 4: Set line style for the text box.
TB.Format.LineStyle = TextBoxLineStyle.Double; TB.Format.LineColor = Color.CornflowerBlue; TB.Format.LineDashing = LineDashing.DashDotDot; TB.Format.LineWidth = 5;
Step 5: Set internal margin for the text box:
TB.Format.InternalMargin.Top = 15; TB.Format.InternalMargin.Bottom = 10; TB.Format.InternalMargin.Left = 12; TB.Format.InternalMargin.Right = 10;
Step 6: Save the document and launch to see effects.
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
Effects:

Full Codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section sec = document.AddSection();
TextBox TB = document.Sections[0].AddParagraph().AppendTextBox(310, 90);
Paragraph para = TB.Body.AddParagraph();
TextRange TR = para.AppendText("Using Spire.Doc, developers will find a simple and effective method to endow their applications with rich MS Word features. ");
TR.CharacterFormat.FontName = "Cambria ";
TR.CharacterFormat.FontSize = 13;
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = 80;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 100;
TB.Format.LineStyle = TextBoxLineStyle.Double;
TB.Format.LineColor = Color.CornflowerBlue;
TB.Format.LineDashing = LineDashing.DashDotDot;
TB.Format.LineWidth = 5;
TB.Format.InternalMargin.Top = 15;
TB.Format.InternalMargin.Bottom = 10;
TB.Format.InternalMargin.Left = 12;
TB.Format.InternalMargin.Right = 10;
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
Embed 3D Interactive Graphics into PDF Document in C#/VB.NET
Universal 3D (U3D) is a compressed file format for 3D computer graphic data. 3D modules in U3D format can be inserted into PDF documents and interactively visualized by Acrobat Reader. This article presents how to embed a pre-created U3D file into a PDF document using Spire.PDF in C#, VB.NET.
Main Steps:
Step 1: Initialize a new object of PdfDocuemnt, and add a blank page to the PDF document.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
Step 2: Draw a rectangle on the page to define the canvas area for the 3D file.
Rectangle rt = new Rectangle(0, 80, 200, 200);
Step 3: Initialize a new object of Pdf3DAnnotation, load the .u3d file as 3D annotation.
Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, "teapot.u3d"); annotation.Activation = new Pdf3DActivation(); annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen;
Step 4: Define a 3D view mode.
Pdf3DView View= new Pdf3DView(); View.Background = new Pdf3DBackground(new PdfRGBColor(Color.Purple )); View.ViewNodeName = "test"; View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid); View.InternalName = "test"; View.LightingScheme = new Pdf3DLighting(); View.LightingScheme.Style = Pdf3DLightingStyle.Day;
Step 5: Set the 3D view mode for the annotation.
annotation.Views.Add(View);
Step 6: Add the annotation to PDF.
page.Annotations.Add(annotation);
Step 7: Save the file.
doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF);
Output:

Full Code:
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace Embed3DInteractiveGraphics
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
Rectangle rt = new Rectangle(0, 80, 200, 200);
Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, "teapot.u3d");
annotation.Activation = new Pdf3DActivation();
annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen;
Pdf3DView View= new Pdf3DView();
View.Background = new Pdf3DBackground(new PdfRGBColor(Color.Purple));
View.ViewNodeName = "test";
View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid);
View.InternalName = "test";
View.LightingScheme = new Pdf3DLighting();
View.LightingScheme.Style = Pdf3DLightingStyle.Day;
annotation.Views.Add(View);
page.Annotations.Add(annotation);
doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF);
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace Embed3DInteractiveGraphics
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()
Dim rt As New Rectangle(0, 80, 200, 200)
Dim annotation As New Pdf3DAnnotation(rt, "teapot.u3d")
annotation.Activation = New Pdf3DActivation()
annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen
Dim View As New Pdf3DView()
View.Background = New Pdf3DBackground(New PdfRGBColor(Color.Purple))
View.ViewNodeName = "test"
View.RenderMode = New Pdf3DRendermode(Pdf3DRenderStyle.Solid)
View.InternalName = "test"
View.LightingScheme = New Pdf3DLighting()
View.LightingScheme.Style = Pdf3DLightingStyle.Day
annotation.Views.Add(View)
page.AnnotationsWidget.Add(annotation)
doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
How to add line numbers in C#
Line numbers is used to display the number of lines counted automatically by Word next to each line of text. It's very useful when we need to refer to specific lines in a document such as contract or legal papers. Line numbers function in word allows us to set the start value, the number interval, the distance from text and the numbering mode of line numbers. Using Spire.Doc, we could achieve all of features mentioned above. This article is going to introduce how to add line numbers in C# using Spire.Doc.
Note: Before start, please download the latest version of Spire.Doc and add Spire.Doc .dll in the bin folder as the reference of visual studio.
Step 1: Load the sample document which only has text.
Document document = new Document();
document.LoadFromFile("T.docx");
Step 2: Set the start value of the line numbers.
document.Sections[0].PageSetup.LineNumberingStartValue = 1;
Step 3: Set the interval between displayed numbers.
document.Sections[0].PageSetup.LineNumberingStep = 6;
Step 4: Set the distance between line numbers and text.
document.Sections[0].PageSetup.LineNumberingDistanceFromText = 40f;
Step 5: Set the numbering mode of line numbers. Here we have four choices: None, Continuous, RestartPage and RestartSection.
document.Sections[0].PageSetup.LineNumberingRestartMode = LineNumberingRestartMode.Continuous;
Step 6: Save the document and launch to see effects.
document.SaveToFile("result.docx",FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
Effects:
Single Page:

Continuous Page:

Full Codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
namespace How_to_add_line_numbering
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("T.docx");
document.Sections[0].PageSetup.LineNumberingStartValue = 1;
document.Sections[0].PageSetup.LineNumberingStep = 6;
document.Sections[0].PageSetup.LineNumberingDistanceFromText = 40f;
document.Sections[0].PageSetup.LineNumberingRestartMode = LineNumberingRestartMode.Continuous;
document.SaveToFile("result.docx",FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
C#/VB.NET: Change Security Permissions of PDF Documents
When you protect your PDF documents with passwords you can optionally specify a set of permissions. The permissions determine how users can interact with the file. For example, you can apply permissions to your document to prohibit the user from printing or using cut and paste operations. This article demonstrates how to change security permissions of PDF documents using Spire.PDF for .NET in C# and VB.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
Change Security Permissions of a PDF Document
The following are the steps to apply security permissions to a PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFileFile() method.
- Specify open password and permission password. The open password can be set to empty so that the generated document will not require a password to open.
- Encrypt the document with the open password and permission password, and set the security permissions using PdfDocument.Security.Encypt() method. This method takes PdfPermissionsFlags enumeration as a parameter, which defines user access permissions for an encrypted document.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;
namespace ChangeSecurityPermission
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a sample PDF file
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//Specify open password
string openPsd = string.Empty;
//Specify permission password
string permissionPsd = "e-iceblue";
//Encrypt the document with open password and permission password, and set the permissions and encryption key size
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy(openPsd, permissionPsd);
// Set the encryption algorithm to AES 128-bit
securityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_128;
// Allow printing of the document
securityPolicy.DocumentPrivilege.AllowPrint = true;
// Allow filling form fields in the document
securityPolicy.DocumentPrivilege.AllowFillFormFields = true;
doc.Encrypt(securityPolicy);
//Save the document to another PDF file
doc.SaveToFile("SecurityPermissions.pdf");
}
}
}

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.
How to split text into two columns and add line between them
Columns are widely used to set page layout, for which could split text into two or more columns so that the text could flow from one column to the next on the same page. Using Spire.Doc, we could achieve this feature and add a line between columns at the same time. This article is going to introduce how to split text into two columns and add line between them.
Note: please download the latest version of Spire.Doc and add Spire.Doc .dll as reference of Visual Studio.
Step 1: Create a new document and load from file
Document document = new Document();
document.LoadFromFile("S.docx");
Step 2: Add a column to section one, set the width of columns and the spacing between columns. Here we set width as 100f and spacing as 20f.
document.Sections[0].AddColumn(100f, 20f);
Step 3: Add a line between the two columns
document.Sections[0].PageSetup.ColumnsLineBetween = true;
Step 4: Save the document and launch to see effects
document.SaveToFile("result.docx",FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
Before adding the columns:

Effects:

Full Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
namespace Column
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("S.docx");
document.Sections[0].AddColumn(100f, 20f);
document.Sections[0].PageSetup.ColumnsLineBetween = true;
document.SaveToFile("result.docx",FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
C#/VB.NET: Extract Attachments from PDF
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.