Spire.Doc for .NET (337)
Children categories
Split a Word document into multiple documents by section break in C#
2016-07-25 08:45:33 Written by KoohjiIn Word, we can split a word document in an easiest way - open a copy of the original document, delete the sections that we don’t want and then save the remains to local drive. But doing this section by section is rather cumbersome and boring. This article will explain how we can use Spire.Doc for .NET to programmatically split a Word document into multiple documents by section break instead of copying and deleting manually.
Detail steps and code snippets:
Step 1: Initialize a new word document object and load the original word document which has two sections.
Document document = new Document();
document.LoadFromFile("Test.docx");
Step 2: Define another new word document object.
Document newWord;
Step 3: Traverse through all sections of the original word document, clone each section and add it to a new word document as new section, then save the document to specific path.
for (int i = 0; i < document.Sections.Count; i++)
{
newWord = new Document();
newWord.Sections.Add(document.Sections[i].Clone());
newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i));
}
Run the project and we'll get the following output:

Full codes:
using System;
using Spire.Doc;
namespace Split_Word_Document
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("Test.doc");
Document newWord;
for (int i = 0; i < document.Sections.Count; i++)
{
newWord = new Document();
newWord.Sections.Add(document.Sections[i].Clone());
newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i));
}
}
}
}
When we want to manage the pagination for the paragraphs, we can insert a page break directly. But later we may find that it is hard to add or remove text above the page break and then we have to remove the whole page break. With Microsoft word, we can also use the Paragraph dialog to manage the pagination flexible for the word paragraph as below:

We have already shown you how to insert page break to the word document, this article we will show you how to manage the pagination by using the Paragraph.Format offered by Spire.Doc. Here comes to the steps of how to manage the pagination for the word document paragraph in C#:
Step 1: Create a new word document and load the document from file.
Document doc = new Document();
doc.LoadFromFile("sample.docx");
Step 2: Get the first section and the paragraph we want to manage the pagination
Section sec = doc.Sections[0]; Paragraph para = sec.Paragraphs[4];
Step 3: Set the pagination format as Format.PageBreakBefore for the checked paragraph.
para.Format.PageBreakBefore = true;
Step 4: Save the document to file.
doc.SaveToFile("result.docx",FileFormat.Docx2010);
Effective screenshot after managing the pagination for the word document:

Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
namespace ManagePage
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("sample.docx");
Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[4];
para.Format.PageBreakBefore = true;
doc.SaveToFile("result.docx", FileFormat.Docx2010);
}
}
}
A table in Microsoft Word can contain a variety of elements, including text, images, and many more. Sometimes, you may want to insert images into a table to present some information or extract images from a table for use in other documents. This article will teach you how to insert or extract images from tables in Word documents in C# and VB.NET 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 Images into a Table in a Word Document in C# and VB.NET
Spire.Doc provides the TableCell.Paragraphs[int].AppendPicture() method which enables you to add an image to a specific table cell. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document by its index through Document.Sections[int] property.
- Get a specific table in the section by its index through Section.Tables[int] property.
- Access a specific cell to which you want to add an image through Table.Row[int].Cells[int] property.
- Add an image to a specific paragraph of the cell using TableCell.Paragraphs[int].AppendPicture() method.
- Set image width and height through DocPicture.Width and DocPicture.Height properties.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertImagesIntoTable
{
class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Table.docx");
//Get the first section of the document
Section section = doc.Sections[0];
//Get the first table from the first section
Table table = (Table)section.Tables[0];
//Add an image to the 3rd cell of the second row in the table
TableCell cell = table.Rows[1].Cells[2];
DocPicture picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("doc.png"));
//Set image width and height
picture.Width = 100;
picture.Height = 100;
//Add an image to the 3rd cell of the 3rd row in the table
cell = table.Rows[2].Cells[2];
picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("xls.png"));
//Set image width and height
picture.Width = 100;
picture.Height = 100;
//Save the result document
doc.SaveToFile("AddImageToTable.docx", FileFormat.Docx2013);
}
}
}

Extract Images from a Table in a Word Document in C# and VB.NET
To extract images from a table, you need to iterate through all rows in the tale, all cells in each row, all paragraphs in each cell and all child objects in each paragraph, then find the objects that are of DocPicture type and call DocPicture.Image.Save() method to save them to a specified file path. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document by its index through Document.Sections[int] property.
- Get a specific table in the section by its index through Section.Tables[int] property.
- Iterate through all rows in the table.
- Iterate through all cells in each row.
- Iterate through all paragraphs in each cell.
- Iterate through all child objects in each paragraph.
- Check if the current child object is of DocPicture type.
- If the result is true, typecast the object as DocPicture and call DocPicture.Image.Save() method to save the image to a specified file path.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing.Imaging;
namespace ExtractImagesFromTable
{
class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("AddImageToTable.docx");
//Get the first section of the document
Section section = doc.Sections[0];
//Get the first table from the first section
Table table = (Table)section.Tables[0];
int index = 0;
string imageName = null;
//Iterate through all rows in the table
for (int i = 0; i < table.Rows.Count; i++)
{
//Iterate through all cells in each row
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
//Iterate through all paragraphs in each cell
foreach (Paragraph para in table[i, j].Paragraphs)
{
//Iterate through all child objects in each paragraph
foreach (DocumentObject obj in para.ChildObjects)
{
//Check if the current child object is of DocPicture type
if (obj is DocPicture)
{
//Save the images to a specified file path
imageName = string.Format(@"images\TableImage-{0}.png", index);
(obj as DocPicture).Image.Save(imageName, ImageFormat.Png);
index++;
}
}
}
}
}
}
}
}

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.