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

news/2024/11/16 17:41:29/

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 sectionpageSetup->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);

http://www.ppmy.cn/news/696572.html

相关文章

word 去除页眉默认的横线及python操作分析

word 去除页眉默认的横线 1,编辑页眉,去除格式,保存,再去除页眉文件,保存,即可去除。 python 去除,调用,操作word 有时实现不了功能时,可以通过录制宏,进行分析,基本上问题都可以解决。 Sub 页眉目() 页眉目 宏 柘城 Selection.TypeText Text:="枯模…

word修改页眉页脚一直乱动

要点 插入分页符合分节符 每个章节取消链接到前一条页眉

html 给word插入页眉和页脚,如何在Word插入页眉和页脚

在页眉和页脚可以输入创键文档的基本信息,例如在页眉中输入文档名称,章节标题或者作者名称等信息,在页脚中输入文档的创建时间、页码等,不仅能使文档更美观,还能向读者快速传递word文档要表达的信息。 步骤1&#xff1a;添加页眉和页脚。 页眉和页脚在文档资料中经常遇到,对文档…

php页眉,phpword页眉页脚

一、php 生成带有页眉的word 把你生成WORD的代码贴出来&#xff0c;我给你修改增加页眉、页脚功能。 补充&#xff1a; 没看见你的模板&#xff0c;关键是看你的模板是用什么方式在生成WORD&#xff0c;如果是使用DCOM对象或者组装XML文件&#xff0c;那么增加页眉、也脚是能做…

word 技巧 - 删除指定页页眉

1. 先在要删除页眉的页面插入分节符 2. 再点击页眉&#xff0c;在设计中将链接到前一页取消&#xff0c;再点击页眉->删除页眉即可。 注&#xff1a;某一行删除不了&#xff0c;可在视图中点击大纲进行删除。

批量删除多个word文档中的页眉页脚

批量删除多个word文档中的页眉页脚 ————————————————————————————————————————————— 首先打开 Microsoft Word ———开发者工具———Visual Basic———点击插入——&#xff08;模块&#xff09; 然后把下方代码复制进去之…

word2007删除页眉

每次写论文格式的东西总会困扰人的问题就是好多格式总忘记怎么改&#xff0c;我来做个笔记&#xff0c;方便自己下次写东西查找。 1.删除首页页眉横线 我们双击页眉处&#xff0c;进入页眉编辑状态&#xff0c;然后点击「开始」-「字体」-橡皮擦图标「清除所有格式」即可删除页…

批量word删除页眉页脚——VBS脚本,在office宏中运行即可

批量word删除页眉页脚——VBS脚本&#xff0c;在office宏中运行即可 Sub 批量删除页眉页脚() 此代码功能为列出指定文件夹中所有选取的WORD文件全路径名 Dim myDialog As FileDialog, oDoc As Document, oSec As Section Dim oFile As Variant, myRange As Range On Erro…