การทำงานกับส่วนหัวและส่วนท้าย

Contents
[ ]

Aspose.Words อนุญาตให้ผู้ใช้ทำงานกับส่วนหัวและส่วนท้ายในเอกสาร ส่วนหัวคือข้อความที่อยู่ด้านบนของหน้า และส่วนท้ายคือข้อความที่ด้านล่างของหน้า โดยทั่วไป พื้นที่เหล่านี้จะใช้ในการแทรกข้อมูลที่ควรจะทำซ้ำบนทั้งหมดหรือบางหน้าของเอกสาร เช่น หมายเลขหน้า วันที่สร้าง ข้อมูลบริษัท และอื่นๆ

สร้างส่วนหัวหรือส่วนท้ายโดยใช้ DocumentBuilder

หากคุณต้องการเพิ่มส่วนหัวหรือส่วนท้ายของเอกสารโดยทางโปรแกรม วิธีที่ง่ายที่สุดคือใช้คลาส DocumentBuilder ในการดำเนินการ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มส่วนหัวและส่วนท้ายสำหรับหน้าเอกสาร:

ระบุตัวเลือกส่วนหัวหรือส่วนท้าย

เมื่อคุณเพิ่มส่วนหัวหรือส่วนท้ายลงในเอกสาร คุณสามารถตั้งค่าคุณสมบัติขั้นสูงบางอย่างได้ Aspose.Words มอบคลาส HeaderFooter และ HeaderFooterCollection ให้กับผู้ใช้ รวมถึงการแจงนับ HeaderFooterType ที่ให้คุณควบคุมกระบวนการปรับแต่งส่วนหัวและส่วนท้ายได้มากขึ้น

ระบุประเภทส่วนหัวหรือส่วนท้าย

คุณสามารถระบุประเภทส่วนหัวที่แตกต่างกันได้สามประเภทและประเภทส่วนท้ายที่แตกต่างกันสามประเภทสำหรับเอกสารเดียว:

  1. ส่วนหัวและ/หรือส่วนท้ายของหน้าแรก
  2. ส่วนหัวและ/หรือส่วนท้ายสำหรับหน้าคู่
  3. ส่วนหัวและ/หรือส่วนท้ายสำหรับหน้าคี่

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มส่วนหัวสำหรับหน้าเอกสารคี่:

ระบุว่าจะแสดงส่วนหัวหรือส่วนท้ายที่แตกต่างกันสำหรับหน้าแรกหรือไม่

ตามที่กล่าวไว้ข้างต้น คุณยังสามารถตั้งค่าส่วนหัวหรือส่วนท้ายอื่นสำหรับหน้าแรกได้ ในการดำเนินการนี้ คุณจะต้องตั้งค่าแฟล็ก DifferentFirstPageHeaderFooter เป็น true จากนั้นระบุค่า HeaderFirst หรือ FooterFirst

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าส่วนหัวสำหรับหน้าแรกเท่านั้น:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want different headers and footers for first page.
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page.");
builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
builder.Write("Footer for the first page.");
builder.MoveToSection(0);
builder.Writeln("Page 1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 2");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.DifferentFirstPage.docx");

ระบุว่าจะแสดงส่วนหัวหรือส่วนท้ายที่แตกต่างกันสำหรับหน้าคี่หรือคู่

ถัดไป คุณจะต้องตั้งค่าส่วนหัวหรือส่วนท้ายที่แตกต่างกันสำหรับหน้าคี่และหน้าคู่ในเอกสาร ในการดำเนินการนี้ คุณต้องตั้งค่าแฟล็ก OddAndEvenPagesHeaderFooter เป็น true จากนั้นระบุค่า HeaderPrimary และ HeaderEven หรือ FooterPrimary และ FooterEven

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want different headers and footers for even and odd pages.
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages.");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for odd pages.");
builder.MoveToHeaderFooter(HeaderFooterType.FooterEven);
builder.Write("Footer for even pages.");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Footer for odd pages.");
builder.MoveToSection(0);
builder.Writeln("Page 1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 2");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.OddEvenPages.docx");

แทรกรูปภาพในตำแหน่งที่แน่นอนลงในส่วนหัว

หากต้องการวางรูปภาพในส่วนหัวหรือส่วนท้าย ให้ใช้ประเภทส่วนหัว HeaderPrimary หรือประเภทส่วนท้าย FooterPrimary และวิธีการ InsertImage

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มรูปภาพในส่วนหัว:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertImage(ImagesDir + "Logo.jpg", RelativeHorizontalPosition.RightMargin, 10,
RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through);
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.InsertImage.docx");
view raw insert-image.cs hosted with ❤ by GitHub

ตั้งค่าคุณสมบัติแบบอักษรและย่อหน้าสำหรับข้อความส่วนหัวหรือส่วนท้าย

ด้วย Aspose.Words คุณสามารถตั้งค่าคุณสมบัติแบบอักษรและย่อหน้า ใช้ประเภทส่วนหัว HeaderPrimary หรือประเภทส่วนท้าย FooterPrimary รวมถึงวิธีการและคุณสมบัติสำหรับการทำงานกับแบบอักษรและย่อหน้าที่คุณใช้สำหรับเนื้อหาของเอกสาร

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าข้อความในส่วนหัวเป็น Arial ตัวหนา ขนาด 14 และการจัดกึ่งกลาง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.Font.Size = 14;
builder.Write("Header for page.");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.FontProps.docx");
view raw font-props.cs hosted with ❤ by GitHub

แทรกหมายเลขหน้าลงในส่วนหัวหรือส่วนท้าย

หากจำเป็น คุณสามารถเพิ่มหมายเลขหน้าลงในส่วนหัวหรือส่วนท้ายได้ เมื่อต้องการทำเช่นนี้ ให้ใช้ประเภทส่วนหัว HeaderPrimary หรือประเภทส่วนท้าย FooterPrimary และวิธีการ InsertField เพื่อเพิ่มฟิลด์ที่จำเป็น

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มหมายเลขหน้าในส่วนท้ายทางด้านขวา:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page ");
builder.InsertField("PAGE", "");
builder.Write(" of ");
builder.InsertField("NUMPAGES", "");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.PageNumbers.docx");
view raw page-numbers.cs hosted with ❤ by GitHub

ใช้ส่วนหัวหรือส่วนท้ายที่กำหนดไว้ในส่วนก่อนหน้า

หากคุณต้องการคัดลอกส่วนหัวหรือส่วนท้ายจากส่วนก่อนหน้า คุณก็สามารถทำได้เช่นกัน

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการคัดลอกส่วนหัวหรือส่วนท้ายจากส่วนก่อนหน้า:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Clones and copies headers/footers form the previous section to the specified section.
/// </summary>
private void CopyHeadersFootersFromPreviousSection(Section section)
{
Section previousSection = (Section)section.PreviousSibling;
if (previousSection == null)
return;
section.HeadersFooters.Clear();
foreach (HeaderFooter headerFooter in previousSection.HeadersFooters)
section.HeadersFooters.Add(headerFooter.Clone(true));
}

ตรวจสอบให้แน่ใจว่าส่วนหัวหรือส่วนท้ายปรากฏเมื่อใช้การวางแนวหน้าและขนาดหน้าที่แตกต่างกัน

Aspose.Words ช่วยให้คุณกำหนดลักษณะที่ปรากฏของส่วนหัวหรือส่วนท้ายเมื่อใช้การวางแนวและขนาดหน้าที่แตกต่างกัน

ตัวอย่างต่อไปนี้แสดงวิธีการทำเช่นนี้:

วิธีลบเฉพาะส่วนหัวหรือส่วนท้ายเท่านั้น

แต่ละส่วนในเอกสารสามารถมีส่วนหัวได้สูงสุดสามส่วนหัวและส่วนท้ายสูงสุดสามส่วน (สำหรับหน้าแรก คู่ และหน้าคี่) หากคุณต้องการลบส่วนหัวหรือส่วนท้ายทั้งหมดในเอกสาร คุณต้องวนซ้ำส่วนทั้งหมดและลบโหนดส่วนหัวหรือโหนดส่วนท้ายที่เกี่ยวข้องแต่ละรายการออก

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบส่วนท้ายทั้งหมดออกจากทุกส่วนแต่ปล่อยให้ส่วนหัวไม่เสียหาย คุณสามารถลบเฉพาะส่วนหัวได้ในลักษณะเดียวกัน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Header and footer types.docx");
foreach (Section section in doc)
{
// Up to three different footers are possible in a section (for first, even and odd pages)
// we check and delete all of them.
HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterFirst];
footer?.Remove();
// Primary footer is the footer used for odd pages.
footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
footer?.Remove();
footer = section.HeadersFooters[HeaderFooterType.FooterEven];
footer?.Remove();
}
doc.Save(ArtifactsDir + "RemoveContent.RemoveFooters.docx");