In this blog you will see how to apply color for the contents inside the run using Open XML SDK 2.0 Assembly:
DocumentFormat.OpenXml.dll
Namesapces:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing; Code:
C#
using System;using System.Collections.Generic;using System.Linq;using System.Text;using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessing;using System.IO.Packaging; namespaceOpenXMLConsole{ classProgram { staticvoidMain(string[] args) { string path = @"E:\OpenXMLColor.docx"; //// Creates the new instance of the WordprocessingDocument class from the specified file //// WordprocessingDocument.Open(String, Boolean) method //// Parameters: //// string path - path is a string which contains the path of the document //// bool isEditable using (WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document)) { //// Defines the MainDocumentPart MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart(); mainDocumentPart.Document = new Document(); Body body = mainDocumentPart.Document.AppendChild(new Body()); Paragraph para = body.AppendChild(new Paragraph()); Run run =para.AppendChild(new Run()); RunProperties runProperties = run.AppendChild(new RunProperties()); Color color = new Color(); color.Val = "CC0000"; runProperties.AppendChild(color); run.AppendChild(new Text("Welcome!!!!!")); mainDocumentPart.Document.Save(); } } }}
Ali HassanPosted Dec 29, 2020, 1:26 AM EST
Hello! Vijai Anand Ramalingam, i m working with word !!! plz guide me how to color background cell in the same way you have changed text color !!!