Wednesday, 03 January 2024 01:14

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.

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();
        }
    }
}

C#: Add or Remove Captions in Word documents

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();
        }
    }
}

C#: Add or Remove Captions in Word documents

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;
        }
    }
}

C#: Add or Remove Captions in Word documents

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.

Published in Others

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:

Preserve Theme When Copying Sections from One Word Document to Another in C#

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);

        }
    }
}
Published in Others
Thursday, 08 September 2016 03:50

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:

How to Extract OLE Objects from a 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:

How to Extract OLE Objects from a Word Document

How to Extract OLE Objects from a Word Document

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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Published in Others
Wednesday, 12 August 2015 02:23

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:

How to Embed Media File in Word in C#, VB.NET

Full Code:

[C#]
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");

        }
    }
}
[VB.NET]
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
Published in Others

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);
        }
    }
}

C#/VB.NET: Insert Superscripts and Subscripts into Word

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.

Published in Others

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);

Load and save word with Macro

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.

Remove Macro

Published in Others
Monday, 29 October 2012 01:11

Create Barcode in Word in C#, VB.NET

Barcode is formed by several blanks and black stripes with a specified order. It presents the brief information of one product, including manufacturer, product name, category, price etc. At present, barcode is widely used in commodity circulation. Also, people can create barcode in Word and print.

Spire.Doc for .NET, a stand-alone professional .NET Word component, enables users to create barcode in Word by using C#, VB.NET. So, this guide will show you the method about how to create via Spire.Doc for .NET.

At first, you need to install barcode font on your system. Then, add barcode text through invoking p.AppendText(string) method and then set text format properties for it, such as FontName, FontSize, TextColor etc. After running, we can get result as following screenshot.

Create Word Barcode

Download and install Spire.Doc for .NET and then use the following code to create barcode in Word.

[C#]
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordBarcode
{
    class BarCode
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();
            Paragraph p = document.AddSection().AddParagraph();

            //Add Barcode and Set Format
            TextRange txtRang = p.AppendText("H63TWX11072");
            txtRang.CharacterFormat.FontName = "C39HrP60DlTt";
            txtRang.CharacterFormat.FontSize = 80;
            txtRang.CharacterFormat.TextColor = Color.SeaGreen;

            //Save and Launch
            document.SaveToFile("barcode.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("barcode.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace WordBarcode
    Friend Class BarCode
        Shared Sub Main(ByVal args() As String)
            'Create Document
            Dim document As New Document()
            Dim p As Paragraph = document.AddSection().AddParagraph()

            'Add Barcode and Set Format
            Dim txtRang As TextRange = p.AppendText("H63TWX11072")
            txtRang.CharacterFormat.FontName = "C39HrP60DlTt"
            txtRang.CharacterFormat.FontSize = 80
            txtRang.CharacterFormat.TextColor = Color.SeaGreen

            'Save and Launch
            document.SaveToFile("barcode.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("barcode.docx")
        End Sub
    End Class
End Namespace

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.

Published in Others
Thursday, 23 August 2012 06:30

Insert OLE Object in Word in C#, VB.NET

Word OLE (Object Linking and Embedding) object is used to make contents, created in one program, available in Word document. For example, users can insert an Excel worksheet in a Word document.

Both Linked object and Embedded object can be used between Word and other programs. The data of Embedded objects is saved in Word and should be updated manually, while data of Linked object remains as separate file and will be updated when source data is changed.

Spire.Doc for .NET , a professional component to manipulate Word documents with .NET, enables users to insert OLE objects in Word by using C#/VB.NET. This guide will show you how to insert one kind of OLE objects, Linked objects (an Excel Worksheet) in Word.

Users can invoke paragraph.AppendOleObject(string pathToFile, olePicture, OleObjectType) method to insert OLE objects in Word. The parameters, olePicture and OleObjectType are properties of DocOleObject class which is provided by Spire.Doc for .NET.

The following steps present details to insert OLE Objects in Word. The following demonstrate a document before insert the OLE Object into Word, and at the bottom, you can find the result screenshot after inserting. Before starting with the steps, download and install Spire.Doc for .NET on system.

Insert Word OLE Objects

Code Detail:

Step 1: Define a GetExcelImage(string ExcelFile) method to get olePicture. Actually, the olePicture is image of data information in original Excel worksheet. The image is generated from Excel through Spire.XLS for .NET that will be shown in documents after inserting OLE object in Word. Double click this picture and you can get the original worksheet.

[C#]
 
private static Image GetExcelImage(String ExcelFile)
 {
  //Load Excel File
  Workbook workbook = new Workbook();
  workbook.LoadFromFile(ExcelFile);
  Worksheet sheet = workbook.Worksheets[0];

  //Set Image Range
  int lastRow = sheet.LastRow;
  int lastColumn = sheet.LastColumn;
  return workbook.Worksheets[0].SaveToImage(1, 1, lastRow, lastColumn);
}
[VB.NET]
   
Private Shared Function GetExcelImage(ByVal ExcelFile As String) As Image
  'Load Excel File
  Dim workbook As New Workbook()
  workbook.LoadFromFile(ExcelFile)
  Dim sheet As Worksheet = workbook.Worksheets(0)

  'Set Image Range
  Dim lastRow As Integer = sheet.LastRow
  Dim lastColumn As Integer = sheet.LastColumn
  Return workbook.Worksheets(0).SaveToImage(1, 1, lastRow, lastColumn)
End Function
 

Step 2: Insert OLE object. After adding paragraph in Word document, declare a new DocPicture. Then, use GetExcelImage(string ExcelFile) method which is defined in step 1 to get image source and then use picture.LoadImage(Image) method to load this image. Finally, insert OLE objects.

[C#]
       
para = mysec.AddParagraph();
DocPicture picture = new DocPicture(mydoc);
Image image = GetExcelImage(@"E:\work\Documents\ExcelFiles\Customers.xlsx");//Get Image Source
picture.LoadImage(image);//Load Image
DocOleObject obj = para.AppendOleObject(@"E:\work\Documents\ExcelFiles\Customers.xlsx", picture, Spire.Doc.Documents.OleObjectType.ExcelWorksheet);
[VB.NET]
  
'Insert OLE Object
para = mysec.AddParagraph()
Dim picture As New DocPicture(mydoc)
Dim image As Image = GetExcelImage("E:\work\Documents\ExcelFiles\Customers.xlsx") 'Get Image Source
picture.LoadImage(image) 'Load Image
Dim obj As DocOleObject = para.AppendOleObject("E:\work\Documents\ExcelFiles\Customers.xlsx", picture, Spire.Doc.Documents.OleObjectType.ExcelWorksheet)

After this coding, you can run this application and a result will show up as below:

Insert Word OLE Objects

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.

Published in Others
Tuesday, 28 December 2010 06:45

How to Traverse a Document Tree

Spire.Doc represents a document as a tree, every document element is a node of that tree. Some nodes such as section, paragraph and table may have many child nodes. For example, a section node has several paragraph nodes, a paragraph node has many text nodes and each row is the child node of a table node. And other nodes have no child node, such as text-range, image, form-field.
If a node has child nodes, it should be an instance of Spire.Doc.Interface.ICompositeObject.
If you want to operate all the nodes, you can use the tree navigation method to visit each node.

Document Tree Traversal

The following example demonstrates how to traverse a document tree to collect all nodes and ouput the text of all text-range nodes.
[C#]
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;
        }
    }
}
          
[VB.NET]
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
          
Published in Others