Mic*_*ero 4 .net c# openxml openxml-sdk
我无法弄清楚如何使用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>
Run Code Online (Sandbox Code Playgroud)
它看起来像是使用一组运行构建字段,然后通过包含一个Text值为"1"的Run来启动此标题的页面编号为1 - 我应该提到使用此标题的部分是打开的第4页.
但是,即使我使用以下代码为我自己的文档生成等效文件,Word仍然坚持在当前页面开始页面编号.例如,第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 })
));
Run Code Online (Sandbox Code Playgroud)
这里是我添加分节符的代码,它仍然没有重新启动页面编号:
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;
}
Run Code Online (Sandbox Code Playgroud)
那么,如何以任意值开始节标题的页面编号?我错过了什么吗?
我终于弄明白了.诀窍是使用以下代码:
string headerID = "YOUR_HEADER_ID";
int pageNumberStart = 1
var paragraph = new Paragraph(
new ParagraphProperties(
new SectionProperties(
new HeaderReference { Id = headerID },
new PageNumberType { Start = pageNumberStart }
)
)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2906 次 |
| 最近记录: |