'OpenXML how to make text bold
I use openXML to write a word document containing my text. Now I want to make a part of the text bold. I however fail to find a solution to make my text bold, since I can't figure it out. My code is:
Dim FilePath As String = "C:\\MyFolder\\MyFile.docx"
'Create a document to work in
Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create(FilePath, WordprocessingDocumentType.Document)
' Add a main document part.
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
' Create the document structure and add some text.
mainPart.Document = New Document()
Dim body As Body = mainPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim runNormal As Run = para.AppendChild(New Run())
Dim runBold As Run = para.AppendChild(New Run())
runBold.RunProperties.Bold()
I want to have two instances: one for normal text runNormal
and one for the bold text runBold
. Sadly the runBold does not work, while the runNormal works.
Solution 1:[1]
Ok I figured it out how it works, so here the solution for everyone:
If you are writing the first time in bold in your text, you need to use this:
Dim runProperties As RunProperties = run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am written in bold!"))
Now if you want to write again in bold, add this code:
run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am also written in bold!"))
That's it. To summarize, here the complete code:
Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create(DOCXPath, WordprocessingDocumentType.Document)
' Add a main document part.
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
' Create the document structure
mainPart.Document = New Document()
Dim body As Body = mainPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim run As Run = para.AppendChild(New Run())
' Write in bold (only works in the first line which you write)
Dim runProperties As RunProperties = run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am written in bold!"))
' If you want to write a second time in bold, just add the next line and afterwards your text to be written in bold.
run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am also written in bold!"))
Solution 2:[2]
Refer to the following code.
WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document)
Dim mainDocumentPart As MainDocumentPart = doc.AddMainDocumentPart()
mainDocumentPart.Document = New Document()
Dim body As Body = mainDocumentPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim run As Run = para.AppendChild(New Run())
Dim runProperties As RunProperties = run.AppendChild(New RunProperties())
Dim bold As Bold = New Bold()
bold.Val = OnOffValue.FromBoolean(True)
runProperties.AppendChild(bold)
run.AppendChild(New Text("Welcome!!!!!"))
doc.MainDocumentPart.Document.Save()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cliff |
Solution 2 |