Hi,
I am using DocumentFormat.OpenXml.dll to create a document file in which we are showing some HTML formatted data. I want to add
additional numbering styles (1, 1.1, i, I, A, a) to the added HTML paragraph. However if I try to add this to the paragraph, the property is displayed as null.
Could you please help me in applying the styling to the HTML formatted paragraph?
The following methods are being used to generate the documents:
privateParagraph GetStyledParagraph(RunProperties rps,ParagraphProperties pgps, string text,refWordprocessingDocument myDoc)
{
Paragraph pp = null;
System.Text.RegularExpressions.Regex tagRegex =new System.Text.RegularExpressions.Regex(@"<\s*([^> ]+)[^>]*>.*?<\s*/\s*\1\s*>");
Run run = new
Run();
if (rps != null)
run.Append(rps.CloneNode(true));
if (myDoc != null&& text !=null&&tagRegex.IsMatch(text))//HTML DATA
{
pp = GetParaEditor(text, myDoc,"");
if (pgps != null)
pp.Append(pgps.CloneNode(true));
//Paragraph property is not getting applied.
pp.AppendChild<Run>(run);
return pp;
}
else//Normal text
{
run.AppendChild<Text>(new Word.Text(text)
{ Space = SpaceProcessingModeValues.Preserve });
Paragraph pg = new Word.Paragraph();
if (pgps != null)
pg.Append(pgps.CloneNode(true));
//Paragraph property is getting applied.
pg.AppendChild<Run>(run);
return pg;
}
}
int icnt = 0;
privateParagraph GetParaEditor(string editorContentBlob,WordprocessingDocument myDoc,
string altChnkId)
{
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
string html =
@"<html>
< head/>
< body>
{0}
< /body>
< /html>";
System.Text.StringBuilder sbu =new System.Text.StringBuilder();
html = sbu.AppendFormat(html, editorContentBlob).ToString();
// html = objEditorContent.ToString();
string altChunkId = "AltChunkId"+ icnt++;
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
"application/xhtml+xml", altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create,FileAccess.Write))
using (StreamWriter stringStream =newStreamWriter(chunkStream))
stringStream.Write(html);
XElement altChunk = newXElement(w + "altChunk",
newXAttribute(r +"id", altChunkId)
);
AltChunk altChunck = newAltChunk();
altChunck.Id = altChunkId;
Paragraph paraEditor =
newParagraph();
paraEditor.Append(newRun(altChunck));
return paraEditor;
}
Thank you.
-Sunil