简体   繁体   English

如何添加页码 和打印日期ms互操作字DLL

[英]How to add page no. and print date ms interop word DLL

I want to add following text into MS-Word footer using MS-Interop Word DLL. 我想使用MS-Interop Word DLL将以下文本添加到MS-Word页脚中。

Required Footer Text: 所需的页脚文字:

"Page 1 of 10 and date = {Current Date}" something like this. “第10页,第1页,日期= {当前日期}”类似。 I have added below code which add page no. 我在下面的代码中添加了添加页码的代码。 and current date but its not allowing me add any custom text like "Page 1 of 10". 和当前日期,但不允许我添加任何自定义文字,例如“第1页,共10页”。

Here is my code 这是我的代码

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;


    }

An idea how to add such functionality? 一个想法如何添加这样的功能?

Here is the solution which I have found. 这是我找到的解决方案。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM