Word开发工具Aspose.Words功能演示:在C ++中以编程方式在Word文档中添加或删除页眉和页脚

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

让我们探索以下有关的内容:

  • 使用C ++在Word文档中添加页眉和页脚
  • 使用C ++删除Word文档中的页眉和页脚

要在Word文档中添加页眉和页脚,我们将使用Aspose.Words for C ++ 它是本机C ++ API,支持创建,读取和修改Word文档,而无需安装Microsoft Word。
Aspose.Words for C ++ 下载(qun:761297826)https://www.evget.com/product/4114/download


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

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

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

  • 创建Document类的实例来表示Word文档。
  • 使用 先前创建的Document对象创建DocumentBuilder类 的实例 。
  • 使用PageSetup-> set_DifferentFirstPageHeaderFooter(bool value)方法指定标题页面需要不同的页眉和页脚。
  • 设置标题文本的字体属性。
  • 为后续页面创建页眉和页脚。
  • 使用DocumentBuilder->InsertImage(System::SharedPtrimage, Aspose::Words::Drawing::RelativeHorizontalPosition horzPos, double left, Aspose::Words::Drawing::RelativeVerticalPosition vertPos, double top, double width, double height, Aspose::Words::WrapType wrapType) 方法将图片加入页眉。
  • 在页脚中添加表格。
  • 使用Document->Save(System::String fileName)方法保存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);
}

使用C ++删除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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值