5

我不知道如何在使用 OpenXML SDK 2.0 的部分的特定点开始页码。当我使用 OpenXML Productivity Tool 反映文档中的标题时,我看到了以下内容:

<w:hdr xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
  <w:p w:rsidR="00FC0BC9" w:rsidP="00FC0BC9" w:rsidRDefault="005F46AD">
    <w:pPr>
      <w:pStyle w:val="Header" />
      <w:ind w:right="360" />
      <w:jc w:val="right" />
    </w:pPr>
    <w:r>
      <w:t xml:space="preserve">I-1 Page </w:t>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="begin" />
    </w:r>
    <w:r>
      <w:instrText xml:space="preserve"> PAGE  \* Arabic  \* MERGEFORMAT </w:instrText>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="separate" />
    </w:r>
    <w:r w:rsidR="00C62387">
      <w:rPr>
        <w:noProof />
      </w:rPr>
      <w:t>1</w:t>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="end" />
    </w:r>
  </w:p>

看起来它正在使用一组 Runs 构建该字段,然后通过包含一个 Text 值为“1”的 Run 来将此标题的页码编号设置为 1——我应该提到使用此标题的部分是 on第 4 页。

但是,即使我为自己的文档使用以下代码生成等效项,Word 仍坚持从当前页面开始页码。例如,第 4 页从第 4 页开始编号,而所需的行为是第 4 页从第 1 页开始编号。

var headerPart = mainDocument.AddNewPart<HeaderPart>(GetHeaderIDFor(actWithScenes.Act, scene));

var header = new Header();
headerPart.Header = header;

header.Append(new Paragraph(
  new ParagraphProperties(new Justification { Val = JustificationValues.Right }),
  new Run(new Text(staticText) { Space = SpaceProcessingModeValues.Preserve }),
  new Run(new FieldChar { FieldCharType = FieldCharValues.Begin }),
  new Run(new FieldCode { Space = SpaceProcessingModeValues.Preserve, Text = " PAGE  \\* Arabic  \\* MERGEFORMAT " }),
  new Run(new FieldChar { FieldCharType = FieldCharValues.Separate }),
  new Run(new RunProperties(new NoProof()), new Text("1")),
  new Run(new FieldChar { FieldCharType = FieldCharValues.End })
));

这是我添加分节符的代码,它仍然不会重新启动页码:

public static Paragraph AppendSectionBreak(Body body, string headerID = null, string footerID = null) {
   var sectionProperties = new SectionProperties();

   if (null != headerID) {
      sectionProperties.Append(new HeaderReference { Id = headerID });
   }

   if (null != footerID) {
      sectionProperties.Append(new FooterReference { Id = footerID });
   }

   var paragraph = new Paragraph(new ParagraphProperties(sectionProperties));

   body.Append(paragraph);

   return paragraph;
}

那么,如何以任意值开始节标题的页码编号?我错过了什么吗?

4

2 回答 2

7

我终于想通了。诀窍是使用以下代码:

string headerID = "YOUR_HEADER_ID";
int pageNumberStart = 1

var paragraph = new Paragraph(
   new ParagraphProperties(
      new SectionProperties(
         new HeaderReference { Id = headerID },
         new PageNumberType { Start = pageNumberStart }
      )
   )
);
于 2012-08-16T17:48:49.097 回答
4

您必须插入一个SectionBreak。

这是一个很好的简短视频,解释了如何从特定页面开始页码。

http://www.youtube.com/watch?v=NGzz2ZmLrFw

于 2012-08-16T05:11:10.943 回答