Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart page numbering in header with OpenXML SDK 2.0

I can't figure out how to start the page numbering at a specific point for a section using OpenXML SDK 2.0. Here's what I see when I reflect on a header in a document using the 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>

It looks like it's building the field using a set of Runs, and then by including a Run with a Text value of "1" to start the page numbering for this header to 1 -- I should mention the section that uses this header is on page 4.

However, when even when I generate the equivalent using the following code for my own document, Word insists on starting the page numbering at the current page. For example, page 4 starts numbering at page 4, when the desired behavior is that page 4 starts numbering at page 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 })
));

And here's the code where I'm adding a section break, which still doesn't restart the page numbering:

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;
}

So, how do I start the page numbering for a section header at an arbitrary value? Am I missing something?

like image 428
Michael Nero Avatar asked Aug 14 '12 07:08

Michael Nero


2 Answers

I figured this out finally. The trick is to use the following code:

string headerID = "YOUR_HEADER_ID";
int pageNumberStart = 1

var paragraph = new Paragraph(
   new ParagraphProperties(
      new SectionProperties(
         new HeaderReference { Id = headerID },
         new PageNumberType { Start = pageNumberStart }
      )
   )
);
like image 140
Michael Nero Avatar answered Oct 22 '22 23:10

Michael Nero


You have to insert a SectionBreak.

here is a good short video explaining on how to start a page numbering from a specific page.

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

like image 34
Michael Daniloff Avatar answered Oct 22 '22 23:10

Michael Daniloff