Knowledgebase (2330)
Children categories
How to fill the table cell with color in PowerPoint document in C#
2014-10-13 08:24:52 Written by KoohjiA table provides a visual grouping of information and gives more convenience for writer to modify and query data in table. In particular when you have a table with colorful cells, your document would be more attractive. With the help of Spire.Presentation, developers can easily add tables and set table styles in PowerPoint document. This tutorial shows you how to fill the table cells with color in C#.
Step 1: Create a presentation document and load the file from disk.
Presentation presentation = new Presentation();
presentation.LoadFromFile("sample.pptx");
Step 2: Fill the table cell with color. You can fill all the cells or only fill one single row of cell in table with color.
foreach (TableRow row in table.TableRows)
{
foreach (Cell cell in row)
{
cell.FillFormat.FillType = FillFormatType.Solid;
cell.FillFormat.SolidColor.Color = Color.Green;
}
}
Step 3: Save the presentation documents to file.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
Effective screenshot for fill the color in all the table cells:

Effective screenshot for fill the color for the first row of table cell:

Full codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace colorfilltablecell
{
class Program
{
static void Main(string[] args)
{
Presentation presentation = new Presentation();
presentation.LoadFromFile("sample.pptx");
ITable table = null;
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
table = (ITable)shape;
foreach (TableRow row in table.TableRows)
{
//TableRow row = table.TableRows[0];
foreach (Cell cell in row)
{
cell.FillFormat.FillType = FillFormatType.Solid;
cell.FillFormat.SolidColor.Color = Color.Green;
}
}
}
}
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
}
}
}
When you type in a document, Word automatically counts the number of pages and words in your document and displays them on the status bar – Word Count, at the bottom of the workspace. But how can we get the number of words, characters in an existing Word document through programming? This article aims to give you a simple solution offered by Spire.Doc.
Test file:

Detailed Steps for Getting the Number of Words and Characters
Step 1: Create a new instance of Spire.Doc.Document class and load the test file.
Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx2010);
Step 2: Display the number of words, characters including or excluding spaces on console.
Console.WriteLine("CharCount: " + doc.BuiltinDocumentProperties.CharCount);
Console.WriteLine("CharCountWithSpace: " + doc.BuiltinDocumentProperties.CharCountWithSpace);
Console.WriteLine("WordCount: " + doc.BuiltinDocumentProperties.WordCount);
Output:

Full Code:
using Spire.Doc;
using System;
namespace CountNumber
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx2010);
Console.WriteLine("CharCount: " + doc.BuiltinDocumentProperties.CharCount);
Console.WriteLine("CharCountWithSpace: " + doc.BuiltinDocumentProperties.CharCountWithSpace);
Console.WriteLine("WordCount: " + doc.BuiltinDocumentProperties.WordCount);
Console.ReadKey();
}
}
}
Imports Spire.Doc
Namespace CountNumber
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document()
doc.LoadFromFile("test.docx", FileFormat.Docx2010)
Console.WriteLine("CharCount: " + doc.BuiltinDocumentProperties.CharCount)
Console.WriteLine("CharCountWithSpace: " + doc.BuiltinDocumentProperties.CharCountWithSpace)
Console.WriteLine("WordCount: " + doc.BuiltinDocumentProperties.WordCount)
Console.ReadKey()
End Sub
End Class
End Namespace
Get alias, tag and id of content controls in a Word document in C#
2014-10-11 06:28:55 Written by AdministratorContent controls provide a way for you to design documents. When you add a content control to a document, the control is identified by a border, a title, and temporary text that can provide instructions to the user. According to Microsoft, content controls mainly benefit from two features:
- Prevent users from editing or deleting protected sections of a document.
- Bind parts of a document or template to data. You can bind content controls to database fields, managed objects in the .NET Framework, XML elements that are stored in the document, and other data sources.
Therefore, it is necessary for developers to get the properties of content controls when dealing content controls at run time. This article illustrates how to get all controls and their properties including alias, id and tag via Spire.Doc.
Firstly, check the test file that contains six content controls distributed in lines and a table. By default, the border and the title of the control do not appear if we don't click the protected section.
Test File:

Main Steps:
Step 1: Create a new Word document and load the test file.
Step 2: Create two lists to store tags which are distributed in lines and a table separately. Here, each content control will be identified by tag.
Step 3: Use foreach sentence to get all tags in the Word document.
Full Code:
static void Main(string[] args)
{
using (Document document = new Document(@"..\..\TestData\test.docx"))
{
StructureTags structureTags = GetAllTags(document);
List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;
string alias = tagInlines[0].SDTProperties.Alias;
decimal id = tagInlines[0].SDTProperties.Id;
string tag = tagInlines[0].SDTProperties.Tag;
List<StructureDocumentTag> tags = structureTags.tags;
alias = tags[0].SDTProperties.Alias;
id = tags[0].SDTProperties.Id;
tag = tags[0].SDTProperties.Tag;
}
}
static StructureTags GetAllTags(Document document)
{
StructureTags structureTags = new StructureTags();
foreach (Section section in document.Sections)
{
foreach (DocumentObject obj in section.Body.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
{
if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
{
structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
}
}
}
else if (obj.DocumentObjectType == DocumentObjectType.Table)
{
foreach (TableRow row in (obj as Table).Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (DocumentObject cellChild in cell.ChildObjects)
{
if (cellChild.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
{
structureTags.tags.Add(cellChild as StructureDocumentTag);
}
else if (cellChild.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject pobj in (cellChild as Paragraph).ChildObjects)
{
if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
{
structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
}
}
}
}
}
}
}
}
}
return structureTags;
}
public class StructureTags
{
List<StructureDocumentTagInline> m_tagInlines;
public List<StructureDocumentTagInline> tagInlines
{
get
{
if (m_tagInlines == null)
m_tagInlines = new List<StructureDocumentTagInline>();
return m_tagInlines;
}
set
{
m_tagInlines = value;
}
}
List<StructureDocumentTag> m_tags;
public List<StructureDocumentTag> tags
{
get
{
if (m_tags == null)
m_tags = new List<StructureDocumentTag>();
return m_tags;
}
set
{
m_tags = value;
}
}
}
Effect Screenshot:
Content controls in lines

Content controls in table
