Java (482)
Headers and footers of Word documents are placed at the top and bottom of document pages respectively to show information such as page numbers, document titles, and author names. However, when the documents are printed or shared online, it may be desirable to delete this information to protect privacy. Moreover, headers and footers take up extra valuable space on printed pages and may interfere with the overall formatting of documents. Removing them can free up page space and ensure the document formatting doesn't become cluttered. This article will demonstrate how to delete headers and footers with Java programs using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.4.0</version>
</dependency>
</dependencies>
Remove Headers and Footers by Their Types
In Word documents, you can set different headers and footers for the first page, odd pages, and even pages. These types of headers and footers can be obtained through the HeaderFooter.getByHeaderFooterType(hfType) method and can be removed using the HeaderFooter.getChildObjects().clear() method.
Here is a list of the Enums and the types of headers and footers they represent.
| Enum | Description |
| HeaderFooterType.Header_First_Page | Represents first-page header |
| HeaderFooterType.Footer_First_Page | Represents first-page footer |
| HeaderFooterType.Header_Odd | Represents odd-page header |
| HeaderFooterType.Footer_Odd | Represents odd-page footer |
| HeaderFooterType.Header_Even | Represents even-page header |
| HeaderFooterType.Footer_Even | Represents even-page footer |
The detailed steps are as follows:
- Create an object of Document class.
- Load a Word document using Doucment.loadFromFile() method.
- Get the first section of the document using Document.getSections().get() method.
- Get the first-page header using Section.getHeadersFooters().getByHeaderFooterType() method and remove it using HeaderFooter.getChildObjects().clear() method.
- Remove the first-page footer using the same methods. The odd-page and even-page headers and footers can also be removed with these methods.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HeaderFooterType;
public class removeHeaderFooter {
public static void main(String[] args) {
//Create an object of Document class
Document doc = new Document();
//Load a Word document
doc.loadFromFile("Sample.docx");
//Get the first section of the document
Section section = doc.getSections().get(0);
//Get the header of the first page and remove it
HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_First_Page);
header.getChildObjects().clear();
//Get the footer of the first page and remove it
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_First_Page);
footer.getChildObjects().clear();
//Get the headers and the footers of the odd pages and remove them
//HeaderFooter header1 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Odd);
//header1.getChildObjects().clear();
//HeaderFooter footer1 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Odd);
//footer1.getChildObjects().clear();
//Get the headers and the footers of the even pages and remove them
//HeaderFooter header2 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Even);
//header2.getChildObjects().clear();
//HeaderFooter footer2 = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Even);
//footer2.getChildObjects().clear();
//Save the document
doc.saveToFile("RemoveByType.docx", FileFormat.Auto);
doc.dispose();
}
}

Remove Headers and Footers by Sections
Different sections may have different headers and footers. To remove the headers and footers of a certain section, use Document.getSections().get() method to get the section and remove the headers and footers within it using HeaderFooter.getChildObjects().clear() method.
It is important to be aware that after deleting the content of the header and footer in a section, they will be automatically changed to those of the previous section. Therefore, it is necessary to add a blank paragraph to the header and footer after deleting them to prevent them from changing automatically.
The detailed steps are as follows:
- Create an object of Document class.
- Load a Word document using Doucment.loadFromFile() method.
- Get the second section of the document using Document.getSections().get() method.
- Get the header of the second section using Section.getHeadersFooters().getHeader() method, remove the content using HeaderFooter.getChildObjects().clear() method, and add an empty paragraph to it using HeaderFooter.addParagraph() method.
- Get the footer of the second section using Section.getHeadersFooters().getFooter() method, remove the content using HeaderFooter.getChildObjects().clear() method, and add an empty paragraph to it using HeaderFooter.addParagraph() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
public class removeHeaderFooterSection {
public static void main(String[] args) {
//Create an object of Document class
Document doc = new Document();
//Load a Word document
doc.loadFromFile("Sample1.docx");
//Get the second section
Section section = doc.getSections().get(1);
//Get the header of the second section, remove the content, and add an empty paragraph
HeaderFooter header1 = section.getHeadersFooters().getHeader();
header1.getChildObjects().clear();
header1.addParagraph();
//Get the footer of the second section, remove the content, and add an empty paragraph
HeaderFooter footer1 = section.getHeadersFooters().getFooter();
footer1.getChildObjects().clear();
footer1.addParagraph();
//Save the document
doc.saveToFile("RemoveBySection.docx");
doc.dispose();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This article demonstrates how to create a scatter chart and add a trendline to it in an Excel document by using Spire.XLS for Java.
import com.spire.xls.*;
import com.spire.xls.core.IChartTrendLine;
import java.awt.*;
public class ScatterChart {
public static void main(String[] args) {
//Create a a Workbook object and get the first worksheet
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
//Rename the first worksheet and set the column width
sheet.getCellRange("A1:B1").setColumnWidth(22f);;
sheet.setName("Scatter Chart");
//Insert data
sheet.getCellRange("A1").setValue("Advertising Expenditure");
sheet.getCellRange("A2").setValue("10429");
sheet.getCellRange("A3").setValue("95365");
sheet.getCellRange("A4").setValue("24085");
sheet.getCellRange("A5").setValue("109154");
sheet.getCellRange("A6").setValue("34006");
sheet.getCellRange("A7").setValue("84687");
sheet.getCellRange("A8").setValue("17560");
sheet.getCellRange("A9").setValue ("61408");
sheet.getCellRange("A10").setValue ("29402");
sheet.getCellRange("B1").setValue("Sales Revenue");
sheet.getCellRange("B2").setValue ("42519");
sheet.getCellRange("B3").setValue("184357");
sheet.getCellRange("B4").setValue ("38491");
sheet.getCellRange("B5").setValue ("214956");
sheet.getCellRange("B6").setValue ("75469");
sheet.getCellRange("B7").setValue ("134735");
sheet.getCellRange("B8").setValue("47935");
sheet.getCellRange("B9").setValue ("151832");
sheet.getCellRange("B10").setValue ("65424");
//Set cell style
sheet.getCellRange("A1:B1").getStyle().getFont().isBold(true);
sheet.getCellRange("A1:B1").getStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B10").getStyle().setHorizontalAlignment(HorizontalAlignType.Center);
sheet.getCellRange("A2:B10").getCellStyle().setNumberFormat("\"$\"#,##0") ;
//Create a scatter chart and set its data range
Chart chart = sheet.getCharts().add(ExcelChartType.ScatterMarkers);
chart.setDataRange(sheet.getCellRange("B2:B10"));
chart.setSeriesDataFromRange(false);
//Set position of the chart.
chart.setLeftColumn(4);
chart.setTopRow(1);
chart.setRightColumn(13);
chart.setBottomRow(22);
//Set chart title and series data label
chart.setChartTitle("Advertising & Sales Relationship");
chart.getChartTitleArea().isBold(true);
chart.getChartTitleArea().setSize(12);
chart.getSeries().get(0).setCategoryLabels(sheet.getCellRange("B2:B10"));
chart.getSeries().get(0).setValues(sheet.getCellRange("A2:A10"));
//Add a trendline
IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Exponential);
trendLine.setName("Trendline");
//Set title of the x and y axis
chart.getPrimaryValueAxis().setTitle("Advertising Expenditure ($)");
chart.getPrimaryCategoryAxis().setTitle("Sales Revenue ($)");
//Save the document
workbook.saveToFile("ScatterChart.xlsx",ExcelVersion.Version2010);
workbook.dispose();
}
}

This article demonstrates how to use Spire. PDF for Java to add multiple headers to an existing PDF document in Java applications. The multiple headers on PDF means that different page has different header on the same PDF document.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class addDifferentHeaders {
public static void main(String[] args) {
String output = "output/addDifferentHeaders.pdf";
//load the sample PDF document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("Sample.pdf");
String header1 = "Add header by Spire.PDF";
String header2 = "Different header";
//define style
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD,12));
PdfBrush brush= PdfBrushes.getBlue();
Rectangle2D rect = new Rectangle2D.Float();
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(doc.getPageSettings().getSize().getWidth(),50f);
rect.setFrame(new Point2D.Float(0, 20), dimension2D);
PdfStringFormat format=new PdfStringFormat();
format.setAlignment(PdfTextAlignment.Center);
//draw header string for the first page
doc.getPages().get(0).getCanvas().drawString(header1,font,brush,rect,format);
//draw header string for the second page
format.setAlignment( PdfTextAlignment.Left);
doc.getPages().get(1).getCanvas().drawString(header2, font, brush, rect, format);
//save the document
doc.saveToFile(output, FileFormat.PDF);
}
}
