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.