Remove header footer in docx file

remove header and footer in docx file. Then write this code.
Because I don’t want header footer data and also page number.
Document doc = new Document(MyDir + “in.docx”);
doc.Save(MyDir + “output.txt”);
@tahir.manzoor

@rabin.samanta,

The following code should help:

Document doc = new Document("E:\\temp\\input.docx");

foreach (Section sec in doc.Sections)
{
    sec.ClearHeadersFooters();
}

doc.Save("E:\\temp\\19.2.docx");

@awais.hafeez
thanks
But i also want to remove the page number . how to remove it.

this code i am not able to build in maven.what is the dependency for it

@rabin.samanta,

Please ZIP and attach your sample Word document here for testing. We will then investigate the issue on our end and provide you more information.

You need to add Aspose.Words for .NET DLL reference in your project to be able to run this code.

test.zip (12.2 KB)
I am upload .docx and converted .txt file.
i need only body data not header and footer data in .txt file

@rabin.samanta,

The following code should help:

Document doc = new Document("E:\\test\\test.docx");

TxtSaveOptions opts = new TxtSaveOptions();
opts.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE);

doc.save("E:\\test\\awjava-19.1.txt", opts);

@awais.hafeez
i wants to use . but i am not able to use both.
TxtSaveOptions options = new TxtSaveOptions();
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
for saving file.

	Document document = new Document(filePath);
	TxtSaveOptions options = new TxtSaveOptions();
	options.setSaveFormat(com.aspose.words.SaveFormat.TEXT);
	options.setEncoding(java.nio.charset.Charset.forName("UTF-8"));

	HtmlSaveOptions saveOptions = new HtmlSaveOptions();
	saveOptions.setExportTextBoxAsSvg(true);
	saveOptions.getSaveFormat();
	
	
	options.setParagraphBreak("\n");
	options.setPreserveTableLayout(false);
	options.setPrettyFormat(true);
	document.save(name, saveOptions);

@rabin.samanta,

If you want to save to HTML as well as to TXT formats with save options, then please call the Save method twice and pass the appropriate TxtSaveOptions and HtmlSaveOptions as parameters:

document.save("out.html", htmlSaveOptions);
document.save("out.txt", txtSaveOptions);

Hope, this helps.

@awais.hafeez
thanks…