Spire.Doc for .NET (338)
Children categories
How to Delete Rows and Columns from a Word Table in C#, VB.NET
2016-12-22 09:23:02 Written by AdministratorAdding rows and columns are common tasks in Word table processing, on the contrary, sometimes we also have the requirement of deleting rows or columns from a table. This article demonstrates how to delete a row and a column from an existing Word table using Spire.Doc.
Below is the screenshot of the original table. Afterwards, we will remove the colored row and column from the table.

Detail steps:
Step 1: Instantiate a Document object and load the Word document.
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
Step 2: Get the table from the document.
Table table = doc.Sections[0].Tables[0] as Table;
Step 3: Delete the third row from the table.
table.Rows.RemoveAt(2);
Step 4: Delete the third column from the table.
for (int i = 0; i < table.Rows.Count; i++)
{
table.Rows[i].Cells.RemoveAt(2);
}
Step 5: Save the document.
doc.SaveToFile("result.docx", FileFormat.Docx2013);
Output:

Full code:
using Spire.Doc;
namespace Delete_Rows_and_Columns
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
Table table = doc.Sections[0].Tables[0] as Table;
table.Rows.RemoveAt(2);
for (int i = 0; i < table.Rows.Count; i++)
{
table.Rows[i].Cells.RemoveAt(2);
}
doc.SaveToFile("result.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Namespace Delete_Rows_and_Columns
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document()
doc.LoadFromFile("Sample.docx")
Dim table As Table = TryCast(doc.Sections(0).Tables(0), Table)
table.Rows.RemoveAt(2)
For i As Integer = 0 To table.Rows.Count - 1
table.Rows(i).Cells.RemoveAt(2)
Next
doc.SaveToFile("result.docx", FileFormat.Docx2013);
End Sub
End Class
End Namespace
Embed private font into Word document when save as .docx file format
2016-11-09 08:49:02 Written by KoohjiNow Spire.Doc supports to embed private fonts from font files into Word document when save as .docx file format. This article will show you the detail steps of how to accomplish this task by using Spire.Doc.
For demonstration, we used a font file DeeDeeFlowers.ttf.

In the following part, we will embed font from above file into a Word document and use it to create text.
Step 1: Create a blank Word document.
Document document = new Document();
Step 2: Add a section and a paragraph to the document.
Section section = document.AddSection(); Paragraph p = section.AddParagraph();
Step 3: Append text to the paragraph, then set the font name and font size for the text.
TextRange range = p.AppendText("Let life be beautiful like summer flowers\n"
+"Life, thin and light-off time and time again\n"
+ "Frivolous tireless");
range.CharacterFormat.FontName = "DeeDeeFlowers";
range.CharacterFormat.FontSize = 20;
Step 4: Allow embedding font in document by setting the Boolean value of EmbedFontsInFile property to true.
document.EmbedFontsInFile = true;
Step 5: Embed private font from font file into the document.
document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf"));
Step 6: Save as .docx file format.
document.SaveToFile("result.docx", FileFormat.Docx);
After running the code, we'll get the following output:

Full code:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Embed_private_font_into_Word
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
Paragraph p = section.AddParagraph();
TextRange range = p.AppendText("Let life be beautiful like summer flowers\n"
+"Life, thin and light-off time and time again\n"
+ "Frivolous tireless");
range.CharacterFormat.FontName = "DeeDeeFlowers";
range.CharacterFormat.FontSize = 20;
document.EmbedFontsInFile = true;
document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf"));
document.SaveToFile("result.docx", FileFormat.Docx);
}
}
}
Sometimes we only need to get the text from the word document for other use when we deal with the word documents with large amount of information. With the help of Spire.Doc, we have already demonstrated how to extract the text from the word document by traverse every paragraph on the word document and then append the text accordingly. This article will show you how to use the method of doc.GetText() to extract the text directly from the word documents with texts, images and tables. It is more convenient for developers to extract the text from the word document from code.
Firstly, view the sample word document which will be extracted the text firstly:

Step 1: Create a word instance and load the source word document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
Step 2: Invoke the doc.GetText() method to get all the texts from the word document.
string s = doc.GetText();
Step 3: Create a New TEXT File to Save Extracted Text.
File.WriteAllText("Extract.txt", s.ToString());
Effective screenshot after get all the text from the word document:

Full codes:
using Spire.Doc;
using System.IO;
namespace GetText
{
class WordText
{
public void GetText()
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
string s = doc.GetText();
File.WriteAllText("Extract.txt", s.ToString());
}
}
}