วิธีเพิ่มหมายเลขหน้าใน Word โดยใช้ C#

คำแนะนำบทช่วยสอนสั้นๆ นี้เกี่ยวกับ วิธีเพิ่มหมายเลขหน้าใน Word โดยใช้ C# โดยการแชร์ข้อกำหนดการกำหนดค่าและตรรกะที่จะใช้เพื่อให้ได้ฟังก์ชันการทำงาน โดยจะอธิบายขั้นตอนต่างๆ เช่น การไปยังส่วนต่างๆ ในเอกสาร DOCX การใช้เงื่อนไขในขณะที่เพิ่มฟิลด์ในส่วนท้าย และสุดท้าย แสดงผลโดยการเพิ่มหน้าว่าง เมื่อใช้บทความนี้ คุณสามารถใช้ การใส่หมายเลขหน้าในเอกสาร word ใน C# ในส่วนท้ายหรือส่วนหัวได้หากจำเป็น

ขั้นตอนในการเพิ่มหมายเลขหน้าใน Word โดยใช้ C#

  1. กำหนดค่าโครงการเพื่อเพิ่ม Aspose.Words สำหรับการเพิ่มหมายเลขหน้าในไฟล์ Word
  2. โหลดไฟล์ Word ลงในวัตถุคลาส Document เพื่อเพิ่มหมายเลขหน้า
  3. สร้างวัตถุคลาส DocumentBuilder สำหรับเอกสารที่โหลด
  4. ย้ายตัวควบคุมไปที่ส่วนท้ายของเอกสารที่โหลด
  5. เพิ่มฟิลด์หมายเลขหน้าตามเงื่อนไขเพื่อเพิ่มหมายเลขหลังหน้าที่ระบุ
  6. ย้ายการควบคุมไปยังเอกสารเริ่มต้นเพื่อเพิ่มหน้าทดสอบ
  7. บันทึกไฟล์ Word ที่เป็นผลลัพธ์ด้วยหมายเลขหน้าในส่วนท้ายตามที่กำหนดไว้ด้านบน

ขั้นตอนเหล่านี้อธิบายลำดับตรรกะของกระบวนการในการ ใส่หมายเลขหน้าในคำโดยใช้ C# แสดงว่าก่อนอื่นคุณต้องโหลดไฟล์ Word จากนั้นคล้ายกับกระบวนการด้วยตนเอง เลื่อนเคอร์เซอร์ไปที่ส่วนท้ายแล้วเพิ่มฟิลด์ รายละเอียดยังแสดงให้เห็นในรหัสเพื่อเพิ่มเงื่อนไขสำหรับการเพิ่มหมายเลขหน้าที่เริ่มต้นจากหน้าใดหน้าหนึ่ง

รหัสเพื่อเพิ่มหมายเลขหน้า Word ในส่วนท้ายโดยใช้ C

namespace AddPageNumbersInWordUsingCSharp
{
class Program
{
static void Main(string[] args) // Main function to Add Page Numbers in Word using C#
{
// Load the license to avoid a watermark in the output Word file
// after adding the page numbers in the footer
Aspose.Words.License licAddPageNumber = new Aspose.Words.License();
licAddPageNumber.SetLicense("Aspose.Word.lic");
// Load the input Word file for adding page numbers
Aspose.Words.Document wordFile = new Aspose.Words.Document("word.docx");
// Instantiate the DocumentBuilder object to move around and insert contents
Aspose.Words.DocumentBuilder fileBuilder = new Aspose.Words.DocumentBuilder(wordFile);
// Using the builder, move to the primary footer section
fileBuilder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
// Add the page number field along with the condition using IF
Aspose.Words.Fields.Field field = fileBuilder.InsertField("IF ", null);
fileBuilder.MoveTo(field.Start.NextSibling.NextSibling);
// Insert the field in to the moved location i.e. footer
fileBuilder.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false);
// add the IF expression to be checked before inserting page number
fileBuilder.Write(" > 4 \"");
// In the TRUE segment of the IF condition add another field
fileBuilder.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false);
// In the FALSE part of the IF condition insert blank string
fileBuilder.Write("\" \"\"");
// Move to the start of the document for adding blank pages
fileBuilder.MoveToDocumentStart();
// Insert a defined number of blank pages
for (int page = 0; page < 15; page++)
fileBuilder.InsertBreak(Aspose.Words.BreakType.PageBreak);
// Save to output Word file with page number in the DOCX format
wordFile.Save("show hide page numbers .docx");
System.Console.WriteLine("Done");
}
}
}

โค้ดนี้สาธิต วิธีเริ่มเลขหน้าจากหน้าเฉพาะใน word โดยใช้ C# โดยให้คำสั่งเพื่อนำทางไปยังส่วนต่างๆ ของเอกสาร Word เช่น การย้ายไปที่ส่วนท้ายโดยใช้วัตถุคลาส DocumentBuilder ตัวสร้างเดียวกันสามารถใช้เพื่อแทรกฟิลด์ในส่วนหัวหรือส่วนท้ายโดยระบุประเภทฟิลด์และจัดรูปแบบข้อความด้วย เมื่อเพิ่มหมายเลขหน้าในส่วนท้ายแล้ว ตัวสร้างจะใช้เพื่อย้ายไปยังจุดเริ่มต้นของเอกสาร และเพิ่มหน้าว่างสำหรับการทดสอบคุณสมบัติ

เราได้เรียนรู้การ เพิ่มเลขหน้าในเอกสาร word ใน C# อย่างไรก็ตาม หากคุณต้องการตรวจสอบวิธีเพิ่มรูปภาพในไฟล์ Word โปรดดูบทความ วิธีเพิ่มรูปภาพในเอกสาร Word โดยใช้ C#

 ไทย