I am exporting content to word and some of the content can be text and some HTML. I have the following two requirements.
Some the style needs to be referred to an external css which might be at the location like http://domain/appname/default.css
The other requirement is to define custom styles displayed in the word ribbon. For this one of the solutions I thought was to create a template with custom styles and apply those styles dynamically when the export is performed. The issue I am facing is, since I am using builder.insertHtml, the style is not applied to the paragraph.
You can read HTML string into stream and pass this stream inside temporary Document constructor along with HtmlLoadOptions. Please implement IResourceLoadingCallback interface if you want to control how Aspose.Words loads external resource when importing a document from HTML or MHTML (see ResourceLoadingCallback Property). This creates a separate Document corresponding to a particular HTML string (remote css/resources should be reflected properly in this Document).
Now in your main Document, use DocumentBuilder.MoveTo method to move cursor to desired position where you want to insert html. Instead of using DocumentBuilder.InsertHtml,please use DocumentBuilder.InsertDocument method to insert temporary HTML Document that we created above.
Secondly yes, you can create template Word document with custom styles and attach it to your main Document by using Document.AttachedTemplate property.
Thanks for your inquiry. Could you please attach your sample HTML document here for testing? Please also share a working link pointing to a sample external CSS file that you want to load. We will investigate the issue on our end and provide you more information.
I don’t have a link for the external CSS but below is the css file content. The below css is applied when I am on a windows environment, but when I deploy it to Solaris environment it does not work. For a workaround solution I have to define constant containing the styles like highlight and h1 tag. By doing that the highlight works fine but for h1 it applies the color and font-family but not the size. Below is the code.
private static final String HIGHLIGHT_STYLE = ".highlight{background-color:#fff7d5;}";
private static final String H1_STYLE = ".h1{color:#993366;font-style:bold;font-family:verdana;font-size:18;}";
private static final String H2_STYLE = ".h2{color:#993366;font-style:bold;font-family:verdana;font-size:16;}";
private static final String H4_STYLE = ".h4{color:#993366;font-family:verdana;font-size:12;}";
Thanks for being patient. Please upgrade to the latest version of Aspose.Words for Java 17.3.0 and try using the following code (output document and correct HTML are also attached).
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
System.out.println(addCssContent() + htmlText + "");
builder.insertHtml(addCssContent() + htmlText + "");
doc.save(“D:\temp\awjava-17.3.0.docx”);
private static final String htmlText = "
Start of BODY
Text with background color
# This is heading 1
End of BODY
";
private static String addCssContent(){
String style = "";
style = "";
style += HIGHLIGHT_STYLE;
style += H1_STYLE;
style += BODY_STYLE;
style += “”;
return style ;
}
I´m having the same problem as stated in this thread.
Im trying to insert the following HTML + CSS with a DocumentBuilder using the insertHtml method:
ol,dl,ul{padding:0 40px}ol ol{list-style:lower-alpha;margin-left:1.25em}ol ol ol{list-style:lower-roman;margin-left:2.5em}ol ol ol ol{list-style:decimal}ol ol ol ol ol{list-style:lower-alpha}ol ol ol ol ol ol{list-style:lower-roman}ul ul ul ul{list-style-type:disc}ul ul ul ul ul{list-style-type:circle}ul ul ul ul ul ul{list-style-type:square}li li{padding-left:.4em}li li li{padding-left:0}
test 1
test 2
test 2 a
test 2 a i
test 2 a ii
test 2 b
test 2 c
test 2 c i
test 3
test 3 a
test 3 b
test 3 b i
test 4
The CSS rules aren´t being applied to the html.
Thank you so much for reading.
Kind regards,
Nuno Silva.
EDIT: This is no longer an issue. Thanks for the time!
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.