2

I am trying to apply formatting to text using open xml. I would like to add Bold, 18 pt font and have it centered.

The bold and font size formatting applies correctly but I cannot get the center justification to work. This is my first use of open XML, any help is much appreciated.

Dim mainPart As MainDocumentPart = mydoc.AddMainDocumentPart() 
mainPart.Document = New Document()
Dim body As New Body()
Dim paragraph As New Paragraph()
Dim run_paragraph As New Run()
'we want to put that text into the output document 
Dim text_paragraph As New Text("Executive Summary - " + name)
'Append elements appropriately. 

'bold
Dim RunProperties As RunProperties = run_paragraph.AppendChild(New RunProperties())
Dim Bold As New Bold
Bold.Val = OnOffValue.FromBoolean(True)
Dim fontSize As New FontSize
fontSize.Val = "22"
RunProperties.AppendChild(fontSize)
RunProperties.AppendChild(Bold)

'center
Dim paragraphProperties As ParagraphProperties = run_paragraph.AppendChild(New ParagraphProperties())
Dim justification As New Justification
justification.Val = JustificationValues.Center
paragraphProperties.AppendChild(justification)
run_paragraph.AppendChild(text_paragraph)
paragraph.Append(run_paragraph)
body.Append(paragraph)
mainPart.Document.Append(body)
mainPart.Document.Save()
Response.Redirect("~/summaries/" + documentfilename)
4

1 回答 1

2

Try to add paragraphProperties to the paragraph object (instead of run_paragraph)

replace:

Dim paragraphProperties As ParagraphProperties = run_paragraph.AppendChild(New ParagraphProperties())

with:

Dim paragraphProperties As ParagraphProperties = paragraph.AppendChild(New ParagraphProperties())
于 2013-05-21T06:16:45.433 回答