翻译|使用教程|编辑:李显亮|2021-05-19 11:01:12.383|阅读 320 次
概述:以编程方式使用Word文档时,可能需要添加或删除页眉和页脚。为此,本文将教您如何使用C ++在Word文档中添加和删除页眉和页脚。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Word文档中的页眉和页脚用于格式化和显示重要信息,例如主题,章节,页码,Copywrite等。以编程方式使用Word文档时,可能需要添加或删除页眉和页脚。为此,本文将教您如何使用C ++在Word文档中添加和删除页眉和页脚。
让我们探索以下有关的内容:
要在Word文档中添加页眉和页脚,我们将使用Aspose.Words for C ++ 它是本机C ++ API,支持创建,读取和修改Word文档,而无需安装Microsoft Word。
>>你可以点击这里下载Aspose.Words for C ++ 最新版测试体验。
Word文档中的页眉和页脚分为三部分,标题页,偶数页和奇数页。可以为这些部分添加不同的页眉和页脚。此外,还可以在页眉和页脚中添加图像和表格之类的元素。
在此示例中,我们将创建一个新的Word文档,并为标题页添加一个不同的标题。我们将在后面的页面中添加带有图片的页眉和带有表格的页脚。以下是在Word文档中添加页眉和页脚的步骤。
下面的示例代码演示了如何使用C ++在Word文档中添加页眉和页脚。
void CopyHeadersFootersFromPreviousSection(const System::SharedPtr& section)
{
System::SharedPtrpreviousSection = System::DynamicCast(section->get_PreviousSibling());
if (previousSection == nullptr)
{
return;
}
section->get_HeadersFooters()->Clear();
for (System::SharedPtrheaderFooterNode : System::IterateOver(previousSection->get_HeadersFooters()))
{
section->get_HeadersFooters()->Add(headerFooterNode->Clone(true));
}
}
int main()
{
// Source and output directory paths.
System::String inputDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
System::SharedPtrdoc = System::MakeObject();
System::SharedPtrbuilder = System::MakeObject(doc);
System::SharedPtrcurrentSection = builder->get_CurrentSection();
System::SharedPtrpageSetup = currentSection->get_PageSetup();
// Specify if we want headers/footers of the first page to be different from other pages.
// You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify
// Different headers/footers for odd and even pages.
pageSetup->set_DifferentFirstPageHeaderFooter(true);
// --- Create header for the first page. ---
pageSetup->set_HeaderDistance(20);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderFirst);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
// Set font properties for header text.
builder->get_Font()->set_Name(u"Arial");
builder->get_Font()->set_Bold(true);
builder->get_Font()->set_Size(14);
// Specify header title for the first page.
builder->Write(u"Aspose.Words Header/Footer Creation Primer - Title Page.");
// --- Create header for pages other than the first page. ---
pageSetup->set_HeaderDistance(20);
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
// Insert absolutely positioned image into the top/left corner of the header.
// Distance from the top/left edges of the page is set to 10 points.
System::String imageFileName = inputDataDir + u"Desert.jpg";
builder->InsertImage(imageFileName, RelativeHorizontalPosition::Page, 10, RelativeVerticalPosition::Page, 10, 50, 50, WrapType::Through);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Right);
// Specify header title for other pages.
builder->Write(u"Aspose.Words Header/Footer Creation Primer.");
// --- Create footer for pages other than the first page. ---
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary);
// We use table with two cells to make one part of the text on the line (with page numbering)
// To be aligned left, and the other part of the text (with copyright) to be aligned right.
builder->StartTable();
// Clear table borders.
builder->get_CellFormat()->ClearFormatting();
builder->InsertCell();
// Set the first cell to 1/3 of the page width.
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 / 3));
// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
builder->Write(u"Page ");
builder->InsertField(u"PAGE", u"");
builder->Write(u" of ");
builder->InsertField(u"NUMPAGES", u"");
// Align this text to the left.
builder->get_CurrentParagraph()->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Left);
builder->InsertCell();
// Set the second cell to 2/3 of the page width.
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 * 2 / 3));
builder->Write(u"(C) 2001 Aspose Pty Ltd. All rights reserved.");
// Align this text to the right.
builder->get_CurrentParagraph()->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Right);
builder->EndRow();
builder->EndTable();
builder->MoveToDocumentEnd();
// Add a page break to create a second page on which the primary headers/footers will be seen.
builder->InsertBreak(BreakType::PageBreak);
// Add a section break to create a third page with different page orientation.
builder->InsertBreak(BreakType::SectionBreakNewPage);
// Get the new section and its page setup.
currentSection = builder->get_CurrentSection();
pageSetup = currentSection->get_PageSetup();
// Set page orientation of the new section to landscape.
pageSetup->set_Orientation(Orientation::Landscape);
// This section does not need different first page header/footer.
// We need only one title page in the document. The header/footer for this page
// has already been defined in the previous section
pageSetup->set_DifferentFirstPageHeaderFooter(false);
// This section displays headers/footers from the previous section by default.
// Call currentSection.HeadersFooters.LinkToPrevious(false) to cancel this behaviour.
// Page width is different for the new section and therefore we need to set
// different cell widths for a footer table.
currentSection->get_HeadersFooters()->LinkToPrevious(false);
// If we want to use the already existing header/footer set for this section
// but with some minor modifications then it may be expedient to copy headers/footers
// from the previous section and apply the necessary modifications where we want them.
CopyHeadersFootersFromPreviousSection(currentSection);
// Find the footer that we want to change.
System::SharedPtrprimaryFooter = currentSection->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary);
System::SharedPtrrow = primaryFooter->get_Tables()->idx_get(0)->get_FirstRow();
row->get_FirstCell()->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 / 3));
row->get_LastCell()->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(100 * 2 / 3));
System::String outputPath = outputDataDir + u"CreateHeaderFooter.docx";
// Save the resulting document.
doc->Save(outputPath);
}
与添加类似,可以根据需要从标题,偶数和奇数页中删除页眉和页脚。以下是删除Word文档中所有页眉和页脚的步骤。
下面的示例代码显示了如何使用C ++删除Word文档中的所有页眉和页脚。
// Source and output directory paths.
System::String inputDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"SampleHeaderFooter.docx");
for (System::SharedPtr<Section> section : System::IterateOver<System::SharedPtr<Section>>(doc))
{
// Up to three different header and footers are possible in a section (for first, even and odd pages).
// We check and delete all of them.
System::SharedPtr<HeaderFooter> header;
System::SharedPtr<HeaderFooter> footer;
header = section->get_HeadersFooters()->idx_get(HeaderFooterType::HeaderFirst);
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterFirst);
if (header != nullptr)
{
header->Remove();
}
if (footer != nullptr)
{
footer->Remove();
}
// Primary header and footer is used for odd pages.
header = section->get_HeadersFooters()->idx_get(HeaderFooterType::HeaderPrimary);
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary);
if (header != nullptr)
{
header->Remove();
}
if (footer != nullptr)
{
footer->Remove();
}
header = section->get_HeadersFooters()->idx_get(HeaderFooterType::HeaderEven);
footer = section->get_HeadersFooters()->idx_get(HeaderFooterType::FooterEven);
if (header != nullptr)
{
header->Remove();
}
if (footer != nullptr)
{
footer->Remove();
}
}
// Output file path
System::String outputPath = outputDataDir + u"RemoveFooters.docx";
// Save the document.
doc->Save(outputPath);
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn