Of course! You've hit upon a very common limitation with older HTML-to-PDF renderers like the one used in iTextSharp's HTMLWorker. The CSS text-decoration: underline; property is often rendered as a simple, thin, 1-pixel line with no control over its thickness, color, or style.
The best way to solve this is to simulate the underline using a border-bottom. This gives you full control over the thickness, style, and color.
Solution: Use border-bottom on a <span>
The trick is to apply the border not to the entire table cell (<td>), but to a <span> that wraps only the text you want to be underlined. This ensures the line only extends for the length of the text itself.
Here is the recommended change to your code:
1. Update your CSS
Modify your .fixedfor style. Remove text-decoration and add border-bottom. I'd also recommend adding a little padding-bottom to give the underline some breathing room from the text, though you can adjust this to your liking.
css
<style>
.fixedfor {
/* Remove the old text-decoration */
/* text-decoration: underline; */
/* Add a border-bottom instead */
border-bottom: 2px solid black; /* <-- CONTROLS THE THICKNESS AND COLOR */
/* Optional: Adjust spacing between text and the new underline */
padding-bottom: 1px;
/* Keep your other styles */
font-weight: bold;
font-size: 1.1em;
}
</style>
Key parts of border-bottom: 2px solid black;:
2px: This is the thickness of the line. You can change this to 3px for an even thicker line, or 1.5pt if you prefer points.
solid: This is the style of the line. It could also be dotted or dashed, though solid is what you want for an underline.
black: The color of the line.
2. Update your HTML
Wrap the text inside your <td> with a <span> and apply the class to that <span>.
<span>: By applying the style to an inline element like a <span>, the border will only be as wide as the text content inside it. If you applied the border to the <td> (a block-level element), the line would stretch across the entire width of the table cell.
border-bottom: This CSS property is much better supported by HTML-to-PDF renderers and gives you the fine-grained control that text-decoration lacks.
Complete Example
Here is the complete, corrected code you should use in your Razor view.
csharp
@* Your model declaration at the top of the Razor view *@
@model YourProject.Models.YourModelName
<html>
<head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
.thick-underline {
/* This is the key part: using a border to simulate a thick underline */
border-bottom: 2px solid black;
/* Adjust this to move the line further down from the text if needed */
padding-bottom: 1px;
/* Your original styling */
font-weight: bold;
font-size: 1.1em;
}
</style>
</head>
<body>
<table width="100%" cellpadding="6" cellspacing="0">
<tr>
<td>
<span class="thick-underline">For @Model.FixedForName</span>
</td>
</tr>
</table>
</body>
</html>
I renamed the class to thick-underline for clarity, but you can keep using fixedfor if you prefer. This approach should give you the thick, bold underline you're looking for in your generated PDF.