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

Free Support Forum - aspose.com

How to remove the word `References` in the EndNote section of Cross-Reference using Java

Using Aspose Word for JAVA 16.2.0- JDK 1.6

Two Word document. A.docx and B.docx, each word document contains cross-reference of type EndNote and End of Section.

Using Aspose to merge both the document and moving all the cross-reference of type end note to the last page end note using below code.

Document destinationApsoseDocument=destinationDocument.getDoc();
moveEndNoteToEnd(destinationApsoseDocument);
for(Document document : mergingdocument){
Document mergingDocument=document.getDoc();        			      
moveEndNoteToEnd(mergingDocument); destinationApsoseDocument.appendDocument(mergingDocument,ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
    AsposeUtil.convertNumPageFieldsToPageRef(destinationApsoseDocument);
    destinationApsoseDocument.updatePageLayout();
    destinationApsoseDocument.save(outputFileName);
    		
    public void moveEndNoteToEnd(Document dstDoc) {
        dstDoc.getFirstSection().getPageSetup().getEndnoteOptions().setLocation(3);
        dstDoc.getFirstSection().getPageSetup().getEndnoteOptions().setNumberStyle(0);;
        dstDoc.getFirstSection().getPageSetup().getEndnoteOptions().setStartNumber(1);
        dstDoc.getFirstSection().getPageSetup().getEndnoteOptions().setRestartRule(0);
    }

The cross-reference at end note of A.docx moved to to the end of document EndNote that is B.docx and A.docx & B.docx are merged.-This is expected and working fine.

How to remove the word text Reference from A.docx after merging both the document ?

Note:

The reference are moved to end of document but the word text Reference remains as it is.It is not getting removed.

@SHOME

You can remove the text Reference from A.docx before joining documents using Range.Replace method. Please replace this word with empty string. We suggest you please read the following article.
Find and Replace

I tried using the code mergingDocument.getRange().replace("References", "", true, true); but the code is replacing all the text References in the entire word file. In my case I want remove only the text References from all the pages of the document except the last page. As we are moving the EndNote cross-reference to the last page. So last page References text should display.

@SHOME

You are using old version of Aspose.Words. We suggest you please use the latest version of Aspose.Words for Java 20.5 and following code example to achieve your requirement.

Document docA = new Document(MyDir + "A.docx");
Document docB = new Document(MyDir + "B.docx");
docA.getRange().replace("References", "", new FindReplaceOptions());
docA.appendDocument(docB, ImportFormatMode.KEEP_SOURCE_FORMATTING);

RepalceReferences callback = new RepalceReferences();
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.BACKWARD);
options.setReplacingCallback(callback);
docA.getRange().replace("References", "", options);
docA.save(MyDir + "20.5.docx");

public class RepalceReferences implements IReplacingCallback {
int match = 1;
public int replacing(ReplacingArgs e) throws Exception {

    if(match == 1)
    {
        match ++;
        return ReplaceAction.SKIP;
    }

    return ReplaceAction.REPLACE;
}
}

The above code replaces all the text References in the word file except the last page. The actual expectation is to replace References with blank empty text just above the EndNote of the document.

Is this text References is normal text as it is replacing all the occurrences in the word file ?

Can we identify the text References just above the EndNote and replace it ?

Please any and all help appreciated.

@SHOME

In this case, we suggest you following solution.

  1. Please get the last node of document using Document.LastSection.LastChild property. Please repeat following three steps until you get the desired paragraph.
  2. Use Node.PreviousPreOrder method to get the previous node.
  3. If the node type is paragraph, please get the text of paragraph using Node.ToString method.
  4. If paragraph’s text is “References”, please use Node.Remove method to remove it.

Hope this helps you.

Could you plz share sample code snippet ?

@SHOME

Please use the following code example to get the desired output. If you still face problem, please share the final output document that your application generates (document contains all “References” text). We will then provide you more information about your query.

Document doc = new Document(MyDir + "input.docx");

Node node = doc.LastSection.Body.LastParagraph;

while (node != null)
{
    if (node.ToString(SaveFormat.Text).Trim() == "References")
    {
        node.Remove();
        break;
    }
    node= node.PreviousPreOrder(doc);
}
doc.Save(MyDir + "20.6.docx");