文檔半島外圍網(wǎng)上直營>>E-iceblue中文文檔>>在word中重置頁碼
                在word中重置頁碼
開發(fā)人員廣泛使用節(jié)來為每個不同的節(jié)設(shè)置不同的格式或布局選項,例如為不同的節(jié)使用不同的頁眉和頁腳信息,并為每個節(jié)動態(tài)重置頁碼。在 Spire.Doc for .NET 的幫助下,我們可以輕松地在 C# 和 VB.NET 中插入詞段和刪除詞段。我們將向您展示如何使用 Spire.Doc 輕松地為每個部分重置從 1 開始的頁碼。
技術(shù)交流Q群(767755948)
首先確保已正確安裝,然后通過以下路徑在下載的 Bin 文件夾中添加 Spire.Doc.dll 作為參考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc .dll”。這是如何重置每個部分的頁碼的代碼。
第 1 步:加載三個不同的 word 文檔
Document document1 = new Document();
document1.LoadFromFile("..\\..\\1.docx");
Document document2 = new Document();
document2.LoadFromFile("..\\..\\2.docx");
Document document3 = new Document();
document3.LoadFromFile("..\\..\\3.docx");
第 2 步: 使用section方法將所有文檔合并為一個word文檔
foreach (Section sec in document2.Sections)
{
document1.Sections.Add(sec.Clone());
}
foreach (Section sec in document3.Sections)
{
document1.Sections.Add(sec.Clone());
}
第 3 步:瀏覽文檔
//Traverse every section of document1
foreach (Section sec in document1.Sections)
{
//Traverse every object of the footer
foreach (DocumentObject obj in sec.HeadersFooters.Footer.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
{
DocumentObject para = obj.ChildObjects[0];
foreach (DocumentObject item in para.ChildObjects)
{
if (item.DocumentObjectType == DocumentObjectType.Field)
第 4 步:找到字段類型 FieldNumPages 并將其更改為 FieldSectionPages
//Find the item and its field type is FieldNumPages
if ((item as Field).Type == FieldType.FieldNumPages)
{
//Change field type to FieldSectionPages
(item as Field).Type = FieldType.FieldSectionPages;
第 5 步 : 重新開始節(jié)的頁碼并將起始頁碼設(shè)置為 1
document1.Sections[1].PageSetup.RestartPageNumbering = true; document1.Sections[1].PageSetup.PageStartingNumber = 1; document1.Sections[2].PageSetup.RestartPageNumbering = true; document1.Sections[2].PageSetup.PageStartingNumber = 1;
第 6 步:將文檔保存到文件并啟動它。
document1.SaveToFile("sample.docx",FileFormat.Docx);
System.Diagnostics.Process.Start("sample.docx");
完整代碼:
namespace ResetPageNumber
{
class Program
{
static void Main(string[] args)
{
Document document1 = new Document();
document1.LoadFromFile("..\\..\\1.docx");
Document document2 = new Document();
document2.LoadFromFile("..\\..\\2.docx");
Document document3 = new Document();
document3.LoadFromFile("..\\..\\3.docx");
foreach (Section sec in document2.Sections)
{
document1.Sections.Add(sec.Clone());
}
foreach (Section sec in document3.Sections)
{
document1.Sections.Add(sec.Clone());
}
foreach (Section sec in document1.Sections)
{
foreach (DocumentObject obj in sec.HeadersFooters.Footer.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
{
DocumentObject para = obj.ChildObjects[0];
foreach (DocumentObject item in para.ChildObjects)
{
if (item.DocumentObjectType == DocumentObjectType.Field)
{
if ((item as Field).Type == FieldType.FieldNumPages)
{
(item as Field).Type = FieldType.FieldSectionPages;
}
}
}
}
}
}
document1.Sections[1].PageSetup.RestartPageNumbering = true;
document1.Sections[1].PageSetup.PageStartingNumber = 1;
document1.Sections[2].PageSetup.RestartPageNumbering = true;
document1.Sections[2].PageSetup.PageStartingNumber = 1;
document1.SaveToFile("sample.docx",FileFormat.Docx);
System.Diagnostics.Process.Start("sample.docx");
}
}
}
                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線