facebooktwittermenuarrow-up

GemBox Support Forum

Using external css

Base on this support ticket we can use external css, Support internal and external CSS in HTML - Community / GemBox.Document Feature Request - GemBox Support Center, but how exaclty would that work? Where would the CSS file go, and does GemBox look for the css file? Do I use relative paths?

Hi Jack,

There is nothing special that you need to do, you just need to add the external CSS to your HTML.

For example:

<head>
  <link rel="stylesheet" href="mystyle.css">
</head>

The path can be absolute or relative.

But note that if the path is relative and you’re loading the HTML from Stream then you’ll need to specify the base address so that GemBox.Document is able to resolve that relative path.

In other words, you’ll need to use something like this:

Stream htmlStream = ...
var htmlOptions = new HtmlLoadOptions() { BaseAddress = "https://example.com/" };
var document = DocumentModel.Load(htmlStream, htmlOptions);

Regards,
Mario

I might be doing something wrong the style is not being applied. What I am doing is grabbing the entire html document via javascript document.documentElement.outerHTML then passing that as a string to my method. Then in my method the document is stored in a variable called, myHhtml, I create the document like this DocumentModel myDocument = DocumentModel.Load(ContentRootPath + filePath); var bookmark = myDocument.Bookmarks["HtmlBookmark"]; myDocument.GetContent(true).LoadText(Hhtml, LoadOptions.HtmlDefault);
Is this the correct way to do it?

Hi Jack,

Can you show me a sample of that myHhtml?

Is it possible that your CSS links have a relative path?
If that is the case, then you need to replace that LoadOptions.HtmlDefault with something like the htmlOptions object that I suggested in the previous post (you need to set the BaseAddress).

Regards,
Mario

Yes I am using relative path, but I am not loading the docuement vai stream. I am getting the html page in text form and adding that to the word document. myHtml is as below

<html lang="en" class="fontawesome-i2svg-active fontawesome-i2svg-complete">
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title> - </title>
  <title> - </title>
  <style type="text/css">svg:not(:root).svg-inline--fa{overflow:visible}.svg-inline--fa{display:inline-block;font-size:inherit;height:1em;overflow:visible;vertical-align:-.125em}.svg-inline--fa.fa-lg{vertical-align:-.225em}.svg-inline--fa.fa-w-1{width:.0625em}.svg-inline--fa.fa-w-2{width:.125em}.svg-inline--fa.fa-w-3{width:.1875em}.svg-inline--fa.fa-w-4{width:.25em}.svg-inline--fa.fa-w-5{width:.3125em}.svg-inline--fa.fa-w-6{width:.375em}.svg-inline--fa.fa-w-7{width:.4375em}.svg-inline--fa.fa-w-8{width:.5em}.svg-inline--fa.fa-w-9{width:.5625em}.svg-inline--fa.fa-w-10{width:.625em}.svg-inline--fa.fa-w-11{width:.6875em}.svg-inline--fa.fa-w-12{width:.75em}.svg-inline--fa.fa-w-13{width:.8125em}.svg-inline--fa.fa-w-14{width:.875em}.svg-inline--fa.fa-w-15{width:.9375em}.svg-inline--fa.fa-w-16{width:1em}.svg-inline--fa.fa-w-17{width:1.0625em}.svg-inline--fa.fa-w-18{width:1.125em}.svg-inline--fa.fa-w-19{width:1.1875em}.svg-inline--fa.fa-w-20{width:1.25em}.svg-inline--fa.fa-pull-left{margin-right:.3em;width:auto}.svg-inline--fa.fa-pull-right{margin-left:.3em;width:auto}.svg-inline--fa.fa-border{height:1.5em}.svg-inline--fa.fa-li{width:2em}.svg-inline--fa.fa-fw{width:1.25em}.fa-layers svg.svg-inline--fa{bottom:0;left:0;margin:auto;position:absolute;right:0;top:0}.fa-layers{display:inline-block;height:1em;position:relative;text-align:center;vertical-align:-.125em;width:1em}.fa-layers svg.svg-inline--fa{-webkit-transform-origin:center center;transform-origin:center center}.fa-layers-counter,.fa-layers-text{display:inline-block;position:absolute;text-align:center}.fa-layers-text{left:50%;top:50%;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);-webkit-transform-origin:center center;transform-origin:center center}.fa-layers-counter{background-color:#ff253a;border-radius:1em;-webkit-box-sizing:border-box;box-sizing:border-box;color:#fff;height:1.5em;line-height:1;max-width:5em;min-width:1.5em;overflow:hidden;padding:.25em;right:0;text-overflow:ellipsis;top:0;-webkit-transform:scale(.25);transform:scale(.25);-webkit-transform-origin:top right;transform-origin:top right}.fa-layers-bottom-right{bottom:0;right:0;top:auto;-webkit-transform:scale(.25);transform:scale(.25);-webkit-transform-origin:bottom right;transform-origin:bottom right}.fa-layers-bottom-left{bottom:0;left:0;right:auto;top:auto;-webkit-transform:scale(.25);transform:scale(.25);-webkit-transform-origin:bottom left;transform-origin:bottom left}.fa-layers-top-right{right:0;top:0;-webkit-transform:scale(.25);transform:scale(.25);-webkit-transform-origin:top right;transform-origin:top right}.fa-layers-top-left{left:0;right:auto;top:0;-webkit-transform:scale(.25);transform:scale(.25);-webkit-transform-origin:top left;transform-origin:top left}.fa-lg{font-size:1.3333333333em;line-height:.75em;vertical-align:-.0667em}.fa-xs{font-size:.75em}.fa-sm{font-size:.875em}.fa-1x{font-size:1em}.fa-2x{font-size:2em}.fa-3x{font-size:3em}.fa-4x{font-size:4em}.fa-5x{font-size:5em}.fa-6x{font-size:6em}.fa-7x{font-size:7em}.fa-8x{font-size:8em}.fa-9x{font-size:9em}.fa-10x{font-size:10em}.fa-fw{text-align:center;width:1.25em}.fa-ul{list-style-type:none;margin-left:2.5em;padding-left:0}.fa-ul>li{position:relative}.fa-li{left:-2em;position:absolute;text-align:center;width:2em;line-height:inherit}.fa-border{border:solid .08em #eee;border-radius:.1em;padding:.2em .25em .15em}.fa-pull-left{float:left}.fa-pull-right{float:right}.fa.fa-pull-left,.fab.fa-pull-left,.fal.fa-pull-left,.far.fa-pull-left,.fas.fa-pull-left{margin-right:.3em}.fa.fa-pull-right,.fab.fa-pull-right,.fal.fa-pull-right,.far.fa-pull-right,.fas.fa-pull-right{margin-left:.3em}.fa-spin{-webkit-animation:fa-spin 2s infinite linear;animation:fa-spin 2s infinite linear}.fa-pulse{-webkit-animation:fa-spin 1s infinite steps(8);animation:fa-spin 1s infinite steps(8)}@-webkit-keyframes fa-spin{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes fa-spin{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}.fa-rotate-90{-webkit-transform:rotate(90deg);transform:rotate(90deg)}.fa-rotate-180{-webkit-transform:rotate(180deg);transform:rotate(180deg)}.fa-rotate-270{-webkit-transform:rotate(270deg);transform:rotate(270deg)}.fa-flip-horizontal{-webkit-transform:scale(-1,1);transform:scale(-1,1)}.fa-flip-vertical{-webkit-transform:scale(1,-1);transform:scale(1,-1)}.fa-flip-both,.fa-flip-horizontal.fa-flip-vertical{-webkit-transform:scale(-1,-1);transform:scale(-1,-1)}:root .fa-flip-both,:root .fa-flip-horizontal,:root .fa-flip-vertical,:root .fa-rotate-180,:root .fa-rotate-270,:root .fa-rotate-90{-webkit-filter:none;filter:none}.fa-stack{display:inline-block;height:2em;position:relative;width:2.5em}.fa-stack-1x,.fa-stack-2x{bottom:0;left:0;margin:auto;position:absolute;right:0;top:0}.svg-inline--fa.fa-stack-1x{height:1em;width:1.25em}.svg-inline--fa.fa-stack-2x{height:2em;width:2.5em}.fa-inverse{color:#fff}.sr-only{border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px}.sr-only-focusable:active,.sr-only-focusable:focus{clip:auto;height:auto;margin:0;overflow:visible;position:static;width:auto}.svg-inline--fa .fa-primary{fill:var(--fa-primary-color,currentColor);opacity:1;opacity:var(--fa-primary-opacity,1)}.svg-inline--fa .fa-secondary{fill:var(--fa-secondary-color,currentColor);opacity:.4;opacity:var(--fa-secondary-opacity,.4)}.svg-inline--fa.fa-swap-opacity .fa-primary{opacity:.4;opacity:var(--fa-secondary-opacity,.4)}.svg-inline--fa.fa-swap-opacity .fa-secondary{opacity:1;opacity:var(--fa-primary-opacity,1)}.svg-inline--fa mask .fa-primary,.svg-inline--fa mask .fa-secondary{fill:#000}.fad.fa-inverse{color:#fff}</style>
  <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/site.css">
  <link rel="stylesheet" href="/css/fontawesome/all.min.css">
 </head>
    <body>
             <form id="budgetForm">
            <table class="table table-hover table-bordered">
              <thead>
                 <tr>
                    <th scope="col"></th>
                    <th scope="col">Budget Line Item</th>
                    <th scope="col">Budget</th>
                    <th scope="col">Narrative (explain how dollar amount is the right amount for the Scope of Work, per line item)</th>
                 </tr>
              </thead>
              <tbody>
                 <tr>
                    <td>A.</td>
                    <td colspan="3">
                       <b>Personnel</b> (list positions, FTE % on project, rate)
                    </td>
                 </tr>
                 <tr>
                    <td onclick="AddRow(this)">
                       <span data-num-row="0" data-row-type="management">
                          <svg class="svg-inline--fa fa-plus fa-w-12" aria-hidden="true" focusable="false" data-prefix="fal" data-icon="plus" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" data-fa-i2svg="">
                             <path fill="currentColor" d="M376 232H216V72c0-4.42-3.58-8-8-8h-32c-4.42 0-8 3.58-8 8v160H8c-4.42 0-8 3.58-8 8v32c0 4.42 3.58 8 8 8h160v160c0 4.42 3.58 8 8 8h32c4.42 0 8-3.58 8-8V280h160c4.42 0 8-3.58 8-8v-32c0-4.42-3.58-8-8-8z"></path>
                          </svg>
                          <!-- <i class="fal fa-plus"></i> Font Awesome fontawesome.com -->
                       </span>
                    </td>
                    <td>Management</td>
                    <td>
                       <span class="managementTotal"></span>
                    </td>
                    <td onclick="ShowControl(this)">
                       <span></span>
                       <textarea name="ManagementTextarea[0]" class="management narrative d-none" onblur="ShowText(this)"></textarea>
                    </td>
                 </tr>
                 <tr style="padding:5%">
                    <td colspan="4"></td>
                 </tr>
              </tbody>
           </table>
        </form>
  <script src="/lib/jquery/dist/jquery.min.js"></script>
  <script src="/lib/jquery-formatCurrency/jquery.formatCurrency.js"></script>
  <script src="/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="/js/site.js?v=lt9dIbXqk5CTdaJg3Ld74BAviZioVKxRAIFO3jGLI6I"></script>
     <script src="/js/fontawesome/all.min.js"></script>
     <script src="/js/mustache/mustache.min.js"></script>
   </body>
</html>

Hi,

The same problem will occur with LoadText as well because GemBox.Document doesn’t know the location of your HTML.

In other words, only when you’re using DocumentModel.Load(path) then GemBox.Document knows from where the HTML is coming from because you gave it the file’s path.
But when you’re loading a text, there is no file that’s being loaded.

I hope this explains things better.

Anyway, please try the previous suggestion and let me know if you need anything else.

Regards,
Mario

It did not work. I do have a small working example, but I am not sure how to send it to you?

You can create a support ticket and attach the project to it.