C#给word添加页码 - 小众知识

C#给word添加页码

2020-07-01 14:25:00 苏内容
  标签: word/页码
阅读:5430

0

I need to add page number (Page 1 of X) starting from say 5th page in the word document. How to do that. The code I have adds to the entire document and I am unable to control it. I am using Word interop in C#. Please help.


 oDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;                         
//Object oMissing = System.Reflection.Missing.Value;                        
oDoc.ActiveWindow.Selection.TypeText(" Page ");                        
Object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;                        
Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;                        
oDoc.ActiveWindow.Selection.HeaderFooter.LinkToPrevious = false;                        
oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);                         oDoc.ActiveWindow.Selection.TypeText(" of ");                         oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range, ref TotalPages, ref oMissing, ref oMissing);                        


In order to (re-)start numbering in the document a Section Break is needed. The follosing example demonstrates how to insert a "Next Page" section break just before the target page, then format the new section's footer for page numbering to start in the section at 1.

Note I also changed the assignment to TotalPages on the assumption the total page count should be that of the new section, not of the entire document.

        //Go to page where page numbering should start
       string pageNum = "3";
       wdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToNext, ref missing, pageNum);
       Word.Range rngPageNum = wdApp.Selection.Range;
       //Insert Next Page section break so that numbering can start at 1
       rngPageNum.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage);

       Word.Section currSec = doc.Sections[rngPageNum.Sections[1].Index];
       Word.HeaderFooter ftr = currSec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];

       //So that the footer content doesn't propagate to the previous section    
       ftr.LinkToPrevious = false;
       ftr.PageNumbers.RestartNumberingAtSection = true;
       ftr.PageNumbers.StartingNumber = 1;

       //If the total pages should not be the total in the document, just the section
       //use the field SectionPages instead of NumPages
       object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldSectionPages;
       object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
       Word.Range rngCurrSecFooter = ftr.Range;
       rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref CurrentPage, ref missing, false);
       rngCurrSecFooter.InsertAfter(" of ");
       rngCurrSecFooter.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
       rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref TotalPages, ref missing, false);


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1