NPOI创建docx脚注

       利用NPOI创建docx脚注或尾注分四个步骤。一是设置格式,即在styles.xml添加脚注和尾注所需的格式;二是创建脚注或尾注内容,即在footnotes.xml中添加脚注内容,在endnotes.xml中添加尾注内容;三是建立脚注或尾注关系,即在document.xml.rels中添加与脚注或尾注的关联;四是在正文中标注脚注或尾注。对于创建docx脚注,NPOI提供了较为完善的方法,在创建脚注内容时也创建了相应的关联,即对于创建docx脚注第三步不须专门操作。而对于创建docx尾注,NPOI在OpenXmlFormats提供了较为完善的有关尾注所有功能,但在XWPF中没有提供创建尾注的有关方法。下面只介绍创建docx脚注步骤。

一、设置格式

        在正文中标注脚注采用阿拉伯数字且为上标,在word中可以事先用格式存储在格式xml中,以后可以在正文引用其格式即可。

        创建格式文件(styles.xml):XWPFStyles m_styles = m_Docx.CreateStyles();

        创建格式xml:CT_Styles m_ctstyles = new CT_Styles();可以根据需要创建相应的格式。在此只列举与脚注有关的格式a6和a7。

        格式a6设置如下,其中需要格式a和Char2。

            //footnotetext

            m_ctstyle = new CT_Style();

            m_ctstyle.type = ST_StyleType.paragraph;

            m_ctstyle.customStyle = ST_OnOff.True;

            m_ctstyle.styleId = "a6";

            m_ctstyle.name = new CT_String();

            m_ctstyle.name.val = "footnote text";

            m_ctstyle.basedOn = new CT_String();

            m_ctstyle.basedOn.val = "a";

            m_ctstyle.link = new CT_String();

            m_ctstyle.link.val = "Char2";

            m_ctstyle.uiPriority = new CT_DecimalNumber();

            m_ctstyle.uiPriority.val = "99";

            m_ctstyle.semiHidden = new CT_OnOff();

            m_ctstyle.semiHidden.val = true;

            m_ctstyle.unhideWhenUsed = new CT_OnOff();

            m_ctstyle.unhideWhenUsed.val = true;

            m_ctstyle.rsid = new CT_LongHexNumber();

            byte[]m_bytefootnoteText = { 0x00, 0xF0, 0x43, 0x96 };

            m_ctstyle.rsid.val =m_bytefootnoteText;

            m_ctstyle.pPr = new CT_PPr();

            m_ctstyle.pPr.snapToGrid = new CT_OnOff();

            m_ctstyle.pPr.snapToGrid.val = false;

            m_ctstyle.pPr.jc = new CT_Jc();

            m_ctstyle.pPr.jc.val = ST_Jc.left;

            m_ctstyle.rPr = new CT_RPr();

            m_ctstyle.rPr.sz = new CT_HpsMeasure();

            m_ctstyle.rPr.sz.val = 18;

            m_ctstyle.rPr.szCs = new CT_HpsMeasure();

            m_ctstyle.rPr.szCs.val = 18;

            m_ctstyles.style.Add(m_ctstyle);

 

       格式a7设置如下,其中需要格式a0。

            //footnotereference

            m_ctstyle = new CT_Style();

            m_ctstyle.type = ST_StyleType.character;

            m_ctstyle.styleId = "a7";

            m_ctstyle.name = new CT_String();

            m_ctstyle.name.val = "footnote reference";

            m_ctstyle.basedOn = new CT_String();

            m_ctstyle.basedOn.val = "a0";

            m_ctstyle.uiPriority = new CT_DecimalNumber();

            m_ctstyle.uiPriority.val = "99";

            m_ctstyle.semiHidden = new CT_OnOff();

            m_ctstyle.semiHidden.val = true;

            m_ctstyle.unhideWhenUsed = new CT_OnOff();

            m_ctstyle.unhideWhenUsed.val = true;

            m_ctstyle.rsid = new CT_LongHexNumber();

            m_ctstyle.rsid.val =m_bytefootnoteText;

            m_ctstyle.rPr = new CT_RPr();

            m_ctstyle.rPr.vertAlign = new CT_VerticalAlignRun();

            m_ctstyle.rPr.vertAlign.val = ST_VerticalAlignRun.superscript;

            m_ctstyles.style.Add(m_ctstyle);

   

        把格式添加到格式文件中:m_styles.SetStyles(m_ctstyles);

 二、创建脚注内容

        所有的脚注内容都存储在footnotes.xml中,实际上脚注内容的格式就是引用前面所述的格式文件styles.xml中的定义,即格式a6和a7。

创建脚注内容文件:XWPFFootnotesm_ftns = m_Docx.CreateFootnotes()。

           

                //创建脚注内容

                int Id = m_ftns.GetFootnotesList().Count;

                CT_FtnEdnm_ftnedn = new CT_FtnEdn();

                m_ftnedn.id = Id.ToString();

                CT_Pm_FtnEdnxmlP = m_ftnedn.AddNewP();

                CT_PPrm_FtnEdnxmlPPr = m_FtnEdnxmlP.AddNewPPr();

               m_FtnEdnxmlPPr.AddNewPStyle().val = "a6";

               m_FtnEdnxmlPPr.AddNewRPr().rFonts = new CT_Fonts();

               m_FtnEdnxmlPPr.AddNewRPr().rFonts.hint = ST_Hint.eastAsia;

                CT_Rm_FtnEdnxmlR = m_FtnEdnxmlP.AddNewR();

                m_FtnEdnxmlR.AddNewRPr().rStyle= new CT_String();

               m_FtnEdnxmlR.AddNewRPr().rStyle.val = "a7";

                m_FtnEdnxmlR.Items = new System.Collections.ArrayList();

                m_FtnEdnxmlR.Items.Add(new CT_Empty());

                m_FtnEdnxmlR.ItemsElementName =new List<RunItemsChoiceType>();

               m_FtnEdnxmlR.ItemsElementName.Add(RunItemsChoiceType.footnoteRef);

                m_FtnEdnxmlR =m_FtnEdnxmlP.AddNewR();

                m_FtnEdnxmlR.AddNewT().Value = " ";

                m_FtnEdnxmlR =m_FtnEdnxmlP.AddNewR();

                m_FtnEdnxmlR.AddNewT().Value =strFtnEdn; //"脚注test内容

                XWPFFootnotem_fn = m_ftns.AddFootnote(m_ftnedn);

 

四、在正文中标注脚注

        最好用CT_P m_p =m_Docx.Document.body.AddNewP();方式创建段,在m_p中可以不断创建CT_R。

         CT_R m_r = m_p.AddNewR(); 

         m_r.AddNewT().Value = "NPOI";

          //标注脚注

         CT_Rm_FtnEdnR = m_p.AddNewR();

         m_FtnEdnR.AddNewRPr().rStyle = new CT_String();

         m_FtnEdnR.AddNewRPr().rStyle.val = "a7";

         m_FtnEdnR.Items = new System.Collections.ArrayList();

         CT_FtnEdnRefm_ftnref = new CT_FtnEdnRef();

         m_ftnref.id = m_FtnId;//创建脚注内容得到的Id

         m_FtnEdnR.Items.Add(m_ftnref);

         m_FtnEdnR.ItemsElementName = new List<RunItemsChoiceType>();

         m_FtnEdnR.ItemsElementName.Add(RunItemsChoiceType.footnoteReference);

 

        m_r = m_p.AddNewR();

        m_r.AddNewT().Value = "……";


       经过调试的例子下载:http://download.csdn.net/detail/gltide/7993809。NPOI是tonyqus提供的2.1.1.0源码经过修改编译,创建的docx在word2007可以打开,例子创建效果图如下。


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页