Java版Word开发工具Aspose.Words功能解析:使用Java在Word文档中添加或删除页眉和页脚

页眉和页脚通常在文档中用于显示重要信息,例如页码,主题,章节等。可以使用Java应用程序在Word文档(DOCX / DOC)中添加,插入,删除或删除页眉和页脚。在本文中,我们将学习有关添加或删除页眉和页脚的信息。

页眉和页脚通常在文档中用于显示重要信息,例如页码,主题,章节等。您可以使用Java应用程序在Word文档(DOCX / DOC)中添加,插入,删除或删除页眉和页脚。

在本文中,我们将学习有关添加或删除页眉和页脚的信息。以下是我们将详细探讨的主题:

>>如果想要测试这项新功能,可点击这里下载最新版试用。

  • 使用Java在Word文档(DOCX / DOC)中添加页眉和页脚
  • 使用Java删除Word文档(DOCX / DOC)的页眉和页脚
  • 使用Java从Word文档(DOCX / DOC)中删除页脚
  • 使用Java从Word文档(DOCX / DOC)中删除标题

好消息来啦!整合所有格式API处理控件Aspose永久授权正在网火热销售中,新购乐享85折起!联系客服立马1分钟了解全部咨询!

①使用Java在Word文档(DOCX / DOC)中添加页眉和页脚

在Word文档(DOCX / DOC)中添加页眉和页脚是使用文字处理文档的基本但重要的用例。但是,可能会出现不同的情况。例如,您可能需要在页眉和页脚部分添加图像,表格或仅添加一些文本。此外,有时标题页上的页眉和页脚与其他页相比有所不同。有时页头和页脚在偶数页号上不同,而在奇数页号上不同。因此,我们创建了一个简洁的基本示例,在Word文档中添加页眉和页脚。

在这里,我们将在标题页(非常第一页)上插入不同的页眉页脚,并在随后的页面上插入不同的页眉页脚。而第二页将具有一个自定义的页眉页脚,其中包括图像,文本和表格元素。需要按照以下步骤使用Java在Word文档(DOCX / DOC)中添加或插入页眉和页脚。

  • 初始化Document和DocumentBuilder类对象
  • 指定您想要标题页的其他页眉页脚
  • 设置标题文本的字体属性
  • 创建标题和后续页面
  • 插入表格并设置页码格式
  • 保存输出DOCX文件

以下代码基于这些步骤,这些步骤显示了如何使用Java以编程方式在Word文档(DOCX / DOC)中添加页眉和页脚:

Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);Section currentSection = builder.getCurrentSection();PageSetup pageSetup = currentSection.getPageSetup();// 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.setDifferentFirstPageHeaderFooter(true);// --- Create header for the first page. ---pageSetup.setHeaderDistance(20);builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);// Set font properties for header text.builder.getFont().setName("Arial");builder.getFont().setBold(true);builder.getFont().setSize(14);// Specify header title for the first page.builder.write("Aspose.Words Header/Footer Creation - Title Page.");// --- Create header for pages other than first. ---pageSetup.setHeaderDistance(20);builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);// 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.String imageFileName = dataDir + "Aspose.Words.gif";builder.insertImage(imageFileName, RelativeHorizontalPosition.PAGE, 10, RelativeVerticalPosition.PAGE, 10, 50, 50, WrapType.THROUGH);builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);// Specify another header title for other pages.builder.write("Aspose.Words Header/Footer Creation");// --- Create footer for pages other than first. ---builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);// 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 bordersbuilder.getCellFormat().clearFormatting();builder.insertCell();// Set first cell to 1/3 of the page width.builder.getCellFormat().setPreferredWidth(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("Page ");builder.insertField("PAGE", "");builder.write(" of ");builder.insertField("NUMPAGES", "");// Align this text to the left.builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);builder.insertCell();// Set the second cell to 2/3 of the page width.builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 * 2 / 3));builder.write("(C) 2020 Aspose Pty Ltd. All rights reserved.");// Align this text to the right.builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);builder.endRow();builder.endTable();builder.moveToDocumentEnd();// Make page break to create a second page on which the primary headers/footers will be seen.builder.insertBreak(BreakType.PAGE_BREAK);// Save the resulting document.doc.save(dataDir + "HeaderFooter_Out.docx");

到目前为止,我们已经学习了如何在Word文件中添加或插入页眉和页脚。以下是通过“打印预览”选项看到输出时的外观的屏幕截图。当您的应用程序要在其中添加一些内容作为输出文档的页脚的应用程序处理不同的DOC / DOCX文档时,此功能会有所帮助。

Java版Word开发工具Aspose.Words功能解析:在Word(DOCX / DOC)中插入或删除注释

②使用Java删除Word文档(DOCX / DOC)的页眉和页脚

可以使用Aspose.Words for Java API删除Word文档的页眉和页脚。如上所述,在文档中可以添加三种不同类型的页眉和页脚。例如,在标题页,偶数页和奇数页上。您可以按照以下步骤删除Word文件中的所有页眉和页脚:

  • 加载源DOCX / DOC文件
  • 查找标题页上的页眉和页脚,偶数页码和奇数页码
  • 找到时删除必填部分
  • 保存输出DOCX文件

以下代码遵循这些步骤,并显示如何使用Java删除Word文档中的页眉和页脚:

Document doc = new Document(dataDir + "HeaderFooter.doc");for (Section section : doc.getSections()) {// Up to three different header footers are possible in a section (for first, even and odd pages).// We check and delete all of them.HeaderFooter footer;        HeaderFooter header;footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);       header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);if (footer != null)footer.remove();        if (header != null)header.remove();// Primary header footer is used for odd pages.footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);if (footer != null)footer.remove();        header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);if (header != null)header.remove();footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);if (footer != null)footer.remove();       header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN);if (header != null)header.remove();}doc.save(dataDir + "RemoveHeaderFooter_Out.docx");

③使用Java从Word文档(DOCX / DOC)中删除页脚

您可能希望仅删除Word文档的页脚,而使页眉保持原样。通过以下步骤可以轻松实现这些要求:

  • 加载源字文件(DOCX / DOC)
  • 遍历word文档的每个部分
  • 初始化HeaderFooter对象
  • 从输入文件中删除页脚
  • 保存更新的DOCX / DOC文件

以下代码段说明了如何使用Java从Word文档中删除页脚:

Document doc = new Document(dataDir + "HeaderFooter.doc");for (Section section : doc.getSections()) {// Up to three different footers are possible in a section (for first, even and odd pages).// We check and delete all of them.HeaderFooter footer;footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);if (footer != null)footer.remove();// Primary footer is the footer used for odd pages.footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);if (footer != null)footer.remove();footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);if (footer != null)footer.remove();}doc.save(dataDir + "RemoveFooters.docx");

④使用Java从Word文档(DOCX / DOC)中删除标题

由于我们已经学会了仅从MS Word文件中删除或删除页脚。让我们进一步探索这一步骤,您可能只需要从Word文档中删除标题即可。页脚将保持不变且不受影响,因为我们在这里的重点是仅删除页眉。以下步骤概述了为此目的要采用的过程:

  • 加载输入的Word文件(DOCX / DOC)
  • 初始化HeaderFooter类的实例
  • 从输入文档的所有部分删除标题
  • 保存更新DOCX文件

下面的代码显示了如何使用Java从Word文档中删除或删除标题:

Document doc = new Document(dataDir + "HeaderFooter.doc");for (Section section : doc.getSections()) {// Up to three different footers are possible in a section (for first, even and odd pages).// We check and delete all of them.HeaderFooter header;header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);if (header != null)header.remove();// Primary footer is the footer used for odd pages.header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);if (header != null)header.remove();header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN);if (header != null)header.remove();}doc.save(dataDir + "RemoveHeader.docx");

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群,我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年8月1日
下一篇 2020年8月1日

相关推荐

发表回复

登录后才能评论