I am using Aspose.Words for Java v25.5 to generate documents (Docx and PDF) from DOCX templates, populated with data from JSON payloads. These templates often include HTML content, which is inserted as rich, formatted text using the <<[node.customField] -html>>
tag. The customField
contains HTML styled with CSS class rule names. For example:
<table class='highlighted-text'>
<tr><td>Name</td><td>Joe</td></tr>
<tr><td>Role</td><td>Developer</td></tr>
</table>
With the following CSS class:
.highlighted-text {
background-color: yellow;
font-weight: bold;
}
The CSS classes are provided to the service in the HTTP request as a text string. I would like to know if it’s possible to configure the document generation saving options to incorporate these CSS classes during the process of inserting the HTML content. I’ve reviewed the documentation but haven’t found any examples of how to achieve this. Is this functionality supported by Aspose.Words?
@sagaofsilence.dev
Cause Aspose.Words does not directly support applying CSS classes from HTML content when generating documents from templates. However, you can control how CSS styles are saved and referenced in the output documents. Solution To incorporate CSS classes during the document generation process, you can use the HtmlSaveOptions
class to specify how CSS styles are handled. Here’s how you can set it up: 1. Set the CSS Stylesheet Type: You can choose to export CSS as inline styles, embedded styles, or as an external stylesheet.
2. Use a CSS Saving Callback: Implement a custom callback to control how CSS styles are saved. Code Example Here’s a sample code snippet demonstrating how to set up the HtmlSaveOptions
to handle CSS classes: ```java
Document doc = new Document(“path/to/your/template.docx”); // Create HtmlSaveOptions to control the saving process
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
options.setCssStyleSheetType(CssStyleSheetType.EXTERNAL); // Use external CSS
options.setCssStyleSheetFileName(“path/to/your/styles.css”); // Specify the CSS file // Optional: Implement a custom CSS saving callback if needed
options.setCssSavingCallback(new ICssSavingCallback() { public void cssSaving(CssSavingArgs args) { // Custom logic for saving CSS // For example, save to a specific file or modify the CSS content }
}); // Save the document
doc.save(“path/to/output/document.html”, options);
- Test the output document to verify that the CSS classes are applied correctly.
- Adjust the CSS saving callback as needed to meet your specific requirements. By following these steps, you will successfully incorporate CSS classes into your document generation process. If you have any further questions or need additional assistance, feel free to reach out.
@sagaofsilence.dev The only way to achieve this is to keep css and html together, like this:
<html>
<head>
<style type="text/css">
.highlighted-text {
background-color: yellow;
font-weight: bold;
}
</style>
</head>
<body>
<table class='highlighted-text'>
<tr><td>Name</td><td>Joe</td></tr>
<tr><td>Role</td><td>Developer</td></tr>
</table>
</body>
</html>