We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.words document merge - remove blank lines

I’m evaluating aspose.words for Java before I commit to purchasing it and i’m testing it for merging the header and footer in a notepaper document into a main document. When I merge the header from the notepaper into the main document, extra blank lines are introduced. I was wondering how I can easily remove these blank lines in the merged document header/footer.

Below is the code im using to merge the notepaper header & footer into the main report document:

								//Loop through headers & footers in notepaper document  
								for(HeaderFooter notepaperHdrFtr : notepaperSection.getHeadersFooters()) {  
									//Check whether current report section contains header/footer of same type in notepaper  
									if(reportSection.getHeadersFooters().getByHeaderFooterType(notepaperHdrFtr.getHeaderFooterType())!=null) {  
										//Add header footer content from notepaper to report  
										for(Node notepaperChild : (Iterable<Node>) notepaperHdrFtr.getChildNodes()) {  
											Node reportChild = report.importNode(notepaperChild, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);	
											reportSection.getHeadersFooters().getByHeaderFooterType(notepaperHdrFtr.getHeaderFooterType()).appendChild(reportChild);  											
										}	  								
									}	  							
									else {  
										Node reportHdrFtr = report.importNode(notepaperHdrFtr, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);  
										reportSection.getHeadersFooters().add(reportHdrFtr);  
									}	  							
								}

@athavant,

Thanks for your interest in Aspose.Words for Java API. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word documents
  • Aspose.Words for Java 19.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

@awais.hafeez
I simply want to know how using the library I can identify and remove blank lines in the header & footer, do you have any example code for this? I don’t need specific help for the work im trying to do.

@athavant,

The following code will simply remove all Paragraphs (with no text inside) in Headers Footers of all the Sections in Word document:

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

for (Section sec : doc.getSections()){
    for (HeaderFooter hf : sec.getHeadersFooters()){
        for (Paragraph para : (Iterable<Paragraph>) hf.getChildNodes(NodeType.PARAGRAPH, true)){
            if (para.toString(SaveFormat.TEXT).trim().equals("")){
                para.remove();
            }
        }
    }
}

doc.save("E:\\Temp\\awjava-19.11.docx");

Hope, this helps.

Thanks for this but it hasn’t worked, within each paragraph is it possible to check for blank lines and remove them?

@athavant,

Could you please ZIP and attach your input and expected output documents? We will then share the code example according to your requirement. Thanks for your cooperation.