MS Word |使用DocumentFormat.OpenXml.dll将样式应用于HTML格式的段落 [英] MS Word | Applying styling to a HTML formatted paragraph using DocumentFormat.OpenXml.dll

查看:160
本文介绍了MS Word |使用DocumentFormat.OpenXml.dll将样式应用于HTML格式的段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 


我正在使用DocumentFormat.OpenXml.dll创建一个文档文件,我们在其中显示一些HTML格式的数据。我想在添加的HTML段落中添加
额外的编号样式(1,1.1,i,I,A,a)。但是,如果我尝试将其添加到段落中,则该属性显示为  null。


 


< span style ="font-family:'Calibri','sans-serif'"> 你能否帮我将样式应用于HTML格式的段落?


 


以下方法用于 生成文件:


 


private
段落 GetStyledParagraph( RunProperties rps,
ParagraphProperties pgps, string text,
ref WordprocessingDocument myDoc)


        {


           
段落 pp = null ;


            System.Text.RegularExpressions。 Regex tagRegex =
new System.Text.RegularExpressions。 Regex @"< \ * *([^>] +)[^>] *>。*?< \s * / \ s * \\\\ s *>" );


           
运行运行= new
运行
();


           
if (rps != null


                run.Append(rps.CloneNode( true ));


           
if (myDoc != null && text!=
null && tagRegex.IsMatch (文本)) // HTML DATA


            {


< span style ="font-size:9.5pt; font-family:Consolas">                pp = GetParaEditor(text,myDoc,
< span style ="color:#A31515">"" );


 


               
if (pgps!= null


                    pp.Append(pgps .CloneNode( true ));
//未应用段落属性。


                pp.AppendChild< 运行>(运行);


               
return pp;


           }


           
else //普通文本


            {


< span style ="font-size:9.5pt; font-family:Consolas"> 


                run.AppendChild< 文字>( new Word。文字(文字)
{Space = SpaceProcessingModeValues .Preserve});


               
段落 pg = new Word。段落();


               
if (pgps!= null


                    pg.Append(pgps .CloneNode( true ));
//段落属性正在应用。


                pg.AppendChild< 运行>(运行);


               
return pg;


           }


 


       }


 


int icnt = 0;


私人
段落 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 />


                     <正文>


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {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 块= mainPart.AddAlternativeFormatImportPart(


     ;           
" application / xhtml + xml",altChunkId);


           
using (< span style ="color:#2B91AF"> Stream
chunkStream = chunk.GetStream( FileMode .Create,
FileAccess .Write))


           
using (< span style ="color:#2B91AF"> StreamWriter
stringStream =
new StreamWriter (chunkStream))


                stringStream.Write(html);


           
XElement altChunk = new
XElement (w + " altChunk"


      ;           
new XAttribute (r +
" id" ,altChunkId)


           );


 


           
AltChunk altChunck = new
AltChunk ();


            altChunck.Id = altChunkId;


 


           
段落 paraEditor =
new
段落();


            paraEditor.Append( new
运行(altChunck));


           
return paraEditor;


       }


 


 


 


谢谢。


 


- Sunil


 

解决方案

Hi Sunil,


对于一般性建议,我建议您使用适用于Microsoft Office的Open XML SDK 2.5生产力工具。

您可以执行以下操作:


1.使用HTML格式的数据创建两个文档,并在其中一个文档上应用编号样式;

2.使用Open XML SDK 2.5生产力适用于Microsoft Office的工具

3.单击比较文件,生成代码,然后您可以获得所需的代码。


有关用于Microsoft Office的Open XML SDK 2.5生产力工具的更多信息,您可以参考以下链接:
$
#用于Microsoft Office的Open XML SDK 2.5¥b $ b https://www.microsoft.com/en-us/download/details .aspx?id = 30425
$


最好的问候,


爱德华


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:

 

private Paragraph GetStyledParagraph(RunProperties rps, ParagraphProperties pgps, string text, ref WordprocessingDocument 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;

private Paragraph 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 = new StreamWriter(chunkStream))

                stringStream.Write(html);

            XElement altChunk = new XElement(w + "altChunk",

                new XAttribute(r + "id", altChunkId)

            );

 

            AltChunk altChunck = new AltChunk();

            altChunck.Id = altChunkId;

 

            Paragraph paraEditor = new Paragraph();

            paraEditor.Append(new Run(altChunck));

            return paraEditor;

        }

 

 

 

Thank you.

 

-Sunil

 

解决方案

Hi Sunil,

For a general suggestion, I suggest you use Open XML SDK 2.5 productivity Tool for Microsoft Office.
You could do as below:

1. Create two documents with HTML formatted data and apply numbering styles on one of the documents;
2. Use Open XML SDK 2.5 productivity Tool for Microsoft Office
3. Click the Compare Files, generate the code, and then you could get the code what you want

For more information about Open XML SDK 2.5 productivity Tool for Microsoft Office, you could refer the link below:
# Open XML SDK 2.5 for Microsoft Office
https://www.microsoft.com/en-us/download/details.aspx?id=30425

Best Regards,

Edward


这篇关于MS Word |使用DocumentFormat.OpenXml.dll将样式应用于HTML格式的段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆