• Csharp Word 插入頁眉頁腳,自動頁碼


    using Microsoft.Office.Interop.Word; //ver 12.0
    
    
           /// <summary>
            /// 自動頁碼,頁眉頁腳
            /// 20120603 塗聚文
            /// Geovin Du
            /// </summary>
            /// <param name="pPageHeader">頁眉自定義文字</param>
            /// <param name="pPageFoot">頁腳自定義文字</param>
            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;
                //}
    
    
    
                //自动页码设置
                //John Yang  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();
    
                
            }
    
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    RuntimeError: An attempt has been made to start a new process before the current
    Expected object of backend CPU but got backend CUDA for argument #2 'weight'
    RuntimeError: multi-target not supported at
    模型load文件时报AttributeError: Can't get attribute 'Cours' on <module '__main__' from 错误
    ArrayList扩容源码分析
    HashMap源码分析(put/get)
    索引优化策略有哪些
    Mysql什么是回表查询和覆盖索引
    MyISAM与InnoDB区别
    使用ReentrantLock实现阻塞队列与交替打印
  • 原文地址:https://www.cnblogs.com/geovindu/p/2533038.html
Copyright © 2020-2023  润新知