2 sidabbs sidabbs 于 2015.05.25 21:11 提问

Open XML 创建的 WORD 目录 没有页码 1C

图片说明

利用 Open XML 创建了一个带目录的 WORD 文档,如上图 想让生成的目录带有页码信息,求大神指教。
代码下载地址:http://pan.baidu.com/s/1kTILhInhttp://pan.baidu.com/s/1kTILhIn
主要代码如下:
#region 根据查找到带有书签的标题,生成目录
for (int i = 0; i < paragrap.Count; i++)
{
W.Paragraph oldParagraph = paragrap[i];
W.BookmarkStart bs = oldParagraph.Descendants().First();
W.ParagraphStyleId ps = oldParagraph.Descendants().First();
W.Text oldText = oldParagraph.Descendants().First();

            #region 创建目录
            W.Paragraph newParagraph = new W.Paragraph();
            W.ParagraphProperties paragraphProperties1 = new W.ParagraphProperties();
            //假设标题对应的格式是标题1,则目录中应该为10
            W.ParagraphStyleId paragraphStyleId = new W.ParagraphStyleId() { Val = ps.Val + 0 };

            W.Tabs tabs1 = new W.Tabs();
            W.TabStop tapStop1 = new W.TabStop() { Val = W.TabStopValues.Right, Leader = W.TabStopLeaderCharValues.Dot, Position = 8296 };

            tabs1.Append(tapStop1);

            W.ParagraphMarkRunProperties paragraphMarkRunProtites = new W.ParagraphMarkRunProperties();
            W.NoProof noProof1 = new W.NoProof();

            paragraphMarkRunProtites.Append(noProof1);

            paragraphProperties1.Append(paragraphStyleId);
            paragraphProperties1.Append(tabs1);
            paragraphProperties1.Append(paragraphMarkRunProtites);


            W.Hyperlink hyperlink1 = new W.Hyperlink() { History = true, Anchor = bs.Name };

            W.Run run1 = new W.Run();

            W.RunProperties runProperties1 = new W.RunProperties();
            W.RunFonts runFonts1 = GenerateRunFonts(fontName);
            W.NoProof noProof2 = new W.NoProof();

            runProperties1.Append(runFonts1);
            runProperties1.Append(noProof2);
            W.Text text1 = new W.Text();
            text1.Text = oldText.Text;

            run1.Append(runProperties1);
            run1.Append(text1);

            W.Run run2 = new W.Run();

            W.RunProperties runProperties2 = new W.RunProperties();
            W.NoProof noProof3 = new W.NoProof();
            W.WebHidden webHidden1 = new W.WebHidden();

            runProperties2.Append(noProof3);
            runProperties2.Append(webHidden1);

            W.TabChar tabChar1 = new W.TabChar();
            run2.Append(runProperties2);
            run2.Append(tabChar1);


            W.Run run3 = new W.Run();

            W.RunProperties runProperties3 = new W.RunProperties();
            W.NoProof noProof4 = new W.NoProof();
            W.WebHidden webHidden2 = new W.WebHidden();
            runProperties3.Append(noProof4);
            runProperties3.Append(webHidden2);
            W.FieldChar fieldChar1 = new W.FieldChar() { FieldCharType = W.FieldCharValues.Begin };

            run3.Append(runProperties3);
            run3.Append(fieldChar1);


            W.Run run4 = new W.Run();

            W.RunProperties runProperties6 = new W.RunProperties();
            W.NoProof noProof6 = new W.NoProof();
            W.WebHidden webHidden3 = new W.WebHidden();

            runProperties6.Append(noProof6);
            runProperties6.Append(webHidden3);
            W.FieldCode fieldCode4 = new W.FieldCode() { Space = SpaceProcessingModeValues.Preserve };
            fieldCode4.Text = "PAGEREF " + bs.Name + "\\h";

            run4.Append(runProperties6);
            run4.Append(fieldCode4);

            W.Run run5 = new W.Run();

            W.RunProperties runProperties8 = new W.RunProperties();
            W.NoProof noProof8 = new W.NoProof();
            W.WebHidden webHidden5 = new W.WebHidden();

            runProperties8.Append(noProof8);
            runProperties8.Append(webHidden5);
            W.FieldChar fieldChar4 = new W.FieldChar() { FieldCharType = W.FieldCharValues.Separate };

            run5.Append(runProperties8);
            run5.Append(fieldChar4);

            W.Run run6 = new W.Run();

            W.RunProperties runProperties10 = new W.RunProperties();
            W.NoProof noProof10 = new W.NoProof();
            W.WebHidden webHidden7 = new W.WebHidden();

            runProperties10.Append(noProof10);
            runProperties10.Append(webHidden7);
            W.FieldChar fieldChar5 = new W.FieldChar() { FieldCharType = W.FieldCharValues.End };

            run6.Append(runProperties10);
            run6.Append(fieldChar5);


            hyperlink1.Append(run1);
            hyperlink1.Append(run2);
            hyperlink1.Append(run3);
            hyperlink1.Append(run4);
            hyperlink1.Append(run5);
            hyperlink1.Append(run6);
            newParagraph.Append(paragraphProperties1);
            if (i == 0)
            {
                //目录是用域做出来的,所以这里首先添加目录域开始代码
                AddCodeStart(newParagraph);
            }
            newParagraph.Append(hyperlink1);
            if (i == paragrap.Count - 1)
            {
                //目录是用域做出来的,所以这里首先添加目录域结束代码
                AddCodeEnd(newParagraph);
            }

            body.InsertAt<W.Paragraph>(newParagraph, i);
            #endregion

        }
        #endregion

1个回答

abuying
abuying   2017.08.01 16:57

fieldCode4.Text = "PAGEREF " + bs.Name + "\h";

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!