[英]How to add page no. and print date ms interop word DLL
我想使用MS-Interop Word DLL將以下文本添加到MS-Word頁腳中。
所需的頁腳文字:
“第10頁,第1頁,日期= {當前日期}”類似。 我在下面的代碼中添加了添加頁碼的代碼。 和當前日期,但不允許我添加任何自定義文字,例如“第1頁,共10頁”。
這是我的代碼
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate,"Date = ");
footerRange.Fields.UpdateSource();
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage, "Page No = ");
footerRange.Fields.UpdateSource();
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
一個想法如何添加這樣的功能?
這是我找到的解決方案。
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages);
Microsoft.Office.Interop.Word.Paragraph p4 = footerRange.Paragraphs.Add();
p4.Range.Text = " of ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
Microsoft.Office.Interop.Word.Paragraph p1 = footerRange.Paragraphs.Add();
p1.Range.Text = "Page: ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
Microsoft.Office.Interop.Word.Paragraph p3 = footerRange.Paragraphs.Add();
p3.Range.Text = " " + Environment.NewLine;
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate);
Microsoft.Office.Interop.Word.Paragraph p2 = footerRange.Paragraphs.Add();
p2.Range.Text = "Print date: ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.