Hello,
Consider the document has this style ( present in styles.xml ):
<w:style w:type="character" w:styleId="CustomEmphasis">
<w:name w:val="Custom Emphasis"/>
<w:basedOn w:val="DefaultParagraphFont"/>
<w:uiPriority w:val="21"/>
<w:qFormat/>
<w:rsid w:val="00810B23"/>
<w:rPr>
<w:i/>
<w:iCs/>
<w:color w:val="0F4761" w:themeColor="accent1" w:themeShade="BF"/>
</w:rPr>
</w:style>
When this docx is converted to html, the span class uses the Id of the style like so,
<span class="CustomEmphasis">
How do we match the id back to the style ? Because, Document.styles or Style object in java only exposes style name and not the style id.
Just removing space cannot be an option, because I have seen style definitions like so,
<w:style w:type="table" w:default="1" w:styleId="TableNormal">
<w:name w:val="Normal Table"/>
<w:uiPriority w:val="99"/>
Here the name & id are completely different and removing space will not provide a match.
I have tried using StyleIdentifier, but that is a mis-match too …
TABLE_NORMAL
Thanks !
@kml2020
To access the style ID in Aspose.Words, you can utilize the Style
class, which represents a single style in the document. However, the Document.styles
or Style
object does not directly expose the style ID.
To match the style ID back to the style name, you can iterate through the styles in the document and check their properties. Here’s a sample code snippet in Java that demonstrates how to retrieve the style ID along with the style name:
import com.aspose.words.*;
public class StyleIdExample {
public static void main(String[] args) throws Exception {
// Load the document
Document doc = new Document("YourDocument.docx");
// Iterate through styles
for (Style style : doc.getStyles()) {
// Print style name and style ID
System.out.println("Style Name: " + style.getName() + ", Style ID: " + style.getStyleIdentifier());
}
}
}
In this code, style.getStyleIdentifier()
will provide the identifier for the style, which you can then use to match with the corresponding HTML class name.
If you encounter specific cases where the style name and ID differ significantly, consider maintaining a mapping in your application to correlate them based on your specific requirements.
Please implement this solution, and let me know if you need further assistance!
@kml2020 Aspose.Words does not store styleId value from the markup in the model, it is used during styles.xml
parsing step as a backup in case of an empty “name”, and it is also used for style.Istd mapping. When the document is being written, styleId value is generated from the current Style.Name by removing spaces and non-alphabetic characters from the name. MS Word does the same, if we manually fix styleId in the markup, the first time the MS Word or Aspose.Words document is being rewritten, styleId name is being rewritten as well.
VBA returns the same as we return in Style.Name , there is no separate API to get styleId .
The following code can be suggested to generate styleId from style name.
private static String getStyleId(String styleName)
{
// Compile the regex pattern
Pattern makeStyleId = Pattern.compile("[^-a-zA-Z0-9]*");
// Replace all matches with an empty string
String styleId = makeStyleId.matcher(styleName).replaceAll("");
// Return result or "a" if empty
return !styleId.isEmpty() ? styleId : "a";
}
Hi @alexey.noskov, I am taking a similar approach, but may not work in all cases. I have a document that has these entries in styles.xml. Here in both cases, I will get the first entry, and not as expected:
<w:style w:type="character" w:styleId="IntenseReference">
<w:name w:val="Intense Reference"/>
<w:basedOn w:val="DefaultParagraphFont"/>
<w:uiPriority w:val="32"/>
<w:qFormat/>
<w:rsid w:val="00810B23"/>
<w:rPr>
<w:b/>
<w:bCs/>
<w:smallCaps/>
<w:color w:val="0F4761" w:themeColor="accent1" w:themeShade="BF"/>
<w:spacing w:val="5"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:customStyle="1" w:styleId="IntenseReference0">
<w:name w:val="IntenseReference"/>
<w:basedOn w:val="Normal"/>
<w:qFormat/>
<w:rsid w:val="006C06FF"/>
<w:rPr>
<w:i/>
<w:color w:val="0070C0"/>
</w:rPr>
</w:style>
@kml2020 As it was mentioned above, Aspose.Words does not store styleId value from the markup in the model. the value is generated from the style name on demand.