翻譯|使用教程|編輯:李顯亮|2021-05-19 11:01:12.383|閱讀 310 次
概述:以編程方式使用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@fc6vip.cn