0

A HTML file is generated by Android MobileBiz Pro invoice app. I'm trying to make software for print HTML based invoice via receipt printer

I need to save necessary information of HTML file to string variable as mentioned below. I tried using IndexOf method. but it's not working for me. How can I get this information using visual c#?

string subtotal = 2,976.00;
string total = 2,976.00;
string payment= 2,760.00;
string balance= 216.00;

This is an example of the HTML code:

<tr><td align="right" colspan="3">Subtotal</td><td align="right">2,976.00</td></tr><tr><td align="right" colspan="3"><b>TOTAL</b></td><td align="right"><b>2,976.00</b></td></tr><tr><td align="right" colspan="3">Less Payment</td><td align="right">2,760.00</td></tr><tr class="total"><td align="right" colspan="3"><strong>Balance Due</strong></td><td align="right">216.00</td></tr>

This is a complete HTML code of HTML file

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body {
    font-family:Verdana, Geneva, sans-serif;
    font-size: 8pt;
    padding: 0 50pt 0 50pt;
}
table td, table th, table.sales th, table td.footer-text {
    font-size: 8pt;
}
h1 {
    font-family:Verdana, Geneva, sans-serif;
    padding-bottom:2px;
    margin-bottom:2px;
    color:chocolate;
    text-transform:uppercase;
    font-size: inherit;
    font-size: 1.5em;
}
h2 {
    font-family:Verdana, Geneva, sans-serif;
    padding-bottom:0px;
    margin-bottom:0px;
    color:chocolate;
    text-transform:uppercase;
    font-size: 1.3em;
}
h3 {
    font-family:Verdana, Geneva, sans-serif;
    padding-bottom:2px;
    margin-bottom:2px;
}
table.sales td {
    padding: 4px 10px 4px 10px;
}
table.sales th {
    padding: 5px 10px 5px 10px;
    background-color:#CCC;
}
tr.saleline td {
    border-bottom-color:chocolate;
    border-bottom-width: 1pt;
    border-bottom-style: solid;
    vertical-align: top;
}
.signature {
    display: none;
}
.horizontal-line {
    border: 0;
    height: 4pt;
    color:chocolate;
    background-color: chocolate;
}
.total {
    font-weight:bold;
    font-size:1.1em;
    background-color:#CCC;
}
.block1 {
    text-align:left;
    vertical-align:bottom
}
.block2 {
    text-align:right;
    vertical-align:bottom
}
.block3 {
    text-align:left;
    vertical-align:top;
}
.block4 {
    text-align:left;
    vertical-align:top;
}
.block5 {
    text-align:right;
    vertical-align:bottom;
}
.block6 {
    text-align:left;
    vertical-align:top;
    margin-top: 15px;
}
.block7 {
    text-align:left;
    vertical-align:top;
    margin-top: 15px;
}
.block8 {
    text-align:left;
    vertical-align:bottom;
}
.block9 {
    text-align:center;
    vertical-align:bottom;
}
.block10 {
    text-align:right;
    vertical-align:bottom;
}
.block11 {
    text-align:left;
    padding: 25px 0 15px 0;
}
.extracols {
    border-style:solid; 
    border-color:gray;
}
table.extracols {
    border-top-width: 1pt;
    border-right-width: 0;
    border-bottom-width: 1pt;
    border-left-width: 1pt;
    border-collapse:collapse;
    margin: 0 0 15pt 0;
}
table.extracols th {
    padding: 5px 10px 5px 10px;
    border-top-width: 0;
    border-right-width: 1pt;
    border-bottom-width: 0;
    border-left-width: 0;
    border-color:gray;
    border-style:solid; 
    background-color:#CCC;
}
table.extracols td {
    padding: 4px 10px 4px 10px;
    border-top-width: 0;
    border-right-width: 1pt;
    border-bottom-width: 0;
    border-left-width: 0;
    border-color:gray;
    border-style:solid;
    background-color:#FFF;
}
#footer {
    margin-top: 35px;
}
.footer-text {
    font-size: inherit;
    font-size: 0.97em   
}
</style>
</head>
<body  style="padding: 20 20 20 20">
<table width="100%">
  <tr>
    <td style="padding-bottom:20px"><table width="100%">
        <tr>
          <td style="text-align:left;"></td>
          <td class="block2" align="right"><h3>Y.P.Brothers</h3>
           No:55/B,<br/>Samagipura,<br/>Sewanagala.
<br/>077-6977139
<br/>[email protected]
<br/>
           </td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><hr class="horizontal-line"/></td>
  </tr>
  <tr>
    <td><table width="100%"> 
        <tr>
          <td style="padding:10px 0 20px 0;"><table width="100%">
              <tr>
                <td width="33%" class="block3"><strong>Bill To</strong><br />
                ANUSHA SURIYA<br/>
                </td>
                <td class="block4"><strong></strong><br />

                </td>
                <td class="block5" align="right"><h1>invoice #1</h1>
                  <b>Date</b>: Oct 9, 2015
<br/><b>Due Date</b>: Oct 9, 2015
                 </td>
              </tr>
            </table></td>
        </tr>

        <tr>
            <td>

            </td>
          </tr>

        <tr>
          <td><table width="100%" class="sales">

              <!-- Headers -->
              <tr>
                 <th align="center">Qty</th>  <th align="center">Item</th>  <th align="right">Price</th>  <th align="right">Amount</th> 
              </tr>

              <!-- Rows -->
                <tr class="saleline">  <td align="left">12</td>   <td align="left">helaligth 35/=</td>   <td align="right">35.00</td>   <td align="right">420.00</td> </tr> 
  <tr class="saleline">  <td align="left">12</td>   <td align="left">200p CR SR 195/=</td>   <td align="right">195.00</td>   <td align="right">2,340.00</td> </tr> 
  <tr class="saleline">  <td align="left">36</td>   <td align="left">Sunlight 35g</td>   <td align="right">6.00</td>   <td align="right">216.00</td> </tr> 

              <!-- Totals -->
              <tr><td align="right" colspan="3">Subtotal</td><td align="right">2,976.00</td></tr><tr><td align="right" colspan="3"><b>TOTAL</b></td><td align="right"><b>2,976.00</b></td></tr><tr><td align="right" colspan="3">Less Payment</td><td align="right">2,760.00</td></tr><tr class="total"><td align="right" colspan="3"><strong>Balance Due</strong></td><td align="right">216.00</td></tr>

            </table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><table width="100%" style="margin-top:30px">
        <tr>
          <td width="50%" class="block6"><h2></h2>

          </td>
          <td width="50%" class="block7" align="right"><h2></h2>

          </td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><table class="block11" width="100%">
        <tr>
          <td><table></table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>

  <div class="signature">
    <table border="0" cellspacing="2" cellpadding="2" align="left">
      <tr>
        <td style="padding-bottom:30px"></td>
      </tr>
      <tr>
        <td><b>Signed by:</b> 
<br/><b>Date:</b> 
<br/><b>Signature:</b><br/></td>
      </tr>
    </table>
  </div>

<div id="footer">
<table width="100%" border="0" cellpadding="2">
  <tr>
    <td align="center"><span class="footer-text">Thank you for your business.</span></td>
  </tr>
</table>
</div>
</body>
</html>
4
  • Yes, i need to get information of this row.. Commented Oct 19, 2015 at 12:37
  • Are you getting this html in string format from third party or this is your html page in HTML format? Commented Oct 19, 2015 at 12:40
  • @ Gagan Jaura, This HTML file is generated by Android MobileBiz Pro invoice app. i'm trying to make software for print HTML based invoice via receipt printer. Commented Oct 19, 2015 at 12:45
  • This problem is still not slowed. please give any suggest for me.. Commented Oct 19, 2015 at 16:04

2 Answers 2

1

You need an html parser, try this one http://htmlagilitypack.codeplex.com/

Load page into HtmlDocument

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDocument = htmlWeb.Load("url");

Get table with specified Id

HtmlNode table = htmlDocument.DocumentNode.Descendants("table").SingleOrDefault(x => x.Id == "tableId");

Loop through nodes to find values

foreach(HtmlNode child in table.ChildNodes)
{
    if (child.NodeType != HtmlNodeType.Text)
    {
        Console.WriteLine(child.Name);
    }
}

More you can check here http://www.codeproject.com/Articles/691119/Html-Agility-Pack-Massive-information-extraction-f

Sign up to request clarification or add additional context in comments.

4 Comments

I tried it several time.. but it's not work for me. please can you provide sample code for me?
This HTML file is generated by third part software.. therefore it has not tableID..that is reason for i can't use that.. can you suggest any difference method for do that?
Are there any other tables or just that 1? Is there any attribute with the table?
This table have class with name "sales" then go foreach(HtmlNode child in table.ChildNodes ) { foreach(HtmlNode childnode in child.ChildNodes) { } } if childnode value is TOTAL then next value is your total value, little dirty but do not know other solution atm
0

You have to use parseHTML function of jquery and then loop through each element to get the values. Below is the working example (It can be more refined as per your need)

$(document).ready(function () {
        var str = '<tr><td align="right" colspan="3">Subtotal</td><td align="right">2,976.00</td></tr><tr><td align="right" colspan="3"><b>TOTAL</b></td><td align="right"><b>2,976.00</b></td></tr><tr><td align="right" colspan="3">Less Payment</td><td align="right">2,760.00</td></tr><tr class="total"><td align="right" colspan="3"><strong>Balance Due</strong></td><td align="right">216.00</td></tr>';
        var html = $.parseHTML(str);

        $.each(html, function (index, element) {
            if ($(this).find("td:first").html() == "Subtotal")
                console.log($(this).find("td:last").html());
            else if ($(this).find("td:first b").html() == "TOTAL")
                console.log($(this).find("td:last b").html());
            else if ($(this).find("td:first").html() == "Less Payment")
                console.log($(this).find("td:last").html());
            else if ($(this).find("td:first strong").html() == "Balance Due")
                console.log($(this).find("td:last").html());
        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.