用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

原创粉丝点击