博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Csharp Word 插入頁眉頁腳,自動頁碼
阅读量:6031 次
发布时间:2019-06-20

本文共 5263 字,大约阅读时间需要 17 分钟。

using Microsoft.Office.Interop.Word; //ver 12.0

 

///         /// 自動頁碼,頁眉頁腳        /// 20120603 塗聚文        /// Geovin Du        ///         /// 頁眉自定義文字        /// 頁腳自定義文字        public void SetPageHeaderAndFooter(string pPageHeader, string pPageFoot)        {            Object missing = System.Reflection.Missing.Value;            //1             //添加页眉              //this._wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;            //this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;            //this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);            设置中间对齐              //this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;            跳出页眉设置              //this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;            //_wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsMessageBox;            //頁腳            //this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekEvenPagesFooter;            //this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);            //http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx            //2.设置要插入到文档各部分主页脚中的文本的字体。此代码示例使用活动文档。            //http://msdn.microsoft.com/zh-cn/library/ms178795(v=vs.90).aspx            foreach (Microsoft.Office.Interop.Word.Section wordSection in this._wordDocument.Sections)            {                wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;                wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;                wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 9;                wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 9;                //2.将文本插入到页脚中                wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = pPageFoot;                wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = pPageHeader;            }            //1.添加“自动图文集”项,以在文档的每个页眉中显示“第 X 页,共 Y 页            //foreach (Microsoft.Office.Interop.Word.Section section in this._wordDocument.Sections)            //{            //    object fieldEmpty = Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty;            //    object autoText = "AUTOTEXT  \"Page X of Y\" ";            //    object preserveFormatting = true;            //    section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(            //        section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,            //        ref fieldEmpty, ref autoText, ref preserveFormatting);            //    section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]            //           .Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;            //}            //自动页码设置            // http://social.msdn.microsoft.com/Forums/en/vsto/thread/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8            object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;             // Open up the footer in the word document            this._wordApplication.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;            // Set current Paragraph Alignment to Center            this._wordApplication.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; //.wdAlignParagraphCenter                        // Type in 'Page '            this._wordApplication.ActiveWindow.Selection.TypeText("Page ");            // Add in current page field            Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;            this._wordApplication.ActiveWindow.Selection.Fields.Add(this._wordApplication.ActiveWindow.Selection.Range, ref CurrentPage, ref missing, ref missing);            // Type in ' of '            this._wordApplication.ActiveWindow.Selection.TypeText(" of ");            // Add in total page field            Object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;            this._wordApplication.ActiveWindow.Selection.Fields.Add(this._wordApplication.ActiveWindow.Selection.Range, ref TotalPages, ref missing, ref missing);            object start = 0;             object end = 7;            Microsoft.Office.Interop.Word.Range rg = this._wordDocument.Range(ref start,ref end);            rg.Select();                    }

 

转载地址:http://gddhx.baihongyu.com/

你可能感兴趣的文章
深度分析Java的ClassLoader机制(源码级别)
查看>>
微服务架构选Java还是选Go - 多用户负载测试
查看>>
我的友情链接
查看>>
Javascript中的异步如何实现回调
查看>>
halcon算子介绍
查看>>
挖掘你不知道的windowsxp中的带宽潜能
查看>>
Software Engineering 招聘要求
查看>>
【转载】InstallAnyWhere自动化制作安装包的知识
查看>>
69、iSCSI共享存储配置实战
查看>>
文本编程
查看>>
乔布斯走了。你还期待苹果吗?
查看>>
优先级
查看>>
Tomcat与Web服务器、应用服务器的关系
查看>>
用DFS实现全排列 & 八皇后问题
查看>>
深度学习博客
查看>>
Android总结篇系列:Android Service
查看>>
Android dumpsys命令的使用
查看>>
Linux Kernel系列一:开篇和Kernel启动概要
查看>>
BZOJ 2756: [SCOI2012]奇怪的游戏 网络流/二分
查看>>
master + worker模式的node多核解决框架——node-cluster
查看>>