Import HTML with css class

Hello,

I’m using linq reporting and generate document and some tag contains html. The “format -html” works fine and include the html in the generated word which mostly display fine.

But the content comes from a wysiwyg component that add a css class style. For exemple table border are defined with a css class, and once the document is generated the table doesn’t have border anymore.

Is it a way to “import css” in the word (before or after build report) so that it can be used to correctly display the table border ?

Example of HTML:

<table class="e-rte-table" style="width: 100%; min-width: 0px;">
   <thead>
      <tr>...</tr>
   </thead>
   <tbody>
      <tr><td style="width: 50%;" class="">...</td></tr>
   </tbody>
</table>

Example of css class:

.e-rte-content .e-rte-table td,
.e-rte-content .e-rte-table th {
  border: 1px solid #bdbdbd;
  height: 20px;
  min-width: 20px;
  padding: 2px 5px;
  vertical-align: middle;
}

Thank you
Fabrice

@Fabske Unfortunately, there is no way to “import css” in the word before building the report. But you can put the required CSS into the HTML. For example:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("<<[value]:\"format\" -html>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, File.ReadAllText(@"C:\Temp\in.html"), "value");

doc.Save(@"C:\Temp\out.docx");

and here is the HTML:

<html>
<head>
    <style type="text/css">
        td, th {
            border: 1px solid #bdbdbd;
        }
    </style>
</head>
<body>
    <table class="mystyle" style="width: 100%; min-width: 0px;">
        <thead>
            <tr>
                <th>test</th>
                <th>test</th>
                <th>test</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>test</td>
                <td>test</td>
                <td>test</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

Hello Alexey

I confirm your solution work thank you for your help. I’ve wrapped the table with the html that include the css definition and now it’s correcly imported.

I’ve another problem that imported html contains an image, and this image is outside of the document
Thats the blue zone:

Is it a way to force image to fit the document area ?

@Fabske When you insert the image from HTML, all image properties are read from the source HTML. Unfortunately, there is no way to “auto-fit” the image in this case. You can either insert images into your document using LINQ syntax, in this case you can define the image placeholder size and location; or postprocess you document to make sure the images do no go outside the page bounds.

@alexey.noskov Thank you but I don’t know what’s the content of the html in advance, so I cannot insert using linq syntax.
Do you have an example on how to postprocess the document ?
Thank you

@Fabske You can use LayoutCollector and LayoutEnumerator classes to calculate actual shape position and bounds and then decide whether it is required to adjust the shape bounds. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

// Get top level shapes, which are in the main document body
// LayoutCollector and LayoutEnumerator does nto work with nodes in document's header/footer.
List<Shape> shapes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
    .Where(s => s.IsTopLevel && s.GetAncestor(NodeType.HeaderFooter) == null).ToList();

// loop throug the selected shapes and reduce shape size if it goes outside the page bounds.
foreach (Shape s in shapes)
{
    // Calculate the current section bounding box.
    PageSetup ps = ((Section)s.GetAncestor(NodeType.Section)).PageSetup;
    RectangleF pageBounds = new RectangleF(
        (float)ps.LeftMargin,
        (float)ps.TopMargin,
        (float)(ps.PageWidth - ps.LeftMargin - ps.TopMargin),
        (float)(ps.PageHeight - ps.TopMargin - ps.BottomMargin));

    // Calculate the shape bounding box.
    enumerator.Current = collector.GetEntity(s);
    RectangleF shapeBounds = enumerator.Rectangle;

    // No actions required if shape rectangle is entirely inside page bounding box
    if (pageBounds.Contains(shapeBounds))
        continue;

    // Lock aspect ratio to preserve the shape proportions when resize.
    s.AspectRatioLocked= true;

    RectangleF targetRect = RectangleF.Intersect(shapeBounds, pageBounds);
    float widthRatio = targetRect.Width / shapeBounds.Width;
    float heightRatio = targetRect.Height / shapeBounds.Height;
    if (widthRatio < heightRatio)
        s.Width = targetRect.Width;
    else
        s.Height = targetRect.Height;
}

doc.Save(@"C:\Temp\out.docx");

Thank you for you code. I’ll try in the next days.

1 Like