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

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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want different headers and footers for first page.
builder.getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.write("Header for the first page.");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_FIRST);
builder.write("Footer for the first page.");
builder.moveToSection(0);
builder.writeln("Page 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page 2");
doc.save(getArtifactsDir() + "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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want different headers and footers for even and odd pages.
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.write("Header for even pages.");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header for odd pages.");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
builder.write("Footer for even pages.");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("Footer for odd pages.");
builder.moveToSection(0);
builder.writeln("Page 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page 2");
doc.save(getArtifactsDir() + "WorkingWithHeadersAndFooters.OddEvenPages.docx");

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.insertImage(getImagesDir() + "Logo.jpg", RelativeHorizontalPosition.RIGHT_MARGIN, 10,
RelativeVerticalPosition.PAGE, 10, 50, 50, WrapType.THROUGH);
doc.save(getArtifactsDir() + "WorkingWithHeadersAndFooters.InsertImage.docx");

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getFont().setName("Arial");
builder.getFont().setBold(true);
builder.getFont().setSize(14);
builder.write("Header for page.");
doc.save(getArtifactsDir() + "WorkingWithHeadersAndFooters.HeaderFooterFontProps.docx");
view raw font-props.java hosted with ❤ by GitHub

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Page ");
builder.insertField("PAGE", "");
builder.write(" of ");
builder.insertField("NUMPAGES", "");
doc.save(getArtifactsDir() + "WorkingWithHeadersAndFooters.PageNumbers.docx");

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.getPreviousSibling();
if (previousSection == null)
return;
section.getHeadersFooters().clear();
for (HeaderFooter headerFooter : previousSection.getHeadersFooters())
section.getHeadersFooters().add(headerFooter.deepClone(true));
}

ให้แน่ใจว่าลักษณะส่วนหัวหรือส่วนท้ายเมื่อใช้ทิศทางหน้าแตกต่างกันและขนาดหน้า

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

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

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

แต่ละส่วนในเอกสารสามารถมีได้ถึงสามส่วนหัวและถึงสามส่วนท้าย(สำหรับหน้าแรกแม้และ ถ้าคุณต้องการลบส่วนหัวทั้งหมดหรือท้ายกระดาษทั้งหมดในเอกสารคุณต้องวนรอบผ่านทุกส่.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Header and footer types.docx");
for (Section section : doc.getSections())
{
// 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.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
footer.remove();
// Primary footer is the footer used for odd pages.
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
footer.remove();
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
footer.remove();
}
doc.save(getArtifactsDir() + "RemoveContent.RemoveFooters.docx");