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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// <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;
     //}
 
 
 
     //自动页码设置
     //<a href="http://social.msdn.microsoft.com/profile/john%20yang%20-%20msft/?ws=usercard-mini" target="_blank" rel="noopener nofollow">John Yang </a> 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();
 
      
 }

 

posted @   ®Geovin Du Dream Park™  阅读(1971)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2012年6月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示