Knowledgebase (2311)
Children categories
Math equations in Word documents are essential tools for expressing mathematical concepts and relationships. Whether you are writing an academic paper, a scientific report, or any other document involving mathematical content, incorporating math equations can greatly enhance your ability to convey complex mathematical concepts and improve the visual appeal and professionalism of your document. In this article, we will explain how to insert math equations into 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 Math Equations into a Word Document in C# and VB.NET
Spire.Doc for .NET allows generating math equations from LaTeX code and MathML code using OfficeMath.FromLatexMathCode(string latexMathCode) and OfficeMath.FromMathMLCode(string mathMLCode) methods. The detailed steps are as follows:
- Create two string arrays from LaTeX code and MathML code.
- Create a Document instance and add a section to it using Document.AddSection() method.
- Iterate through each LaTeX code in the string array.
- Create a math equation from the LaTeX code using OfficeMath.FromLatexMathCode(string latexMathCode) method.
- Add a paragraph to the section, then add the math equation to the paragraph using Paragraph.Items.Add() method.
- Iterate through each MathML code in the string array.
- Create a math equation from the MathML code using OfficeMath.FromMathMLCode(string mathMLCode) method.
- Add a paragraph to the section, then add the math equation to the paragraph using Paragraph.Items.Add() method.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.OMath;
namespace AddMathEquations
{
internal class Program
{
static void Main(string[] args)
{
//Create a string array from LaTeX code
string[] latexMathCode = {
"x^{2}+\\sqrt{x^{2}+1}=2",
"\\cos (2\\theta) = \\cos^2 \\theta - \\sin^2 \\theta",
"k_{n+1} = n^2 + k_n^2 - k_{n-1}",
"\\frac {\\frac {1}{x}+ \\frac {1}{y}}{y-z}",
"\\int_0^ \\infty \\mathrm {e}^{-x} \\, \\mathrm {d}x",
"\\forall x \\in X, \\quad \\exists y \\leq \\epsilon",
"\\alpha, \\beta, \\gamma, \\Gamma, \\pi, \\Pi, \\phi, \\varphi, \\mu, \\Phi",
"A_{m,n} = \\begin{pmatrix} a_{1,1} & a_{1,2} & \\cdots & a_{1,n} \\\\ a_{2,1} & a_{2,2} & \\cdots & a_{2,n} \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\ a_{m,1} & a_{m,2} & \\cdots & a_{m,n} \\end{pmatrix}",
};
//Create a string array from MathML code
string[] mathMLCode = {
"<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>a</mi><mo>≠</mo><mn>0</mn></math>",
"<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>a</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>b</mi><mi>x</mi><mo>+</mo><mi>c</mi><mo>=</mo><mn>0</mn></math>",
"<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>x</mi><mo>=</mo><mrow><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>",
};
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.AddSection();
//Add a paragraph to the section
Paragraph textPara = section.AddParagraph();
textPara.AppendText("Creating Equations from LaTeX Code");
textPara.ApplyStyle(BuiltinStyle.Heading1);
textPara.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Iterate through each LaTeX code in the string array
for (int i = 0; i < latexMathCode.Length; i++)
{
//Create a math equation from the LaTeX code
OfficeMath officeMath = new OfficeMath(doc);
officeMath.FromLatexMathCode(latexMathCode[i]);
//Add the math equation to the section
Paragraph paragraph = section.AddParagraph();
paragraph.Items.Add(officeMath);
section.AddParagraph();
}
section.AddParagraph();
//Add a paragraph to the section
textPara = section.AddParagraph();
textPara.AppendText("Creating Equations from MathML Code");
textPara.ApplyStyle(BuiltinStyle.Heading1);
textPara.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Iterate through each MathML code in the string array
for (int j = 0; j < mathMLCode.Length; j++)
{
//Create a math equation from the MathML code
OfficeMath officeMath = new OfficeMath(doc);
officeMath.FromMathMLCode(mathMLCode[j]);
//Add the math equation to the section
Paragraph paragraph = section.AddParagraph();
paragraph.Items.Add(officeMath);
section.AddParagraph();
}
//Save the result document
doc.SaveToFile("AddMathEquations.docx", FileFormat.Docx2013);
doc.Dispose();
}
}
}

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.
A text box is an element you can insert and position anywhere in a document. MS Word provides several pre-formatted text boxes, and you can also create custom text boxes with unique appearances to grab the reader's attention. In this article, you will learn how to programmatically insert or remove a text box in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.1.3</version>
</dependency>
</dependencies>
Insert a Text Box in a Word Document
Spire.Doc for Java provides the Paragraph.appendTextBox(float width, float height) method to insert a text box in a specified paragraph. The detailed steps are as follows.
- Create a Document instance, and then load a sample Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method, and then add a paragraph to the section using Section.addParagraph() method.
- Add a text box to the paragraph using Paragraph.appendTextBox(float width, float height) method.
- Get the format of the text box using TextBox.getFormat() method, and then set the text box's wrapping type, position, border color and fill color using the methods under TextBoxFormat Class.
- Add a paragraph to the text box using TextBox.getBody().addParagraph() method, and then insert an image to the paragraph using Paragraph.appendPicture() method.
- Insert text to the text box using Paragraph.appendText() method, and then set the text font.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextBox;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class InsertTextbox {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("E:\\Files\\Ralph.docx");
//Append a text box and set its wrapping style
TextBox tb = doc.getSections().get(0).addParagraph().appendTextBox(120f, 320f);
tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);
//Set the position of text box
tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Right_Margin_Area);
tb.getFormat().setHorizontalPosition(-100f);
tb.getFormat().setVerticalOrigin(VerticalOrigin.Page);
tb.getFormat().setVerticalPosition(130f);
//Set the border color and fill color of the text box
tb.getFormat().setLineColor(Color.BLUE);
tb.getFormat().setFillColor(new Color(203,234,253) );
//Insert an image to text box as a paragraph
Paragraph para = tb.getBody().addParagraph();
DocPicture picture = para.appendPicture("C:\\Users\\Administrator\\Desktop\\Ralph.jpg");
//Set alignment for the paragraph
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the size of the inserted image
picture.setHeight(90f);
picture.setWidth(90f);
//Insert text to text box as the second paragraph
para = tb.getBody().addParagraph();
TextRange textRange = para.appendText("Emerson is truly the center of the American transcendental movement, "
+"setting out most of its ideas and values in a little book, Nature, published in 1836, "
+"that represented at least ten years of intense study in philosophy, religion, and literature.");
//Set alignment for the paragraph
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the text font
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
textRange.getCharacterFormat().setItalic(true);
//Save to file
doc.saveToFile("InsertTextBox.docx", FileFormat.Docx_2013);
}
}

Remove a Text Box from a Word Document
Spire.Doc for Java provides the Document.getTextBoxes().removeAt() method to delete a specified text box by index. If you want to delete all text boxes from the Word document, you can use the Document.getTextBoxes().clear() method. The below example shows how to remove the first text box from a Word document.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Remove the first text box using Document.getTextBoxes().removeAt() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class DeleteTextbox {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("E:\\Files\\TextBox.docx");
//Remove text box by index
doc.getTextBoxes().removeAt(0);
//Remove all text boxes
//doc.getTextBoxes().clear();
//Save to file
doc.saveToFile("RemoveTextbox.docx", FileFormat.Docx);
}
}

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.
In MS Word, WordArt is used to insert text with special effects such as shadows, outlines, colors, gradients, and 3D effects. It can be helpful when creating stylish headlines or highlighting specific content. In this article, you will learn how to programmatically insert WordArt in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.1.3</version>
</dependency>
</dependencies>
Insert WordArt in Word
The ShapeType enumeration provided by Spire.Doc for Java defines a variety of WordArt shape types whose names begin with "Text". In order to create a WordArt in Word, you need to initialize an instance of ShapeObject and specify the WordArt type and text content. The detailed steps are as follows:
- Create a Document instance.
- Add a section to the document using Document.addSection() method, and then add a paragraph to the section using Section.addParagraph() method.
- Append a shape to the paragraph and specify the shape size and type using Paragraph.appendShape(float width, float height, ShapeType shapeType) method.
- Set the position of the shape using ShapeObject.setVerticalPosition() and ShapeObject.setHorizontalPosition() methods.
- Set the text of WordArt using ShapeObject.getWordArt().setText() method.
- Set the fill color and stroke color of WordArt using ShapeObject.setFillColor() and ShapeObject.setStrokeColor() methods.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class WordArt{
public static void main(String[] args) throws Exception {
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.addSection();
//Add a paragraph.
Paragraph paragraph = section.addParagraph();
//Add a shape to the paragraph and specify the shape size and type
ShapeObject shape = paragraph.appendShape(400, 150, ShapeType.Text_Deflate_Bottom);
//Set the position of the shape
shape.setVerticalPosition(60);
shape.setHorizontalPosition(60);
//set the text of WordArt
shape.getWordArt().setText("Create WordArt in Word");
// Set the fill color and stroke color of WordArt
shape.setFillColor(Color.CYAN);
shape.setStrokeColor(Color.BLUE);
//save the document to file.
doc.saveToFile("WordArt.docx", FileFormat.Docx_2013);
}
}

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.