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

Free Support Forum - aspose.com

Inject style sheet in aspose.words for .NET

Hii Team,

I am converting HTML to word by using Aspose.words. For that, I am going to inject a style sheet for the document.

//this method is used to inject the style sheet

private HtmlDocument AddCssLinks(IDictionary<string, string> options, HtmlDocument htmlDocument)
{
    if (options.TryGetValue(ContentsConstants.CONVERT_OPT_CSS, out string cssReference))
    {
        var links = cssReference.Split(',', '|');
        var linknode = htmlDocument.CreateElement("link");
        linknode.SetAttributeValue("href", links[0]);
        linknode.SetAttributeValue("rel", "stylesheet");

        var htmlHead = htmlDocument.DocumentNode.SelectSingleNode("//head");
        htmlHead.AppendChild(linknode);
    }
    return htmlDocument;

}
//this is my main method ,which is used to call the above method. 

public List<byte[]> Convert(ContentsDto content, IDictionary<string, string> options, System.Func<DependentContent, Task<ContentsDto>> GetDependency = null)
{
    //TODO:load license from application startup
    License htmlLicense1 = new License();
    htmlLicense1.SetLicense("Aspose.Words.NET.lic");
    var byteArray = new List<byte[]>();
    using (var dataStream = new MemoryStream(content.Data))
    {
        HtmlLoadOptions docOptions = new HtmlLoadOptions
        {
            ResourceLoadingCallback = new AsposeWordsAuthHandler(_settingsProvider, _authSettings, _logger)
        };

        var document = new Aspose.Words.Document(dataStream, docOptions);
        using (var outputStream = new MemoryStream())
        {
            HtmlDocument htmlDocument = new HtmlDocument();

            using (var htmlStream = new MemoryStream(content.Data))
            {
                htmlDocument.Load(htmlStream);
            }

            SetImageLayout(document, htmlDocument);
            SetupHeader(document, GetDependency);
            SetupFooter(document);

            // AddCssLinks Method
            var modifiedHTML = AddCssLinks(options, htmlDocument);
            //
            document.Save(outputStream, SaveFormat.Docx);
            Document doc2 = new Document(outputStream);
            using (var outputStream2 = new MemoryStream())
            {
                SetImageMargins(doc2);
                doc2.Save(outputStream2, SaveFormat.Docx);
                byteArray.Add(outputStream2.ToArray());
            }
        }
    }
    return byteArray;
}

this is not working. Do I need to convert returned HTML in the first method to the document??? How Can I further process that method in the main method???

@nethmi As I can see in your code you make changes to HtmlDocument instance, but it is not related to Aspose.Words.Document instance.
Pseudo-code of what your code should look like is the following:

HtmlDocument sourceHTML= LoadHtml(content.Data);
HtmlDocument modifiedHTML = AddCssLinks(options, sourceHTML);
using(MemoryStream modifiedHtmlStream = new MemoryStream)
{
    // Save the modified HTML into the stream.
    modifiedHTML.Save(modifiedHtmlStream);
    modifiedHtmlStream.Position = 0;
    // Load the modified HTML document in Aspose.Words.Document.
    Document doc = new Document(modifiedHtmlStream);
    // Process Aspose.Words.Document instance.
    // .......................
}
1 Like

Hii

Now it’s working. Thank you very much

1 Like