C#: Add or Remove Captions in Word Documents
Captions are important elements in a Word document that enhance readability and organizational structure. They provide explanations and supplementary information for images, tables, and other content, improving the clarity and comprehensibility of the document. Captions are also used to emphasize key points and essential information, facilitating referencing and indexing of specific content. By using captions effectively, readers can better understand and interpret data and images within the document while quickly locating the desired information. This article will demonstrate how to use Spire.Doc for .NET to add or remove captions in a Word document within a C# project.
- Add Image Captions to a Word document in C#
- Add Table Captions to a Word document in C#
- Remove Captions from a Word document in C#
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Add Image Captions to a Word document in C#
To add captions to images in a Word document, you can achieve it by creating a paragraph, adding an image, and calling the method DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) to generate the caption with a specified name, numbering format, and caption position. The following are the detailed steps:
- Create an object of the Document class.
- Use the Document.AddSection() method to add a section.
- Add a paragraph using Section.AddParagraph() method.
- Use the Paragraph.AppendPicture(Image image) method to add a DocPicture image object to the paragraph.
- Use the DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) method to add a caption with numbering format as CaptionNumberingFormat.Number.
- Set the Document.IsUpdateFields property to true to update all fields.
- Use the Document.SaveToFile() method to save the resulting document.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace AddPictureCaption { internal class Program { static void Main(string[] args) { // Create a Word document object Document document = new Document(); // Add a section Section section = document.AddSection(); // Add a new paragraph and insert an image Paragraph pictureParagraphCaption = section.AddParagraph(); pictureParagraphCaption.Format.AfterSpacing = 10; DocPicture pic1 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\1.png")); pic1.Height = 100; pic1.Width = 100; // Add a caption to the image CaptionNumberingFormat format = CaptionNumberingFormat.Number; pic1.AddCaption("Image", format, CaptionPosition.BelowItem); // Add another paragraph and insert another image pictureParagraphCaption = section.AddParagraph(); DocPicture pic2 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\2.png")); pic2.Height = 100; pic2.Width = 100; // Add a caption to the second image pic2.AddCaption("Image", format, CaptionPosition.BelowItem); // Update all fields in the document document.IsUpdateFields = true; // Save to a docx document string result = "AddImageCaption.docx"; document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016); // Close and dispose of the document object to release resources document.Close(); document.Dispose(); } } }
Add Table Captions to a Word document in C#
To add captions to a table in a Word document, you can achieve this by creating the table and using the Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) method to generate a numbered caption. The steps involved are as follows:
- Create an object of the Document class.
- Use the Document.AddSection() method to add a section.
- Create a Table object and add it to the specified section in the document.
- Use the Table.ResetCells(int rowsNum, int columnsNum) method to set the number of rows and columns in the table.
- Add a caption to the table using the Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) method, specifying the caption numbering format as CaptionNumberingFormat.Number.
- Set the Document.IsUpdateFields property to true to update all fields.
- Use the Document.SaveToFile() method to save the resulting document.
- C#
using Spire.Doc; namespace AddTableCation { internal class Program { static void Main(string[] args) { // Create a Word document object Document document = new Document(); // Add a section Section section = document.AddSection(); // Add a table Table tableCaption = section.AddTable(true); tableCaption.ResetCells(3, 2); // Add a caption to the table tableCaption.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem); // Add another table and caption tableCaption = section.AddTable(true); tableCaption.ResetCells(2, 3); tableCaption.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem); // Update all fields in the document document.IsUpdateFields = true; // Save to a docx document string result = "AddTableCaption.docx"; document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016); // Close and dispose of the document object to release resources document.Close(); document.Dispose(); } } }
Remove Captions from a Word document in C#
Spire.Doc for .NET can also facilitate the removal of captions from an existing Word document. Here are the detailed steps:
- Create an object of the Document class.
- Use the Document.LoadFromFile() method to load a Word document.
- Create a custom method, named DetectCaptionParagraph(Paragraph paragraph), to determine if a paragraph contains a caption.
- Iterate through all the Paragraph objects in the document using a loop and utilize the custom method, DetectCaptionParagraph(Paragraph paragraph), to identify and delete paragraphs that contain captions.
- Use the Document.SaveToFile() method to save the resulting document.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace DeleteCaptions { internal class Program { static void Main(string[] args) { // Create a Word document object Document document = new Document(); // Load the example.docx file document.LoadFromFile("Data/Sample.docx"); Section section; // Iterate through all sections for (int i = 0; i < document.Sections.Count; i++) { section = document.Sections[i]; // Iterate through paragraphs in reverse order for (int j = section.Body.Paragraphs.Count - 1; j >= 0; j--) { // Check if the paragraph is a caption paragraph if (DetectCaptionParagraph(section.Body.Paragraphs[j])) { // If it's a caption paragraph, remove it section.Body.Paragraphs.RemoveAt(j); } } } // Save the document after removing captions string result = "RemoveCaptions.docx"; document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016); // Close and dispose of the document object to release resources document.Close(); document.Dispose(); } // Method to detect if a paragraph is a caption paragraph static bool DetectCaptionParagraph(Paragraph paragraph) { bool tag = false; Field field; // Iterate through the child objects in the paragraph for (int i = 0; i < paragraph.ChildObjects.Count; i++) { if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.Field) { // Check if the child object is of Field type field = (Field)paragraph.ChildObjects[i]; if (field.Type == FieldType.FieldSequence) { // Check if the Field type is FieldSequence, indicating a caption field type return true; } } } return tag; } } }
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.
Preserve Theme When Copying Sections from One Word Document to Another in C#
A theme is a set of colors, fonts, and effects that determines the overall look of your Word document. Suppose you have a document which is neat and stylish, you’d like to copy contents of a section to another document without losing the theme and style. You can clone the theme to destination file using CloneThemeTo method.
Step 1: Create a Document object and load a sample Word file.
Document doc = new Document(); doc.LoadFromFile("theme.docx");
Step 2: Create a new Word document.
Document newWord = new Document();
Step 3: Clone default style, theme, compatibility from the source file to destination document.
doc.CloneDefaultStyleTo(newWord); doc.CloneThemesTo(newWord); doc.CloneCompatibilityTo(newWord);
Step 4: Add the cloned section to destination document.
newWord.Sections.Add(doc.Sections[0].Clone());
Step 5: Save the file.
newWord.SaveToFile("result.docx", FileFormat.Docx);
Output:
Full Code:
using Spire.Doc; namespace PreserveTheme { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("theme.docx"); Document newWord = new Document(); doc.CloneDefaultStyleTo(newWord); doc.CloneThemesTo(newWord); doc.CloneCompatibilityTo(newWord); newWord.Sections.Add(doc.Sections[0].Clone()); newWord.SaveToFile("result.docx", FileFormat.Docx); } } }
How to Extract OLE Objects from a Word Document
Sometimes, we need to extract the OLE Objects that are embedded in a word document. With Spire.Doc, we can easily achieve this task with a few lines of code. This article explains how to extract the embedded PDF document and Excel workbook from a word document using Spire.Doc and C#.
Below is the screenshot of the word document:
Detail steps:
Step 1: Instantiate a Document object and load the word document.
Document doc = new Document(); doc.LoadFromFile("OleObject.docx");
Step 2: Traverse through the word document, find the Ole Objects, then get the Object type of each Ole Object to determine if the Ole Object is PDF document or Excel workbook and write the native data of the Ole object into a new PDF document or an Excel workbook.
//Traverse through all sections of the word document foreach (Section sec in doc.Sections) { //Traverse through all Child Objects in the body of each section foreach (DocumentObject obj in sec.Body.ChildObjects) { if (obj is Paragraph) { Paragraph par = obj as Paragraph; //Traverse through all Child Objects in Paragraph foreach (DocumentObject o in par.ChildObjects) { //Find the Ole Objects and Extract if (o.DocumentObjectType == DocumentObjectType.OleObject) { DocOleObject Ole = o as DocOleObject; string s = Ole.ObjectType; //If s == "AcroExch.Document.11", means it’s a PDF document if (s == "AcroExch.Document.11") { File.WriteAllBytes("Result.pdf", Ole.NativeData); } //If s == " Excel.Sheet.12", means it’s an Excel workbook else if (s == "Excel.Sheet.12") { File.WriteAllBytes("Result.xlsx", Ole.NativeData); } } } } } }
Below is the screenshot of the extracted PDF file and Excel workbook after running the code:
Full codes:
using System.IO; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Extract_OLEObjects_from_Word { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("OleObject.docx"); foreach (Section sec in doc.Sections) { foreach (DocumentObject obj in sec.Body.ChildObjects) { if (obj is Paragraph) { Paragraph par = obj as Paragraph; foreach (DocumentObject o in par.ChildObjects) { if (o.DocumentObjectType == DocumentObjectType.OleObject) { DocOleObject Ole = o as DocOleObject; string s = Ole.ObjectType; if (s == "AcroExch.Document.11") { File.WriteAllBytes("Result.pdf", Ole.NativeData); } else if (s == "Excel.Sheet.12") { File.WriteAllBytes("Result.xlsx", Ole.NativeData); } } } } } } } } }
How to Embed Media File in Word in C#, VB.NET
Spire.Doc supports to insert any type of file such as Excel, PDF, PowerPoint and etc, as OLE object into a Word document. In this article, you'll learn how to add a media file (audio or video) to a Word document using Spire.Doc in C#, VB.NET.
In the class of DocOleObject, a method named AppendOleObject(Stream oleStream, DocPicture olePicture, string fileExtension) is available for users to insert media file with the extension of mp3, mp4, avi or any other format into a Word document. The three parameters in this method represent:
- oleStream: The OLE file stream.
- olePicture: The image (icon) that is displayed in Word to show the OLE object.
- fileExtension: The file extension.
Code Snippet:
Step 1: Initialize a new instance of Document class and add a new section.
Document doc = new Document(); Section section = doc.AddSection();
Step 2: Add a new paragraph, append some formatted text into the paragraph.
Paragraph para1 = section.AddParagraph(); para1.AppendText("Double click the PLAY button to view the video file"); ParagraphStyle style1 = new ParagraphStyle(doc); style1.Name = "Style"; style1.CharacterFormat.FontName = "Calibri"; style1.CharacterFormat.FontSize = 15; style1.CharacterFormat.Bold = true; style1.CharacterFormat.TextColor = Color.Red; doc.Styles.Add(style1); para1.ApplyStyle(style1.Name);
Step 3: Add another paragraph, append a video file as OLE object into the paragraph.
Paragraph para2 = section.AddParagraph(); Stream s = File.OpenRead("media.mp4"); DocPicture pic = new DocPicture(doc); pic.LoadImage(Image.FromFile("button.png")); para2.AppendOleObject(s, pic, "mp4");
Step 4: Save the view the file.
doc.SaveToFile("Result.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("Result.docx");
Output:
Full Code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; using System.IO; namespace EmbedMediaFile { class Program { static void Main(string[] args) { //create a new Word document and insert section Document doc = new Document(); Section section = doc.AddSection(); //add a paragraph and append some text Paragraph para1 = section.AddParagraph(); para1.AppendText("Double click the PLAY button to view the video file"); ParagraphStyle style1 = new ParagraphStyle(doc); style1.Name = "Style"; style1.CharacterFormat.FontName = "Calibri"; style1.CharacterFormat.FontSize = 15; style1.CharacterFormat.Bold = true; style1.CharacterFormat.TextColor = Color.Red; doc.Styles.Add(style1); para1.ApplyStyle(style1.Name); //add another paragraph, append video file as OLE object in Word Paragraph para2 = section.AddParagraph(); Stream s = File.OpenRead("media.mp4"); DocPicture pic = new DocPicture(doc); pic.LoadImage(Image.FromFile("button.png")); para2.AppendOleObject(s, pic, "mp4"); //save and view the file doc.SaveToFile("Result.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("Result.docx"); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Imports System.IO Namespace EmbedMediaFile Class Program Private Shared Sub Main(args As String()) 'create a new Word document and insert section Dim doc As New Document() Dim section As Section = doc.AddSection() 'add a paragraph and append some text Dim para1 As Paragraph = section.AddParagraph() para1.AppendText("Double click the PLAY button to view the video file") Dim style1 As New ParagraphStyle(doc) style1.Name = "Style" style1.CharacterFormat.FontName = "Calibri" style1.CharacterFormat.FontSize = 15 style1.CharacterFormat.Bold = True style1.CharacterFormat.TextColor = Color.Red doc.Styles.Add(style1) para1.ApplyStyle(style1.Name) 'add another paragraph, append video file as OLE object in Word Dim para2 As Paragraph = section.AddParagraph() Dim s As Stream = File.OpenRead("media.mp4") Dim pic As New DocPicture(doc) pic.LoadImage(Image.FromFile("button.png")) para2.AppendOleObject(s, pic, "mp4") 'save and view the file doc.SaveToFile("Result.docx", FileFormat.Docx2010) System.Diagnostics.Process.Start("Result.docx") End Sub End Class End Namespace
C#/VB.NET: Insert Superscripts and Subscripts into Word
Superscripts or subscripts are characters positioned slightly above or below the normal line of text. They are commonly used in scientific formulas such as mathematical equations or chemical expressions. If you are creating a document containing scientific formulas, you most likely need to insert superscripts or subscripts. In this article, we will demonstrate how to insert superscripts and subscripts into Word in C# and VB.NET using Spire.Doc for .NET library.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Insert Superscripts and Subscripts into Word using C# and VB.NET
The following are the main steps to insert a superscript or subscript into a Word document using Spire.Doc for .NET:
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get the specific section through Document.Sections[sectionIndex] property.
- Add a paragraph to the section using Section.AddParagraph() method.
- Add normal text to the paragraph using Paragraph.AppendText() method.
- Add superscript or subscript text to the paragraph using Paragraph.AppendText() method.
- Apply superscript or subscript formatting to the superscript or subscript text through TextRange.CharacterFormat.SubSuperScript property.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace InsertSuperscriptAndSubscript { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.LoadFromFile("Sample.docx"); //Get the first section Section section = document.Sections[0]; //Add a paragraph to the section Paragraph paragraph = section.AddParagraph(); //Add normal text to the paragraph paragraph.AppendText("E = mc"); //Add superscript text to the paragraph TextRange superscriptText = paragraph.AppendText("2"); //Apply superscript formatting to the superscript text superscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript; //Start a new line paragraph.AppendBreak(BreakType.LineBreak); //Add normal text to the paragraph paragraph.AppendText("H"); //Add subscript text to the paragraph TextRange subscriptText = paragraph.AppendText("2"); //Apply subscript formatting to the subscript text subscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SubScript; //Add normal text to the paragraph paragraph.AppendText("O"); //Set font size for the text in the paragraph foreach (var item in paragraph.Items) { if (item is TextRange) { TextRange textRange = item as TextRange; textRange.CharacterFormat.FontSize = 36f; } } //Save the result document document.SaveToFile("InsertSuperscriptAndSubscript.docx", FileFormat.Docx2013); } } }
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.
Load Word with Marco and remove word Marco in C#
Word Marco is widely used to record the operation that needs to be done repeatedly and you can apply it with only a single click. It can save lots of time for you. It is not an easy work to load a word document with Macro in C# and sometimes, you need to remove the Marco in the word documents in C#. This article will focus on demonstrate how to load and save word document with Marco, and clear the Marco by using Spire.Doc in C# in simple lines of codes.
First, download Spire.Doc and install on your system. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer.
Then adds Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".
Here comes to the steps.
Step 1: Load and save the document with Marco. Spire.Doc for .NET supports .doc, .docx(Word 97-2003) document with macros and .docm(Word 2007 and Word 2010) document.
//Loading document with macros. document.LoadFromFile(@"D:\Macros.docm", FileFormat.Docm); //Save docm file. document.SaveToFile("Sample.docm", FileFormat.Docm);
Step 2: Clear the Marco in word document. With Spire.Doc, you only need one line of code to remove all the Marcos at one time.
//Removes the macros from the document document.ClearMacros(); //Save docm file. document.SaveToFile("Sample.docm", FileFormat.Docm);
Here comes to the screenshot which has removed the Marco in word document.
C#: Create Barcodes in a Word Document
Creating barcodes in a Word document is a useful technique for enhancing productivity and organization. Barcodes facilitate quick scanning and tracking, making them essential for businesses, events, and personal projects.
This article explains two methods for creating barcodes in a Word document using C#: one with barcode fonts via Spire.Doc for .NET API, and the other using a Barcode API alongside the Word API.
- Create Barcodes in a Word Document Using Barcode Fonts
- Create Barcodes in a Word Document Using Barcode API
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Create Barcodes in a Word Document Using Barcode Fonts
A barcode font is a typeface that converts alphanumeric data into a scannable format of bars and spaces. To use it, you typically need to install the font on your system and then format text in a Word document.
The steps to create barcodes in a Word document using barcode fonts are as follows:
- Download and install the desired barcode font on your computer.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific section and add a paragraph using Section.AddParagraph() method.
- Add text to the paragraph using Paragraph.AppendText() method.
- Apply the barcode font to the text using TextRange.CharacterFormat.FontName property.
- Set the font size and color for the text.
- Save the document to a different Word file.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace Name { class Program { static void Main(string[] args) { // Create a Document object Document document = new Document(); // Load a Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx"); // Get a specific section Section section = document.Sections[0]; // Add a paragraph Paragraph paragraph = section.AddParagraph(); // Append text to the paragraph TextRange txtRang = paragraph.AppendText("Hello,World"); // Apply barcode font to the text txtRang.CharacterFormat.FontName = "Code 128"; // Set the font size and text color txtRang.CharacterFormat.FontSize = 80; txtRang.CharacterFormat.TextColor = Color.Black; // Save the document to a different Word file document.SaveToFile("Barcode.docx", FileFormat.Docx); // Dispose resources document.Dispose(); } } }
Create Barcodes in a Word Document Using Barcode API
Spire.Barcode for .NET is a Barcode API that allows you to easily create a barcode with customized settings, such as barcode type, data, size, and color. You can install the library from NuGet using the following command.
PM> Install-Package Spire.Barcode
After the barcode image is created, you can then insert it to a Word document with the help of the Spire.Doc for .NET library.
The steps to create barcode in a Word document using a Barcode API are as follows:
- Install Spire.Barcode for .NET in your .NET program.
- Create a BarcodeSettings object.
- Specify the barcode type, data, width and other attributes using the properties under the BarcodeSettings object.
- Generate a barcode image based on the settings using BarCodeGenerator.GenerateImage() method.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific section and add a paragraph using Section.AddParagraph() method.
- Add the barcode image to the paragraph using Paragraph.AppendPicture() method.
- Save the document to a different Word file.
- C#
using Spire.Barcode; using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace Name { class Program { static void Main(string[] args) { // Create a BarcodeSettings object BarcodeSettings settings = new BarcodeSettings(); // Set barcode type settings.Type = BarCodeType.QRCode; // Set barcode data settings.Data2D = "Hello, World"; // Set the other attributes of the barcode settings.X = 1.5f; settings.QRCodeECL = QRCodeECL.H; settings.ShowTopText = false; settings.ShowText = false; // Create a BarCodeGenerator object BarCodeGenerator generator = new BarCodeGenerator(settings); // Generate a barcode image Image image = generator.GenerateImage(); // Create a Document object Document document = new Document(); // Load a Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx"); // Get a specific section Section section = document.Sections[0]; // Add a paragraph Paragraph paragraph = section.AddParagraph(); // Add the barcode image to the paragraph paragraph.AppendPicture(image); // Save the document to a different Word file document.SaveToFile("Barcode.docx", FileFormat.Docx); // Dispose resources document.Dispose(); } } }
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.
C#: Insert or Extract OLE Objects in Word
OLE, short for Object Linking and Embedding, is a powerful technology integrated into Microsoft Word and other Microsoft Office applications. Its primary purpose is to seamlessly integrate objects from external programs directly into your documents. These objects can range from simple images or charts to more complex items like spreadsheets, presentations, multimedia files and more. In this article, we will demonstrate how to insert OLE objects as well as extract OLE objects in Word documents in C# using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Insert OLE Objects in Word in C#
Spire.Doc for .NET offers the Paragraph.AppendOleObject(string pathToFile, DocPicture olePicture), OleObjectType type) method, which allows you to insert various types of documents (including Excel spreadsheets, PDF files, Word documents, PowerPoint presentations, and more) as OLE objects into a Word document.
The detailed steps are as follows:
- Create an instance of the Document class.
- Load a Word document using the Document.LoadFromFile() method.
- Get a specific section using the Document.Sections[index] property.
- Add a paragraph to the section using the Section.AddParagraph() method.
- Create an instance of the DocPicture class.
- Load an image that will be used as the icon of the embedded object using the DocPicture.LoadImage() method, and then set image width and height.
- Append an Excel spreadsheet as an OLE object to the paragraph using the Paragraph.AppendOleObject(string pathToFile, DocPicture olePicture, OleObjectType type) method.
- Repeat the above 4-7 steps to add more paragraphs and append more types of documents, like a PDF file, a PowerPoint presentation, and a Word document as OLE objects.
- Save the result file using the Document.SaveToFile() method.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace InsertOleObjects { internal class Program { static void Main(string[] args) { // Create an instance of the Document class Document doc = new Document(); // Load a Word document doc.LoadFromFile("Example.docx"); // Get the first section Section section = doc.Sections[0]; // Add a paragraph to the section Paragraph para1 = section.AddParagraph(); para1.AppendText("Excel File: "); // Load an image that will be used as the icon of the OLE object DocPicture picture1 = new DocPicture(doc); picture1.LoadImage("Excel-Icon.png"); picture1.Width = 50; picture1.Height = 50; // Append an Excel spreadsheet to the paragraph as an OLE object para1.AppendOleObject("Budget.xlsx", picture1, OleObjectType.ExcelWorksheet); // Add a paragraph to the section Paragraph para2 = section.AddParagraph(); para2.AppendText("PDF File: "); // Load an image that will be used as the icon of the OLE object DocPicture picture2 = new DocPicture(doc); picture2.LoadImage("PDF-Icon.png"); picture2.Width = 50; picture2.Height = 50; // Append a PDF file to the paragraph as an OLE object para2.AppendOleObject("Report.pdf", picture2, OleObjectType.AdobeAcrobatDocument); // Add a paragraph to the section Paragraph para3 = section.AddParagraph(); para3.AppendText("PPT File: "); // Load an image that will be used as the icon of the OLE object DocPicture picture3 = new DocPicture(doc); picture3.LoadImage("PPT-Icon.png"); picture3.Width = 50; picture3.Height = 50; // Append a PowerPoint presentation to the paragraph as an OLE object para3.AppendOleObject("Plan.pptx", picture3, OleObjectType.PowerPointPresentation); // Add a paragraph to the section Paragraph para4 = section.AddParagraph(); para4.AppendText("Word File: "); // Load an image that will be used as the icon of the OLE object DocPicture picture4 = new DocPicture(doc); picture4.LoadImage("Word-Icon.png"); picture4.Width = 50; picture4.Height = 50; // Append a Word document to the paragraph as an OLE object para4.AppendOleObject("Introduction.docx", picture4, OleObjectType.WordDocument); doc.SaveToFile("InsertOLE.docx", FileFormat.Docx2013); doc.Close(); } } }
Extract OLE Objects from Word in C#
To extract OLE objects from a Word document, you need to locate the OLE objects within the document first. Once located, identify the file format of each OLE object. Finally, save the data of each OLE object to a file in its original format.
The detailed steps are as follows:
- Create an instance of the Document class.
- Load a Word document using the Document.LoadFromFile() method.
- Iterate through all sections of the document.
- Iterate through all child objects in the body of each section.
- Identify the paragraphs within each section.
- Iterate through the child objects in each paragraph.
- Locate the OLE object within the paragraph.
- Determine the file format of the OLE object.
- Save the data of the OLE object to a file in its native file format.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; namespace InsertOrExtractOleObjects { internal class Program { static void Main(string[] args) { // Create an instance of the Document class Document doc = new Document(); // Load a Word document doc.LoadFromFile("InsertOLE.docx"); int i = 1; // Iterate through all sections of the Word document foreach (Section sec in doc.Sections) { // Iterate through all child objects in the body of each section foreach (DocumentObject obj in sec.Body.ChildObjects) { // Check if the child object is a paragraph if (obj is Paragraph par) { // Iterate through the child objects in the paragraph foreach (DocumentObject o in par.ChildObjects) { // Check if the child object is an OLE object if (o is DocOleObject ole) { string s = ole.ObjectType; string ext = ""; // Check if the OLE object is a PDF file if (s.StartsWith("AcroExch.Document")) { ext = ".pdf"; } // Check if the OLE object is an Excel spreadsheet else if (s.StartsWith("Excel.Sheet")) { ext = ".xlsx"; } // Check if the OLE object is a PowerPoint presentation else if (s.StartsWith("PowerPoint.Show")) { ext = ".pptx"; } // Check if the OLE object is a Word document else if (s.StartsWith("Word.Document")) { ext = ".docx"; } else { continue; } // Write the data of OLE into a file in its native format using (var file = System.IO.File.OpenWrite($"Output/OLE{i}{ext}")) { file.Write(ole.NativeData, 0, ole.NativeData.Length); } i++; } } } } } doc.Close(); } } }
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 Traverse a Document Tree
Document Tree Traversal
using System; using System.Collections.Generic; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Doc.Interface; using Spire.Doc.Collections; namespace ExtractText { class Program { static void Main(string[] args) { //Open a word document. Document document = new Document("Sample.doc"); IList<IDocumentObject> nodes = GetAllObjects(document); foreach (IDocumentObject node in nodes) { //Judge the object type. if (node.DocumentObjectType == DocumentObjectType.TextRange) { TextRange textNode = node as TextRange; Console.WriteLine(textNode.Text); } } } private static IList<IDocumentObject> GetAllObjects(Document document) { //Create a list. List<IDocumentObject> nodes = new List<IDocumentObject>(); //Create a new queue. Queue<ICompositeObject> containers = new Queue<ICompositeObject>(); //Put the document objects in the queue. containers.Enqueue(document); while (containers.Count > 0) { ICompositeObject container = containers.Dequeue(); DocumentObjectCollection docObjects = container.ChildObjects; foreach (DocumentObject docObject in docObjects) { nodes.Add(docObject); //Judge the docObject. if (docObject is ICompositeObject) { containers.Enqueue(docObject as ICompositeObject); } } } return nodes; } } }
Imports System Imports System.Collections.Generic Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports Spire.Doc.Interface Imports Spire.Doc.Collections Module Module1 Sub Main() 'Open a word document. Dim document As New Document("Sample.doc") Dim nodes As IList(Of IDocumentObject)() = GetAllObjects(document) Dim containers As New Queue(Of ICompositeObject)() For Each node As IDocumentObject In nodes 'Judge the object type. If (node.DocumentObjectType = DocumentObjectType.TextRange) Then Dim textNode As TextRange = node Console.WriteLine(textNode.Text) End If Next End Sub Function GetAllObjects(ByVal document As Document) As IList(Of IDocumentObject) 'Create a list. Dim nodes As New List(Of IDocumentObject)() 'Create a new queue. Dim containers As New Queue(Of ICompositeObject)() 'Put the document objects in the queue. containers.Enqueue(document) While (containers.Count > 0) Dim container As ICompositeObject = containers.Dequeue() Dim docObjects As DocumentObjectCollection = container.ChildObjects For Each docObject As DocumentObject In docObjects nodes.Add(docObject) 'Judge the docObject. If TypeOf docObject Is ICompositeObject Then containers.Enqueue(TryCast(docObject, ICompositeObject)) End If Next End While Return nodes End Function End Module