Bullet point aliment issue from Java 18 version to 24 version

Hello Aspsoe Team,

Recently we moved from Aspose version 18 version to Aspose version 24 word document. I am facing an issue with the bullet point alignment when replacing the placeholder with other content. It is working fine with the Aspose java words 18 version.

Here is the smaple input file - 1
Input_Source_Document.docx (113.8 KB)

Here is the smaple input file - 2
Input_placeholder.docx (16.0 KB)

Here is the output file -
Output.docx (99.4 KB)

Here is the sample code which I have wrote for the placeholder replacement -

package test.assumption;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.regex.Pattern;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.FindReplaceOptions;
import com.aspose.words.IReplacingCallback;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.ImportFormatOptions;
import com.aspose.words.License;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.ReplaceAction;
import com.aspose.words.ReplacingArgs;
import com.aspose.words.Run;


public class Test2Aspose {
    public static void main(String[] args) throws Exception {

    	System.out.println("Hello Started!!!");
        Document targetDoc = new Document("D:\\ACA33\\Aspose\\Assumption\\Input_Source_Document.docx");
        Document docWithContent1 = new Document("D:\\ACA33\\Aspose\\Assumption\\Input_placeholder.docx");
        ReplacingCallback callback = new ReplacingCallback(docWithContent1);
        FindReplaceOptions options1 = new FindReplaceOptions(callback);
		options1.setMatchCase(true);
		
		targetDoc.getRange().replace("SOWPH_Export", "", options1);
	
		targetDoc.save("D:\\ACA33\\Aspose\\Assumption\\Output.docx", com.aspose.words.SaveFormat.DOCX);
		System.out.println("Hello and completed finish!!!");
    }

    public static final String ASPOSE_LICENSE_FILE_PATH = "D:\\ACA33\\Aspose\\New Jar\\Aspose.Words.Java.lic";
	
    public static void initAsposeLicense() throws Exception {
		InputStream license = new FileInputStream(ASPOSE_LICENSE_FILE_PATH);
		if (license != null) {
			License manager = new License();
			try {
				manager.setLicense(license);
			} catch (Exception e) {
				 System.out.println("SOW_WordReplace_mxJPO:initAsposeLicense==============Error:unable to load the license");
			}
		} else {
			 System.out.println("SOW_WordReplace_mxJPO:initAsposeLicense==============Error:no Aspose.Words.lic license found.");
		}
	}
	
	static {
		Locale previous = Locale.getDefault();
		Locale.setDefault(Locale.US);
		try {
			System.out.println("Aspose license execution begins");
			Test2Aspose.initAsposeLicense();
			System.out.println("Aspose license execution end");
		} catch (Exception e) {
			System.err.println("SOW_Util_mxJPO:Error: while calling static initializer in " + " class:");
		} finally {
			Locale.setDefault(previous);
		}
	}
    
	static class ReplacingCallback implements IReplacingCallback {
		Document doc1;

		public ReplacingCallback(Document d) {
			doc1 = d;
		}

		@Override
		public int replacing(ReplacingArgs e) throws Exception {
			// This is a Run node that contains either the beginning or the complete match.
			Node currentNode = e.getMatchNode();

			// The first (and may be the only) run can contain text before the match,
			// in this case it is necessary to split the run.
			if (e.getMatchOffset() > 0)
				currentNode = splitRun((Run) currentNode, e.getMatchOffset());

			ArrayList runs = new ArrayList();

			// Find all runs that contain parts of the match string.
			int remainingLength = e.getMatch().group().length();
			while ((remainingLength > 0) && (currentNode != null)
					&& (currentNode.getText().length() <= remainingLength)) {
				runs.add(currentNode);
				remainingLength = remainingLength - currentNode.getText().length();

				// Select the next Run node.
				// Have to loop because there could be other nodes such as BookmarkStart etc.
				do {
					currentNode = currentNode.getNextSibling();
				} while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
			}

			// Split the last run that contains the match if there is any text left.
			if ((currentNode != null) && (remainingLength > 0)) {
				splitRun((Run) currentNode, remainingLength);
				runs.add(currentNode);
			}

			//// to insert Document
			DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
			builder.moveTo((Run) runs.get(runs.size() - 1));

			ImportFormatOptions opt = new ImportFormatOptions();
			opt.setSmartStyleBehavior(true);
			
			builder.insertDocument(doc1, ImportFormatMode.USE_DESTINATION_STYLES);

			for (Run run : (Iterable<Run>) runs)
				run.remove();

			// Signal to the replace engine to do nothing because we have already done all
			// what we wanted.
			return ReplaceAction.SKIP;
		}

		private Run splitRun(Run run, int position) throws Exception {
			Run afterRun = (Run) run.deepClone(true);
			afterRun.setText(run.getText().substring(position));
			run.setText(run.getText().substring((0), (0) + (position)));
			run.getParentNode().insertAfter(afterRun, run);
			return afterRun;
		}


	}
}

Above code I have wrote for the latest version Aspose jar.

The only difference between above code and the old verions code is

ImportFormatOptions opt = new ImportFormatOptions();
			opt.setSmartStyleBehavior(true);

Here is the - target output file -
Target_Output.docx (114.2 KB)

Suggest me, what changes do I need to do to achieve or fix this issue.

@ketanpbh

It appears you are experiencing alignment issues with bullet points after upgrading from Aspose.Words version 18 to version 24. This may be due to changes in how the library handles styles and formatting.

To address the bullet point alignment issue, consider the following steps:

  1. Check List Formatting: Ensure that the list formatting in your source document is correctly set up. You may need to explicitly set the list format in your code after replacing the placeholder content.

  2. Reapply List Formatting: After inserting the new content, reapply the list formatting to ensure that the bullets are aligned correctly. Here’s a snippet to illustrate how you can do this:

    // After inserting the document
    builder.ListFormat.List = targetDoc.getLists().add(Aspose.Words.Lists.ListTemplate.Bullet);
    
  3. Debugging: If the issue persists, consider debugging by checking the styles of the paragraphs before and after the replacement to identify any discrepancies.

If these suggestions do not resolve the issue, you may want to consult the Aspose documentation or community forums for more specific guidance related to bullet point formatting changes between versions.

Feel free to reach out if you have more questions!

@ketanpbh Please try using DocumentBuilder.insertDocumentInline method instead of DocumentBuilder.insertDocument. You should modify your code like this to get the desired output:

ImportFormatOptions opt = new ImportFormatOptions();
opt.setSmartStyleBehavior(true);

builder.insertDocumentInline(doc1, ImportFormatMode.KEEP_SOURCE_FORMATTING, opt);
builder.writeln();