@AhmedTariq6
We have investigated the issue and found the issue happens because of wrong HTML passing. When we read the HTML file to a string variable as follows, it insert HTML into Microsoft Word Doc in Java using Aspose.Words REST API without any issue.
Sample.docx (11.2 KB) Sample_output.docx (10.6 KB)
Steps to Insert HTML into Microsoft Word Doc in JAVA
- Free sign up with aspose.cloud to get the credentials
- Create an new Maven Project in Java IDE and add Aspose.Words Cloud SDK for Java Maven dependency
- Create an instance of Aspose.Words Cloud API
- Read the HTML file in a string
- Create ReplaceWithTextRequest to Insert HTML into the Word template from cloud storage with replaceWithText API method
- Run the code and that’s it
Add HTML File in Word Document JAVA
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.aspose.words.cloud.ApiClient;
import com.aspose.words.cloud.ApiException;
import com.aspose.words.cloud.api.WordsApi;
import com.aspose.words.cloud.model.DocumentResponse;
import com.aspose.words.cloud.model.ReplaceRange.TextTypeEnum;
import com.aspose.words.cloud.model.requests.ReplaceWithTextRequest;
public class ReplaceHTMLText {
public static void main(String[] args) throws IOException, ApiException {
// TODO Auto-generated method stub
// Get Client ID and Secret from https://dashboard.aspose.cloud/
// Create an instance of Aspose.Word Cloud
WordsApi wordsApi = new WordsApi("xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxx", "https://api.aspose.cloud");
ApiClient client = wordsApi.getApiClient();
client.setConnectTimeout(12 * 60 * 1000);
client.setReadTimeout(12 * 60 * 1000);
client.setWriteTimeout(12 * 60 * 1000);
String htmlData = new String(Files.readAllBytes(Paths.get("C:\\Temp\\intialTemplate.html")));
com.aspose.words.cloud.model.ReplaceRange requestRangeText = new com.aspose.words.cloud.model.ReplaceRange();
requestRangeText.setText(htmlData);
requestRangeText.setTextType(TextTypeEnum.HTML);
try {
ReplaceWithTextRequest request = new ReplaceWithTextRequest("Sample.docx", "id0.0.0", requestRangeText, null, null, null, null, null, null);
DocumentResponse result = wordsApi.replaceWithText(request);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}