Word处理控件Aspose.Words功能演示:使用 C++ 在 Word 文档中添加或删除页眉和页脚

Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(qun:761297826)icon-default.png?t=N0U7https://www.evget.com/product/4116/download

Word 文档中的页眉和页脚用于格式化和显示重要信息,例如主题、章节、页码、文案等。以编程方式处理 Word 文档时,您可能需要添加或删除页眉和页脚。为此,本文将教您如何使用 C++ 在 Word 文档中添加和删除页眉和页脚。

一、用于在 Word 文档中添加和删除页眉和页脚的 C++ API

要在 Word 文档中添加页眉和页脚,我们将使用Aspose.Words for C++ API。它是一个原生 C++ API,支持创建、读取和修改 Word 文档,无需安装 Microsoft Word。您可以通过NuGet安装 API,也可以直接从“下载”部分下载。

PM> Install-Package Aspose.Words.Cpp

二、使用 C++ 在 Word 文档中添加页眉和页脚

Word 文档中的页眉和页脚分为三个部分,即标题页、偶数页和奇数页。您可以为这些部分添加不同的页眉和页脚。此外,您还可以在页眉和页脚中添加图像和表格等元素。

在此示例中,我们将创建一个新的 Word 文档并为标题页添加不同的标题。我们将在后续页面中添加带有图像的页眉和带有表格的页脚。以下是在 Word 文档中添加页眉和页脚的步骤。

  • 创建Document类的实例来表示 Word 文档。
  • 使用之前创建的Document对象创建DocumentBuilder类的实例。
  • 使用PageSetup->set_DifferentFirstPageHeaderFooter(bool value)方法指定您想要标题页的不同页眉和页脚。
  • 设置标题文本的字体属性。
  • 为后续页面创建页眉和页脚。
  • 使用DocumentBuilder->InsertImage(System::SharedPtr System::Drawing::Image image, Aspose::Words::Drawing::RelativeHorizontalPosition horzPos, double left, Aspose::Words::Drawing::RelativeVerticalPosition在标题中添加图像vertPos、双顶、双宽、双高、Aspose::Words::Drawing::WrapType wrapType) 方法。
  • 在页脚中添加表格。
  • 使用Document->Save(System::String fileName)方法保存 Word 文档。

以下示例代码演示了如何使用 C++ 在 Word 文档中添加页眉和页脚。

void CopyHeadersFootersFromPreviousSection(const System::SharedPtr<Section>& section)
{
System::SharedPtr<Section> previousSection = System::DynamicCast<Section>(section->get_PreviousSibling());

if (previousSection == nullptr)
{
return;
}

section->get_HeadersFooters()->Clear();

for (System::SharedPtr<Node> headerFooterNode : 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::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

System::SharedPtr<Section> currentSection = builder->get_CurrentSection();
System::SharedPtr<PageSetup> pageSetup = 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::SharedPtr<HeaderFooter> primaryFooter = currentSection->get_HeadersFooters()->idx_get(HeaderFooterType::FooterPrimary);

System::SharedPtr<Row> row = 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);
}

三、使用 C++ 删除 Word 文档中的页眉和页脚

在上一节中,您学习了如何在 Word 文档中添加页眉和页脚。与添加类似,您可以根据需要从标题、偶数页和奇数页中删除页眉和页脚。以下是删除 Word 文档中所有页眉和页脚的步骤。

  • 使用Document类加载 Word 文档。
  • 创建HeaderFooter类的两个实例来表示页眉和页脚。
  • 使用Section->get_HeadersFooters()->idx_get(Aspose::Words::HeaderFooterType headerFooterType)方法检索标题、偶数页和奇数页的页眉和页脚。
  • 使用HeaderFooter->Remove()方法删除页眉和页脚。
  • 使用Document->Save(System::String fileName)方法保存 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);

以上便是使用 C++ 在 Word 文档中添加或删除页眉和页脚详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值