Get Started
Documentation

CreateStyle

CreateStyle(sStyleName, sTypeopt) → {ApiStyle}

Create a new style with the specified type and name. If there is a style with the same name it will be replaced with a new one.

Parameters:

Name Type Attributes Default Description
sStyleName string
The name of the style which will be created.
sType StyleType <optional>
"paragraph" The document element which the style will be applied to.

Returns:

Type
ApiStyle

Example

Copy code
builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph, oParaPr;
var oHeading1Style = oDocument.CreateStyle("Heading 1", "paragraph");
oParaPr = oHeading1Style.GetParaPr();
oParaPr.SetKeepNext(true);
oParaPr.SetKeepLines(true);
oParaPr.SetSpacingAfter(240);
oTextPr = oHeading1Style.GetTextPr();
oTextPr.SetColor(0xff, 0x68, 0x00, false);
oTextPr.SetFontSize(40);
oTextPr.SetFontFamily("Calibri Light");
oParagraph = oDocument.GetElement(0);
oParagraph.SetStyle(oHeading1Style);
oParagraph.AddText("This is a heading with a style created above");
oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is just a text.");
oDocument.Push(oParagraph);
builder.SaveFile("docx", "SetStyle.docx");
builder.CloseFile();

Resulting document