I have a string that I am concatenating so as to then generate a pdf using C# and ITextSharp. I have some values such as paymentId from my model that I would like to also display on the pdf.
The pdf is generated successfully until I try to add the values from my model e.g "onlineTransactionViewModel.OnlineTransaction.PaymentId"
var example_html = #"<html>
<body style = 'font-family: Helvetica Neue, Helvetica, Helvetica, Arial, sans-serif; text-align: center; color: #777'>;
<div class='invoice- box'>
<table>
<tr class='top'>
<td colspan=""5"">
<table>
<tr>
<td colspan= ""3"">'onlineTransactionViewModel.OnlineTransaction.PaymentId' ""</br>"" ""onlineTransactionViewModel.OnlineTransaction.PayFastReference"" ""</br>"" ""onlineTransactionViewModel.OnlineTransaction.PayFastReference""
<td class= ""title"" style = 'text - align:right'></ td >
</ tr >
</ table >
</ table >
</ div >
</ body >
</ html>";
using (var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc))
{
//HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
using (var sr = new StringReader(example_html))
{
//Parse the HTML
htmlWorker.Parse(sr);
}
}
doc.Close();
You need to use string interpolation.
From C# 6, string interpolation could be combined with string literals by appending "$#" to a string.
When using string interpolation, you need to contain the content that you want to reference code with "{" and "}".
If you update your string to the following, you should get the intended result:
var example_html = $#"<html>
<body style = 'font-family: Helvetica Neue, Helvetica, Helvetica, Arial, sans-serif; text-align: center; color: #777'>;
<div class='invoice- box'>
<table>
<tr class='top'>
<td colspan=""5"">
<table>
<tr>
<td colspan= ""3"">'{onlineTransactionViewModel.OnlineTransaction.PaymentId}' ""</br>"" ""{onlineTransactionViewModel.OnlineTransaction.PayFastReference}"" ""</br>"" ""{onlineTransactionViewModel.OnlineTransaction.PayFastReference}""
<td class= ""title"" style = 'text - align:right'></ td >
</ tr >
</ table >
</ table >
</ div >
</ body >
</ html>";
Related
I'm trying to use Regex to extract all image sources from html string. For couple reasons I cannot use HTML Agitility Pack.
I need to extract 'gfx/image.png' from strings which looks like
<table cellpadding="0" cellspacing="0" border="0" style="height:350px; margin:0; background: url('gfx/image.jpg') no-repeat;">
<table cellpadding="0" cellspacing="0" border="0" background="gfx/image.jpg" style=" width:700px; height:250px; "><tr><td valign="middle">
you can use this regex: (['"])([^'"]+\.jpg)\1
then get Groups[2], this code is worked fine:
var str = #"<table cellpadding=""0"" cellspacing=""0"" border=""0"" style=""height:350px; margin:0; background: url('gfx/image.jpg') no-repeat;"">
<table cellpadding=""0"" cellspacing=""0"" border=""0"" background=""gfx/image.jpg"" style="" width:700px; height:250px; ""><tr><td valign=""middle"">";
var regex = new Regex(#"(['""])([^'""]+\.jpg)\1");
var match = regex.Match(str);
while (match.Success)
{
Console.WriteLine(match.Groups[2].Value);
match = match.NextMatch();
}
The HTTP GET response for a request is like below
<html>
<head> <script type="text/javascript">----</script> <script type="text/javascript">---</script> <title>Detailed Notes</title>
</head>
<body style="background-color: #FFFFFF; border-width: 0px; font-family: sans-serif; font-size: 13; color: #000000"> <p>this is one note </p> </body> </html>
I am getting this as a string and i have to read the body part out of it.
I tried HtmlAgility pack, but HTML parsing is getting failed due to some specials in the html content (I think something from the commented script causing this issue).
So to read the tag content i am thinking of a SubString operation.
Like SubString from the beginning of <body tag.
How can we do SubString from the beginning of a word from a text?
Using a simple SubString() with IndexOf() + LastIndexOf():
string BodyContent = input.Substring(0, input.LastIndexOf("</body>") - 1).Substring(input.IndexOf("<body"));
BodyContent = BodyContent.Substring(BodyContent.IndexOf(">") + 1).Trim();
This will return:
<p> this is one note </p>
string FullBody = input.Substring(0, input.LastIndexOf("</body>") + 7).Substring(input.IndexOf("<body")).Trim();
This will return:
<body style = background-color: #FFFFFF; border-width: 0px; font-family: sans-serif; font-size: 13; color: #000000' >< p > this is one note </p> </body>
The " will cause a problme so you need to replace every " after you get the request source
WebClient client = new WebClient(); // make an instance of webclient
string source = client.DownloadString("url").Replace("\"",",,"); // get the html source and escape " with any charachter
string code = "<body style=\"background-color: #FFFFFF; border-width: 0px; font-family: sans-serif; font-size: 13; color: #000000\"> <p>this is one note </p> </body>";
MatchCollection m0 = Regex.Matches(code, "(<body)(?<body>.*?)(</body>)", RegexOptions.Singleline); // use RE to get between tags
foreach (Match m in m0) // loop through the results
{
string result = m.Groups["body"].Value.Replace(",,", "\""); // get the result and replace the " back
}
I am dynamically generating pdf from HTML which has content that might run across pages.I am using IE11.
When there is a table that runs to multiple pages, the bottom border of the last row is not shown in each broken page. The bottom border appears only in the last page or where the table is closing.
Is there any way to apply bottom border in this case.
You just need to put javascript part and (border-collapse) in css. I have written other css just for styling.
var $foo = $('#foo');
var bodyLastRow = $foo.children('tbody').children('tr:last-child');
bodyLastRow.css({
'border-bottom': '1px solid #555'
});
#foo {
border-collapse: collapse;
}
#foo thead tr {
background: rgba(0,255,0,0.6);
}
#foo tbody tr {
background: rgba(0,255,0,0.3);
}
#foo tbody tr td, #foo thead tr td {
padding: 5px;
}
<table id="foo">
<thead>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
</thead>
<tbody>
<tr>
<td>Ashish</td>
<td>ashish#gmail.com</td>
</tr>
<tr>
<td>Asdh</td>
<td>asdh#outlook.com</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$('table#foo tbody tr:last-child').css({
border-bottom: #555
});
I need to get rid of the borders around the individual checkboxes that are rendered by a CheckBox control. Here's what it looks like now:
The ASP.Net markup is straightforward:
<asp:CheckBoxList ID="cblEthnicity" runat="server" RepeatDirection="Vertical"
RepeatColumns="3" RepeatLayout="Table" BorderStyle="None" BorderWidth="0">
</asp:CheckBoxList>
which is in a cell in a table with the class formTable applied (see below).
As you can see, I've tried setting the attributes BorderStyle="None" and BorderWidth="0" to no effect.
I'm pretty sure that what's behind this is the following CSS, which puts rounded corner borders around the enclosing table cells, which I want to keep:
.formTable
{
background-color: #eeeeee;
border: solid 1px #bbbbbb;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border-radius: 7px;
}
.formTable tr, .formTable tr td, .formTable tr th
{
background-color: #eeeeee;
padding: 3px;
border: solid 1px #bbbbbb;
vertical-align: top;
}
I added the following CSS, which also did nothing:
.formTable tr td input[type="checkbox"]
{
border: none;
}
Finally, the HTML rendered from the .aspx for the CheckBoxList, as seen in Chrome DevTools, looks like this (edited a little for brevity):
<table id="main_cblEthnicity" style="border-width:0px; border-style:None; border-top-left-radius:5px; border-top-right-radius:5px; border-bottom-left-radius:5px; border-bottom-right-radius:5px;">
<tbody>
<tr>
<td style="border-top-left-radius:5px; border-top-right-radius:5px; border-bottom-left-radius:5px; border-bottom-right-radius:5px;">
<input id="main_cblEthnicity_0" type="checkbox" name="ctl00$main$cblEthnicity$0"
checked="checked" value="Native American" />
<label for="main_cblEthnicity_0">Native American</label>
</td>
...
</tr>
</tbody>
</table>
Any suggestions on how I can get rid of the unwanted borders?
UPDATE: Here are some images to make it more clear what's going on and what I'm trying to accomplish:
This is what I'm getting now:
This is what I get if I use either suggestion that has been presented so far:
This is what I'm trying to achieve:
In addition to the suggestions made here, I tried adding this to the CSS, but it made no difference:
.formTable tr td > input[type="checkbox"] {
border: none;
}
I also tried this in Javascript/jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('.formTable tr td > input[type="checkbox"]').removeAttr("border");
});
</script>
The problem isn't the input but in it's td.
Look:
<td style="border-top-left-radius:5px; border-top-right-radius:5px; border-bottom-left-radius:5px; border-bottom-right-radius:5px;">
Here (above) is defined the border radius. And here (below) the border color:
.formTable tr, .formTable tr td, .formTable tr th
{
background-color: #eeeeee;
padding: 3px;
border: solid 1px #bbbbbb;
vertical-align: top;
}
So, to change this, you may want to add just after the above CSS code, this:
.formTable tr td
{
border:0;
}
Doing this, you'll just make the td borders to disappear and not the borders of tr or th
UPDATE AFTER OP's CLARIFICATIONS
Oh, all right. Now with those new screenshots we can see well what you're tryning to do achieve.
Anyway, you're still trying to remove a border from the input, but I repeat, the problem isn't the input but it's td.
I'll explain you with the code you gave us ok? So:
<table id="main_cblEthnicity" style="border-width:0px; border-style:None; border-top-left-radius:5px; border-top-right-radius:5px; border-bottom-left-radius:5px; border-bottom-right-radius:5px;">
<tbody>
<tr>
<td style="border-top-left-radius:5px; border-top-right-radius:5px; border-bottom-left-radius:5px; border-bottom-right-radius:5px;">
<input id="main_cblEthnicity_0" type="checkbox" name="ctl00$main$cblEthnicity$0"
checked="checked" value="Native American" />
<label for="main_cblEthnicity_0">Native American</label>
</td>
...
</tr>
</tbody>
</table>
This is the HTML code of the table that has inside all those checkboxes. All it's TDs have rounded borders and stuff we already know. This table that has inside all those checkboxes is inside a bigger TD (which borders you want to keep) W're in the following situation:
So now you got 2 ways to act without changing all your HTML: CSS or jQuery.
The CSS way
Pretty simple, you may want to put inline style at those table cells (which have checkboxes inside) like this: style="border:0" instead of style="border-top-left-radius:5px; border-top-right-radius:5px; border-bottom-left-radius:5px; border-bottom-right-radius:5px;". Or Just create a new CSS class like this
.no-borders {
border:0;
}
and apply it on every td you don't want to see.
The jQuery way
<script type="text/javascript">
$(document).ready(function() {
$('.formTable input[type="checkbox"]').parent().css('border','none');
});
</script>
Your code isn't showing it, but apparently at some point class .formTable is being assigned to the CheckBoxList. Just remove border: solid 1px #bbbbbb; from the second class declaration:
.formTable tr, .formTable tr td, .formTable tr th
{
background-color: #eeeeee;
padding: 3px;
vertical-align: top;
}
Demo: http://jsfiddle.net/pgpR3/1/
I am attempting to send an email to a gmail address using C# VS2012 .NET 4.5 System.Net.Mail.MailMessage.
The email get's sent but always has:
Content-Transfer-Encoding: quoted-printable
when I view the source of the email in the mail client (gmail web mail client).
I have tried every different combination I can think of with BodyEncoding, and BodyTransferEnconding and DeliveryFormat etc... but nothing can get rid of this quoted-printable.
The problem is that the CSS is not working in the email. The HTML renders ok, but not CSS is applied, and when I view the email source, I see that it has this 3D inserted after all the = signs and this is preventing my CSS from working.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir=3D"ltr" xml:lang=3D"en" xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" content=3D"Type=3Dtext/html; charset=3Dut=f-8" />
<style type=3D"text/css">
...
The code I am using to send the email is as follows:
using (MailMessage _MailMessage = new MailMessage("[SENDER_EMAIL_REMOVED]", "[RECIPIENT_EMAIL_REMOVED]"))
{
SmtpClient _SmtpClient = new SmtpClient("smpt.gmail.com", 587);
_SmtpClient.EnableSsl = true;
_SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
_SmtpClient.UseDefaultCredentials = false;
_SmtpClient.Credentials = new System.Net.NetworkCredential("[USERNAME_REMOVED]","[PASSWORD_REMOVED]");
_MailMessage.Subject = ""Test Email";
_MailMessage.BodyEncoding = System.Text.Encoding.UTF8;
_MailMessage.BodyTransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
_SmtpClient.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
_MailMessage.Body = _MessageBody;
}
The HTML I am sending is loaded from a string that was serialized into the web.config and is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="Type=text/html; charset=utf-8" />
<title>Page Title</title>
<style type="text/css">
.style1 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; }
.style2 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight:bold; }
.tblRowHdrCol {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight:bold; background-color:#f0f0f0; width:180px; }
.tblRowValCol {font-family: Arial, Helvetica, sans-serif; font-size: 12px; padding-left:10px; width:100%; }
</style>
</head>
<body>
<p class="style1"><img src="http://www.domain.comn/image.jpg" alt="Some Image" /></p>
<p class="style1">
Some text here...
</p>
<table width="100%">
<tr>
<td class="tblRowHdrCol">Field One:</td>
<td class="tblRowValCol">[FIELD_ONE]</td>
</tr>
<tr>
<td class="tblRowHdrCol">Field Two:</td>
<td class="tblRowValCol">[FIELD_TWO]</td>
</tr>
<tr>
<td class="tblRowHdrCol">Field Three:</td>
<td class="tblRowValCol">[FIELD_THREE]</td>
</tr>
<tr>
<td class="tblRowHdrCol">Field Four:</td>
<td class="tblRowValCol">[FIELD_FOUR]</td>
</tr>
<tr>
<td class="tblRowHdrCol" colspan="2">Exception Details:</td>
</tr>
<tr>
<td class="tblRowValCol" colspan="2" style="padding-left:1px">[EXCEPTION_DETAILS]</td>
</tr>
</table>
</body>
</html>
So I load this in and do a find and replace on the [] place holders.
How can I resolve this issue?
I even tried adding a header to set it to 7BIT:
_MailMessage.Headers.Add("Content-Transfer-Encoding", "7BIT");
It added this but still had the quoted-printable above it when I viewed the source of the email in gmail.
Regards,
Scott
From what I understand, you can't get the css to work.
I think you have to convert the html so that the style are taken out of the style tag and placed inline to the html tags.
There are many tool that do this, if you want to do this dynamically in C# take a look at this: Inlining CSS in C#