We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Apply External CSS to Word

Hi,

I am exporting content to word and some of the content can be text and some HTML. I have the following two requirements.

  1. Some the style needs to be referred to an external css which might be at the location like http://domain/appname/default.css

  2. 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.

Regards,

Hi Carol,


Thanks for your inquiry.

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.

Hope, this helps.

Best regards,

Hi,

Can you please share an example. I am using the following code to refer to external css but getting illegeal argument exception.

LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(LoadFormat.HTML);
loadOptions.setResourceLoadingCallback(new HandleResourceLoading());
Document templateDocument = new Document(new FileInputStream(getTemplate(“Template.docx”)), loadOptions);
public class HandleResourceLoading implements IResourceLoadingCallback{
    @Override
    public int resourceLoading(ResourceLoadingArgs resourceLoadingArgs) throws RuntimeException{
    try{
        URL cssUrl = new URL(“http://domain/styles.css”);
        URLConnection urlConnection = cssUrl.openConnection();
        InputStream cssData = urlConnection.getInputStream();
        resourceLoadingArgs.setData(IOUtils.toByteArray(cssData));
    }catch(Exception ex){
        throw new RuntimeException("Can’t retrieve CSS from URL: " +
            resourceLoadingArgs.getUri(), ex);
    }
    return ResourceLoadingAction.USER_PROVIDED;
}

}

Regards,
Hi Carol,

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.

Best regards,

Hi,

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;}";
private String addCssContent(){
    String style = StringUtils.EMPTY;
    style = "";
    style += HIGHLIGHT_STYLE;
    style += H1_STYLE;
    style += "";
    return style ;
}

I call the above method before inserting html content using builder object.

builder.insertHtml(addCssContent + htmlText);

CSS Content

body
{
font-family: Verdana;
color: #333;
background-color: #fff;
margin: 20px;
}
.highlight
{
background-color:#fff7d5;
}
h1
{
font-style: bold,
color: #993366,
font-size: 18,
font-family: verdana
}

Regards,

Hi Carol,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi Carol,

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 ;
}

Hope, this helps.

Best regards,

Hey Awais Hafeez.

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!

    Hi Nuno,


    Thanks for your feedback. Please follow your other thread for any further proceedings.

    Best regards,