用OpenXml SDK 2.0 创建一个页脚带页码的Word文档
来源:互联网 发布:talkback是什么软件 编辑:程序博客网 时间:2024/05/01 08:01
实现环境:Visual Studio 2010, OpenXml SDK 2.0.50727
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessing;namespace OpenXmlFooterPageNumber{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SaveFileDialog objSaveFileDialog = new SaveFileDialog(); objSaveFileDialog.Filter = "Word Document (*.docx)|*.docx"; objSaveFileDialog.ShowDialog(); string strSavePath = objSaveFileDialog.FileName; if (strSavePath.Length > 0) { using (WordprocessingDocument objWordprocessingDocument = WordprocessingDocument.Create (strSavePath, WordprocessingDocumentType.Document)) { MainDocumentPart objMainDocumentPart = objWordprocessingDocument.AddMainDocumentPart(); Document objDocument = new Document(); objMainDocumentPart.Document = objDocument; Body objBody = new Body(); SectionProperties objSectionProperties = new SectionProperties(); FooterPart objFootPart = objMainDocumentPart.AddNewPart<FooterPart>(); Footer objFooter = new Footer(); objFootPart.Footer = objFooter; SdtBlock objSdtBlock_1 = new SdtBlock(); SdtContentBlock objSdtContentBlock_1 = new SdtContentBlock(); SdtBlock objSdtBlock_2 = new SdtBlock(); SdtContentBlock objSdtContentBlock_2 = new SdtContentBlock(); Paragraph objParagraph_1 = new Paragraph(); ParagraphProperties objParagraphProperties = new ParagraphProperties(); ParagraphStyleId objParagraphStyleId = new ParagraphStyleId() { Val = "Footer" }; objParagraphProperties.Append(objParagraphStyleId); Justification objJustification = new Justification() { Val = JustificationValues.Right }; objParagraphProperties.Append(objJustification); objParagraph_1.Append(objParagraphProperties); Run objRun_1 = new Run(); Text objText_1 = new Text(); objText_1.Text = "Page "; objRun_1.Append(objText_1); objParagraph_1.Append(objRun_1); Run objRun_2 = new Run(); FieldChar objFieldChar_1 = new FieldChar() { FieldCharType = FieldCharValues.Begin }; objRun_2.Append(objFieldChar_1); objParagraph_1.Append(objRun_2); Run objRun_3 = new Run(); FieldCode objFieldCode_1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve }; objFieldCode_1.Text = "PAGE "; objRun_3.Append(objFieldCode_1); objParagraph_1.Append(objRun_3); Run objRun_4 = new Run(); FieldChar objFieldChar_2 = new FieldChar() { FieldCharType = FieldCharValues.Separate }; objRun_4.Append(objFieldChar_2); objParagraph_1.Append(objRun_4); Run objRun_5 = new Run(); Text objText_2 = new Text(); objText_2.Text = "2"; objRun_5.Append(objText_2); objParagraph_1.Append(objRun_5); Run objRun_6 = new Run(); FieldChar objFieldChar_3 = new FieldChar() {FieldCharType = FieldCharValues.End }; objRun_6.Append(objFieldChar_3); objParagraph_1.Append(objRun_6); Run objRun_7 = new Run(); Text objText_3 = new Text(); objText_3.Text = "of "; objRun_7.Append(objText_3); objParagraph_1.Append(objRun_7); Run objRun_8 = new Run(); FieldChar objFieldChar_4 = new FieldChar() { FieldCharType = FieldCharValues.Begin }; objRun_8.Append(objFieldChar_4); objParagraph_1.Append(objRun_8); Run objRun_9 = new Run(); FieldCode objFieldCode_2 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve }; objFieldCode_2.Text = "NUMPAGES "; objRun_9.Append(objFieldCode_2); objParagraph_1.Append(objRun_9); Run objRun_10 = new Run(); FieldChar objFieldChar_5 = new FieldChar() { FieldCharType = FieldCharValues.Separate }; objRun_10.Append(objFieldChar_5); objParagraph_1.Append(objRun_10); Run objRun_11 = new Run(); Text objText_4 = new Text(); objText_4.Text = "2"; objRun_11.Append(objText_4); objParagraph_1.Append(objRun_11); Run objRun_12 = new Run(); FieldChar objFieldChar_6 = new FieldChar() {FieldCharType = FieldCharValues.End }; objRun_12.Append(objFieldChar_6); objParagraph_1.Append(objRun_12); objSdtContentBlock_2.Append(objParagraph_1); objSdtBlock_2.Append(objSdtContentBlock_2); objSdtContentBlock_1.Append(objSdtBlock_2); objSdtBlock_1.Append(objSdtContentBlock_1); objFooter.Append(objSdtBlock_1); string strFootrID = objMainDocumentPart.GetIdOfPart(objFootPart); FooterReference objFooterReference = new FooterReference() { Type = HeaderFooterValues.Default, Id = strFootrID }; objSectionProperties.Append(objFooterReference); objBody.Append(objSectionProperties); objMainDocumentPart.Document.Append(objBody); DocumentSettingsPart objDocumentSettingPart = objMainDocumentPart.AddNewPart<DocumentSettingsPart>(); objDocumentSettingPart.Settings = new Settings(); Compatibility objCompatibility = new Compatibility(); CompatibilitySetting objCompatibilitySetting = new CompatibilitySetting() { Name = CompatSettingNameValues.CompatibilityMode, Uri = "http://schemas.microsoft.com/office/word", Val = "14" }; objCompatibility.Append(objCompatibilitySetting); objDocumentSettingPart.Settings.Append(objCompatibility); } } } }}相关资源:http://download.csdn.net/detail/tx_officedev/3972762
- 用OpenXml SDK 2.0 创建一个页脚带页码的Word文档
- OpenXml SDK 2.0 创建Word文档——最简化的版本
- OpenXml SDK 2.0 创建Word 2010文档(不带[Compatibility])最简代码
- OpenXML SDK 读取WORD文档中的内容控件
- Word文档独立页码创建方法
- word文档的前几页不要页码,后面的排页码
- WORD文档中插入页码的问题
- OpenXML SDK 2.0发布
- 使用OpenXml SDK 给Word文档添加样式并应用于文本
- 使用OpenXml SDK向Word文档中添加页、段落、页眉和页脚
- 使用OpenXml SDK向Word文档中添加页、段落、页眉和页脚
- 使用OpenXml SDK向Word文档中添加页、段落、页眉和页脚
- 用OpenXml在文档的尾部添加一个Rich Text Content Control/Plain Text Content Control
- 使用OpenXML更新Word文档中的表格
- 使用OpenXML更新Word文档中的表格
- 在Word文档中添加页码的技巧
- word文档页眉页脚页码的修改问题
- Word的页码
- 适配器模式(adapter)
- 在SQL Server里查看当前连接的在线用户数
- SQL 左外连接,右外连接,全连接,内连接
- hibernate15道精选面试题
- C# 中的委托和事件
- 用OpenXml SDK 2.0 创建一个页脚带页码的Word文档
- svn import
- C#中的委托和事件(续)
- tcpdump使用说明
- 如何将twitter关联到你的magento站点 oauth1
- VC 编写 stdcall 方式的DLL
- hibernate面试题1
- 多线程编程注意事项
- FTP连接报530 User 用户名 cannot log in home directory inaccessible的解决方法