עבודה עם כותרות עליונות ותחתונות

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-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Specify that we want different headers and footers for first page.
builder->get_PageSetup()->set_DifferentFirstPageHeaderFooter(true);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderFirst);
builder->Write(u"Header for the first page.");
builder->MoveToHeaderFooter(HeaderFooterType::FooterFirst);
builder->Write(u"Footer for the first page.");
builder->MoveToSection(0);
builder->Writeln(u"Page 1");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"Page 2");
doc->Save(ArtifactsDir + u"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-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Specify that we want different headers and footers for even and odd pages.
builder->get_PageSetup()->set_OddAndEvenPagesHeaderFooter(true);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderEven);
builder->Write(u"Header for even pages.");
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->Write(u"Header for odd pages.");
builder->MoveToHeaderFooter(HeaderFooterType::FooterEven);
builder->Write(u"Footer for even pages.");
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary);
builder->Write(u"Footer for odd pages.");
builder->MoveToSection(0);
builder->Writeln(u"Page 1");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"Page 2");
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.OddEvenPages.docx");

הכנס תמונה ממוקמת לחלוטין לכותרת

כדי למקם תמונה בכותרת עליונה או תחתונה, השתמש בסוג הכותרת HeaderPrimary או בסוג הכותרת FooterPrimary ובשיטה InsertImage.

דוגמת הקוד הבאה מראה כיצד להוסיף תמונה לכותרת:

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

הגדר מאפייני גופן ופסקה עבור טקסט כותרת עליונה או תחתונה

עם Aspose.Words אתה יכול להגדיר את מאפייני הגופן והפסקה, להשתמש בסוג הכותרת HeaderPrimary או בסוג הכותרת התחתונה FooterPrimary, כמו גם בשיטות ומאפיינים לעבודה עם הגופנים והפסקאות שבהם אתה משתמש עבור גוף המסמך.

דוגמת הקוד הבאה מראה כיצד להגדיר את הטקסט בכותרת לאריאל, מודגש, גודל 14 ויישור מרכז:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_Font()->set_Name(u"Arial");
builder->get_Font()->set_Bold(true);
builder->get_Font()->set_Size(14);
builder->Write(u"Header for page.");
doc->Save(ArtifactsDir + u"WorkingWithHeadersAndFooters.FontProps.docx");
view raw font-props.h hosted with ❤ by GitHub

הכנס מספרי עמודים לכותרת העליונה או התחתונה

במידת הצורך, תוכל להוסיף מספרי עמודים לכותרת העליונה או התחתונה. לשם כך, השתמש בסוג הכותרת HeaderPrimary או בסוג הכותרת התחתונה FooterPrimary ובשיטה InsertField כדי להוסיף את השדה הנדרש.

דוגמת הקוד הבאה מראה כיצד להוסיף מספרי עמודים לכותרת התחתונה מימין:

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

השתמש בכותרות עליונות או תחתונות שהוגדרו בסעיף הקודם

אם עליך להעתיק את הכותרת העליונה או התחתונה מהקטע הקודם, תוכל לעשות זאת גם כן.

דוגמת הקוד הבאה מראה כיצד להעתיק את הכותרת העליונה או התחתונה מהקטע הקודם:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
/// <summary>
/// Clones and copies headers/footers form the previous section to the specified section.
/// </summary>
void CopyHeadersFootersFromPreviousSection(SharedPtr<Section> section)
{
auto previousSection = System::ExplicitCast<Section>(section->get_PreviousSibling());
if (previousSection == nullptr)
{
return;
}
section->get_HeadersFooters()->Clear();
for (const auto& headerFooter : System::IterateOver<HeaderFooter>(previousSection->get_HeadersFooters()))
{
section->get_HeadersFooters()->Add((System::ExplicitCast<Node>(headerFooter))->Clone(true));
}
}

ודא מראה כותרת עליונה או תחתונה בעת שימוש בכיווני עמוד שונים וגודל עמוד

Aspose.Words מאפשר לך לספק מראה של כותרת עליונה או תחתונה בעת שימוש בכיוונים ובגדלי עמודים שונים.

הדוגמה הבאה מראה כיצד לעשות זאת:

כיצד להסיר רק כותרות עליונות או רק תחתונות

כל קטע במסמך יכול להכיל עד שלוש כותרות עליונות ועד שלוש כותרות תחתונות (לדפים ראשונים, זוגיים ומשונים). אם ברצונך להסיר את כל הכותרות העליונות או את כל הכותרות התחתונות במסמך, עליך לעבור בין כל הסעיפים ולהסיר כל צומת כותרת עליונה או צומת כותרת תחתונה מתאימה.

דוגמת הקוד הבאה מראה כיצד להסיר את כל הכותרות התחתונות מכל החלקים אך להשאיר כותרות שלמות. ניתן להסיר רק כותרות באופן דומה:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Header and footer types.docx");
for (const auto& section : System::IterateOver<Section>(doc))
{
// Up to three different footers are possible in a section (for first, even and odd pages)
// we check and delete all of them.
SharedPtr<HeaderFooter> footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterFirst);
if (footer != nullptr)
{
footer->Remove();
}
// Primary footer is the footer used for odd pages.
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary);
if (footer != nullptr)
{
footer->Remove();
}
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterEven);
if (footer != nullptr)
{
footer->Remove();
}
}
doc->Save(ArtifactsDir + u"RemoveContent.RemoveFooters.docx");