c# read nodes from html table - c#

After taking a whole weekend on google and here, without success, I ask for help.
I have this html table:
<TABLE id=tabella width="100%" border=1>
<TBODY>
<TR>
<TD class=ListTitle width=55></TD>
<TD class=ListTitle width=55>Data</TD>
<TD class=ListTitle width=45>Paziente</TD>
<TD class=ListTitle width=150>Prestazioni</TD>
<TD class=ListTitle width=41>Stato</TD>
<TD class=ListTitle width=41>Sede</TD>
<TD class=ListTitle width=70>Unità</TD>
<TD class=ListTitle width=100>Specialista</TD>
<TD class=ListTitle width=41>Contatto (codice,data, n.impegnat.)</TD>
<TD class=ListTitle width=15>Stato pag.</TD>
<TD class=ListTitle width=20></TD>
<TD class=ListTitle width=20></TD>
<TD class=ListTitle width=20></TD>
</TR>
<!-- riga sotto: "title" sostituisce "name" (compatibilità browser) STP 08/05/13 -->
<TR id=riga_0 title=201503548353>
<TD class=ListElement>
<INPUT onclick=selectPatientName(this);abilitaCambiaStato() type=checkbox value="PALMA ROCCO" name=patientSelect>
</TD>
<TD class=ListElement style="FONT-SIZE: 12px">
<CENTER>Giovedi
<BR>03/12/2015
<BR>10:45</CENTER>
</TD>
<TD class=ListElement style="FONT-SIZE: 11px">
<B>xxxxx xxxx</B>
<BR>[xxxxxxxx, 25/05/1965, tel.0000000000]</TD>
<TD class=ListElement style="FONT-SIZE: 12px">RM COLONNA LOMBOSACRALE</TD>
<TD class=ListElement style="FONT-SIZE: 12px">
<CENTER>Eseguito</CENTER>
</TD>
<TD class=ListElement style="FONT-SIZE: 12px">11 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</TD>
<TD class=ListElement style="FONT-SIZE: 12px">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx </TD>
<TD class=ListElement style="FONT-SIZE: 12px">EQUIPE</TD>
<TD class=ListElement style="FONT-SIZE: 12px">
<CENTER>PB14310408
<BR>09/11/2015
<BR>16015-44444444</CENTER>
</TD>
<TD class=ListElement style="FONT-SIZE: 12px">niente</TD>
<TD class=ListElement>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick=void(0) title="Appuntamento prenotato" class=button style="CURSOR: pointer" alt="Appuntamento prenotato" src="Images/app_noconfBW.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doAppointmentChangeStatus('201503548353')" title="Cambia stato appuntamento" class=button style="CURSOR: pointer" alt="Cambia stato appuntamento" src="Images/app_status.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<IMG alt="" src="Images/empty.gif"> </TD>
<TD class=ListElement>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick=void(0) title="Appuntamento non revocabile" class=button style="CURSOR: pointer" alt="Appuntamento non revocabile" src="Images/iconDeleteBW.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick=void(0) title="Appuntamento non spostabile" class=button style="CURSOR: pointer" alt="Appuntamento non spostabile" src="Images/iconRightArrow_BW.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<IMG alt="" src="Images/empty.gif"> </TD>
<TD class=ListElement>
<!-- ....LER, 07/06/12 -->
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doAppointmentDetails('201503548353')" title="Dettagli appuntamento" class=button style="CURSOR: pointer" alt="Dettagli appuntamento" src="Images/iconDetails.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doModificaRichiesta('201503548353')" title="Modifica Richiesta" class=button style="CURSOR: pointer" alt="Modifica Richiesta" src="Images/ModificaRichiesta.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doPagaRichiesta('PB14310408')" title="Effettua pagamento" class=button style="CURSOR: pointer" alt="Effettua pagamento" src="Images/fCash2.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TD class=ListElement>
<INPUT onclick=selectPatientName(this);abilitaCambiaStato() type=checkbox value="PAIANO ADDOLORATA" name=patientSelect>
</TD>
<TD class=ListElement style="FONT-SIZE: 12px">
<CENTER>Giovedi
<BR>03/12/2015
<BR>11:00</CENTER>
</TD>
<TD class=ListElement style="FONT-SIZE: 11px">
<B>PAIANO ADDOLORATA</B>
<BR>[MAGLIE, 21/03/1956, tel.0836210592]</TD>
<TD class=ListElement style="FONT-SIZE: 12px">RM COLONNA LOMBOSACRALE</TD>
<TD class=ListElement style="FONT-SIZE: 12px">
<CENTER>Eseguito</CENTER>
</TD>
<TD class=ListElement style="FONT-SIZE: 12px">11 - Istituto Santa Chiara - Castrignano dei Greci</TD>
<TD class=ListElement style="FONT-SIZE: 12px">Istituto S. Chiara RMN - Castrignano de' Greci </TD>
<TD class=ListElement style="FONT-SIZE: 12px">EQUIPE</TD>
<TD class=ListElement style="FONT-SIZE: 12px">
<CENTER>PB14108241
<BR>28/09/2015
<BR>16015-0371711463</CENTER>
</TD>
<TD class=ListElement style="FONT-SIZE: 12px">niente</TD>
<TD class=ListElement>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick=void(0) title="Appuntamento prenotato" class=button style="CURSOR: pointer" alt="Appuntamento prenotato" src="Images/app_noconfBW.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doAppointmentChangeStatus('201503040650')" title="Cambia stato appuntamento" class=button style="CURSOR: pointer" alt="Cambia stato appuntamento" src="Images/app_status.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<IMG alt="" src="Images/empty.gif"> </TD>
<TD class=ListElement>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick=void(0) title="Appuntamento non revocabile" class=button style="CURSOR: pointer" alt="Appuntamento non revocabile" src="Images/iconDeleteBW.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick=void(0) title="Appuntamento non spostabile" class=button style="CURSOR: pointer" alt="Appuntamento non spostabile" src="Images/iconRightArrow_BW.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<IMG alt="" src="Images/empty.gif"> </TD>
<!-- LER, 07/06/12...faccio diventare la colonna di tipo List element per poter aggiungere altro pulsante -->
<TD class=ListElement>
<!-- ....LER, 07/06/12 -->
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doAppointmentDetails('201503040650')" title="Dettagli appuntamento" class=button style="CURSOR: pointer" alt="Dettagli appuntamento" src="Images/iconDetails.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doModificaRichiesta('201503040650')" title="Modifica Richiesta" class=button style="CURSOR: pointer" alt="Modifica Richiesta" src="Images/ModificaRichiesta.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD>
<IMG onclick="doPagaRichiesta('PB14108241')" title="Effettua pagamento" class=button style="CURSOR: pointer" alt="Effettua pagamento" src="Images/fCash2.gif">
</TD>
</TR>
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<!-- riga sotto: "title" sostituisce "name" (compatibilità browser) STP 08/05/13 -->
<TR>
<TD class=Button></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
And want extract the data nodes.
Most pepole use HtmlAgilityPack and i tried this:
HtmlAgilityPack.HtmlNodeCollection aNodi = doc.DocumentNode.SelectNodes("//table[#id=tabella]/tbody/tr");
Also have tested most parameters in SelectNodes, but return always NULL!
What am i doing wrong?
Thank you all for the help!

If you want to select it by id in SelectNodes you have to wrap tabella in apostrophes:
var aNodi = doc.DocumentNode.SelectNodes("//table[#id='tabella']//tbody//tr");
You could also use GetElementbyId:
var aNodi = doc.GetElementbyId("tabella").SelectNodes("//tbody//tr");

Related

Convert HTML Template to PDF using iText 7: how to move a table that prints across pages

I'm developing an asp.net mvc project and I'm using an html template to generate an invoice document with dynamic data which I filled up thanks to handlebars. The html resulting is being converted to PDF by using iText 7, since is the tool required by the company for using to, however I'm facing an issue when converting that to a PDF because of an html table that shows data, some times has enough data to fit just into one page, but in other cases, there are many rows that meet into a page break, printing the data across both pages. I need to move the whole table block to the next page whenever the data doesn't fit in one page.
Here is the template (Mytemplate.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
<!-- Removed inline styles for space reasons -->
</style>
</head>
<body>
<div style="width: 100%; margin: 0 auto; text-align: center; display: flex; justify-content: center;">
<div style="max-width: 700px; background-color: white; width: 100%">
<table style="width:100%">
<tbody>
<tr>
<td>
<table class="table-header table-information m-0 border-zero">
<tr>
<td class="text-al" style="vertical-align: top">
<img src="{{baseUrl}}\images\clear_purple.png" style="width: 150px; height: 50px;">
</td>
<td class="text-al" style="vertical-align: top">
<table class="table-information m-0 border-zero">
<tbody>
<tr>
<td class="td-information p-0">
<p class="text p-0">Insured: {{insured}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">{{facilityName}}</p>
</td>
</tr>
</tbody>
</table>
</td>
<td class="text-al vertical-ab">
<table class="table-information m-0 border-zero">
<tbody>
<tr>
<td class="td-information p-0">
<p class="text p-0">Invoice Period: {{invoicePeriod}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">Date sent to LCTI: {{invoiceDateLTCI}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">LTCI Rep: {{clientReferrer}}</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="3">
<p class="text-title">Residency Information</p>
</th>
</tr>
<tr>
<td class="td-information p-0 vertical-ab" colspan="3">
<table class="table-information border-zero mb-0"">
<tbody>
<tr>
<td class="td-information w-35">
<p class="text text-bold">Facility Type</p>
<p class="text text-bold">Room/Unit Number</p>
<p class="text text-bold">Insurance Provider</p>
<p class="text text-bold">Insured’s Policy Number</p>
<p class="text text-bold">Invoice Number</p>
</td>
<td class="td-information w-65">
<p class="text-light">{{residencyInfo.facilityType}}</p>
<p class="text-light">{{residencyInfo.roomNumberChangeTo}}{{#if residencyInfo.roomNumberChange}} - {{residencyInfo.roomNumberChangeReason}}{{/if}}</p>
<p class="text-light">{{residencyInfo.insurerProvider}}</p>
<p class="text-light">{{residencyInfo.policyNumber}}</p>
<p class="text-light">{{residencyInfo.invoiceNumber}}</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="td-information td-border-top br-1 w-45">
<table class="w-full">
<tbody>
{{#if residencyInfo.haveDaysOutOfFacility}}
<tr>
<td>
<p class="text text-bold">Total Days in Facility</p>
</td>
<td>
<p class="text-light">{{residencyInfo.totalDaysInFacility}}</p>
</td>
</tr>
<tr>
<td>
<p class="text text-bold">Total Days out of Facility</p>
</td>
<td>
<p class="text-light">{{residencyInfo.totalDaysOutOfFacility}}</p>
</td>
</tr>
{{/if}}
{{#if residencyInfo.haveBedHoldDays}}
<tr>
<td>
<p class="text text-bold">Bedhold Days</p>
</td>
<td>
<p class="text-light">{{residencyInfo.bedHoldDays}}</p>
</td>
</tr>
{{/if}}
</tbody>
</table>
</td>
<td class="td-information bl td-border-top">
<table class="w-full">
<tbody>
<tr>
<td class="td-information-width">
<p class="text-light-2">Reason for Absence</p>
</td>
<td class="td-information-width">
<p class="text-light-2 text-ar">Dates of Absence</p>
</td>
</tr>
{{#each residencyInfo.absenceReasons}}
<tr>
<td>
<p class="text-light">{{reason}}</p>
</td>
<td>
<p class="text-light text-ar">{{from}}th{{#if hasTo}} - {{to}}th{{/if}}</p>
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="3">
<p class="text-title">Care Provided</p>
</th>
</tr>
<tr>
<td class="td-information p-0" style="width: 50%;">
<table class="table-information border-zero mb-0">
<tbody>
<tr>
<th class="td-information w-58 th-activity td-border-bottom">
<p class="text text-bold">Activities of Daily Living (ADLS)</p>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Standby</p>
</div>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Hands On</p>
</div>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Provided</p>
</div>
</th>
</tr>
{{#each careProvided.adls}}
<tr>
<td class="td-information th-activity br-1 vertical-ac td-border-bottom">
<p class="text text-light">{{name}}</p>
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if standBy}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if handsOn}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if provided}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
<td class="td-information p-0" rowspan="3" style="width: 50%; padding-top: 7px;">
<table class="table-information m-0 border-zero mb-0">
<tbody>
<tr>
<th class="td-information td-border-bottom">
<p class="text text-bold pl-10">Instrumental Activities of Daily Living (IADLS)</p>
</th>
</tr>
{{#each careProvided.iadls}}
<tr>
<td class="td-information pt-0 pb-0">
<p class="text text-light pl-10 ">{{name}}</p>
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="4">
<p class="text-title">Invoice Totals</p>
</th>
</tr>
<tr>
<td style="width: 58%;">
<table class="table-information border-zero m-0">
<tbody>
{{#each invoiceTotals.charges}}
<tr>
<td class="td-information pt-0-10" style="width:60%">
<p class="text text-bold" style="font-size:15px">{{name}}</p>
</td>
<td class="td-information pt-0-10" style="width:15%">
<p class="text text-light text-ar">{{amount}}</p>
</td>
<td class="td-information p-4" style="width:25%">
{{#if includedRnB}}
<p class="text-small">Included in R&B</p>
{{/if}}
</td>
</tr>
{{/each}}
<tr>
<td class="td-information text-ar" style="padding-right: 10px" colspan="2">
<p class="text-title-35 text-green-500 text-ar">${{invoiceTotals.totalAmount}}</p>
</td>
</tr>
</tbody>
</table>
</td>
<td class="td-information pt-0" rowspan="2" style="width: 48%;">
<table class="table-information p-0 m-0 border-zero">
<tbody>
<tr>
<th class="td-information pl-0 pt-0-10"><p class="text text-bold">Other Payment sources:</p></th>
</tr>
{{#each invoiceTotals.otherPaymentSources}}
<tr>
<td class="td-information pl-0 pt-0-10"><p class="text text-light">{{name}}</p></td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" style="width: 25%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Reviewed By</p>
</th>
<th class="td-background" style="width: 35%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Signed By</p>
</th>
<th class="td-background" style="width: 25%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Signed On</p>
</th>
<th class="td-background" style="width: 15%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Ip Address</p>
</th>
</tr>
<tr>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text pl-4" style="font-size: 15px">{{signature.facility.title}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text" style="font-family: BrushScript; font-size: 24px">{{signature.facility.signedBy}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text" style="font-size: 15px">{{signature.facility.signedOn}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text text-light" style="font-size: 15px">{{signature.facility.ipAddress}}</p></td>
</tr>
<tr>
<td class="td-information pt-0-10"><p class="text pl-4" style="font-size: 15px">{{signature.client.title}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.client.signedBy}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.client.signedOn}}</p></td>
<td class="td-information pt-0-10"><p class="text text-light" style="font-size: 15px">{{signature.client.ipAddress}}</p></td>
</tr>
<tr>
<td class="td-information pt-0-10"><p class="text pl-4" style="font-size: 15px">{{signature.local.title}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.local.signedBy}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.local.signedOn}}</p></td>
<td class="td-information pt-0-10"><p class="text text-light" style="font-size: 15px">{{signature.local.ipAddress}}</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
You can see markers such as {{insured}}, etc. and commands such as {{#each residencyInfo.absenceReasons}} for looping through data, all are required by handlebars for data replacing. Here part of the code:
string templateHtml = File.ReadAllText(System.IO.Path.Combine(basePath, "templates/MyTemplate.html"));
var template = Handlebars.Compile(templateHtml);
var data = new
{
baseUrl = basePath,
insured = invoice.CustomerStatus, // Customer Name
facilityName = invoice.FacilityName,
invoicePeriod = invoice.PeriodFrom.ToString("MMM yyyy"),
invoiceDateLTCI = DateTime.Now.ToString("MM/dd/yyyy"),
clientReferrer = invoice.ClientReferrerName,
residencyInfo = new
{
facilityType = currenttFacilityType,
insurerProvider = invoice.Insurer,
policyNumber = invoice.ClaimNumber,
... // removed intencionally
absenceReasons = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityAbsenceReasons), jsonFacilityAbsenceReasonsDefinition)
},
careProvided = new
{
adls = ..., // removed intencionally
iadls = ... // removed intencionally
},
invoiceTotals = new
{
charges = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityCharges), jsonFacilityChargesDefinition),
totalAmount = totalAmount,
otherPaymentSources = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityOtherPayments), jsonFacilityOtherPaymentsDefinition)
},
signature = new
{
facility = new
{
signedBy = invoice.SignatureName,
signedOn = DateTime.ParseExact(invoice.SignatureDate, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy h:mm tt"),
title = invoice.SignatureFacilityTitleName,
ipAddress = invoice.IpAddress
},
client = new
{
... // removed intencionally
},
...
}
};
string invoiceHtml = template(data);
invoiceHtml has the final HTML with data, which is used to generate the PDF by using iText7. Here part of the code:
ConverterProperties converterProperties = new ConverterProperties();
// Create a Temp PDF file temporary, remove this when the whole invoice is created to manaqge it as stream byte[]
string file = System.IO.Path.Combine(basePath, $#"Documents\Temp_Invoice_{invoice.FacilityInvoiceId}.pdf");
PdfWriter writer = new PdfWriter(file);
PdfDocument pdf = new PdfDocument(writer);
pdf.SetDefaultPageSize(PageSize.LEGAL);
var document = HtmlConverter.ConvertToDocument(invoiceHtml, pdf, converterProperties);
document.Close();
Here is is the PDF document in which you can see the section of signers prints across a page break. In this case if data printed in the above section (charges) is too much to produce signers section prints in this way, I need to move signers block to a new page. Even, if the charges section (Invoice Totals) is too much, also to fit in first page, I need to move it to the next page. Here is the screenshot:
I tried by recovering IElements by using ConverToElements, but it gives me the whole element objects with no page these belong to.
Thanks for any help.
The CSS property page-break-inside is processed by iText 7's HTML to PDF conversion to avoid page breaks within an element. On the table that you don't want to split over two pages:
<table style="page-break-inside: avoid;">
<!-- table content -->
</table>
Of course, if the table grows to a height that is larger than a page, it's inevitable to split it.
Alternatively, when converting to Elements first with ConvertToElements, you can use SetKeepTogether(true) on the resulting Table instance.

The width of the row for HTML table is not displayed as expected when sent as an email using SMTP client

I am using a HTML code with css styling and sending it as an email body using SMTP. But in the email, the width of the rows are more than it is expected.The spacing between the rows are more.
I have also added
'IsBodyHtml = true'
'BodyEncoding = System.Text.Encoding.UTF7'
Still nothing seems to work.
please suggest.
Below is the HTML code. Also attached is the screenshot of the email https://i.stack.imgur.com/jZbpJ.png-:
Hello Team,
This needs your input.
<HTML>
<DIV>
<TABLE BORDER ="1" WIDTH = 1000 >
<TR>
<TH align ="left" bgcolor="#D3D3D3"><H3><B>Test Details</H3></TH>
</TR>
<TR>
<TD>
<Div>
<table align="left" width=100%>
<TR bgcolor="#D3D3D3">
<TD class="ColumnBorder" align="right"></TD>
<TD class="ColumnBorder" align="right">Test1</TD>
<TD class="ColumnBorder" align="right">Test2</TD>
<TD class="ColumnBorder" align="right">Price</TD>
</TR>
<TR class="RowBorder">
<TD align="left" BGCOLOR="#99B4D1" width="25%">Price1</TD>
<TD BGCOLOR="D8E6FE" align="right" width="25%">27,082.16</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">64.23</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">49,167.00</TD>
</TR>
<TR class="RowBorder">
<TD align="left" BGCOLOR="#99B4D1" width="25%">Price2</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">10.00</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">20.00</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">30.00</TD>
</TR>
</TABLE>
<TABLE align="left" style = "border-width:1px" width=100%>
<TR>
<TD align="left" BGCOLOR="#99B4D1" width="25%">Price Testing TTTTTT xxxxxxx</TD>
<TD BGCOLOR="#FFC000" align="right" width="25%">Value1</TD>
<TD BGCOLOR="#99B4D1" align="right" width="25%">Some Value2</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%"></TD>
</TR>
<TR>
<TD align="left" BGCOLOR="#99B4D1" width="25%">The final Status</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">Success</TD>
<TD BGCOLOR="#99B4D1" align="right" width="25%">The Previous Status</TD>
<TD BGCOLOR="#D8E6FE" align="right" width="25%">Failed</TD>
</TR>
<TR>
<TD BGCOLOR="#99B4D1">This is the Correct Version</TD>
<TD BGCOLOR="#D8E6FE" align="right">1</TD>
<TD BGCOLOR="#99B4D1" align="right">2</TD>
<TD BGCOLOR="#D8E6FE" align="right">3</TD>
</TR>
</TABLE>
</Div>
</TD>
</TR>
<TR>
<TD><TABLE width = "100%">
<TH colspan = "5" bgcolor ="#D3D3D3"><B>The main Price</B></TH>
<TR bgcolor ="#D3D3D3">
<TD align="left">Description</TD>
<TD align="right">Final Price(>=)</TD>
<TD align="right">Min Discount</TD>
<TD align="right">Days</TD>
<TD align="right">Max Discount</TD>
</TR>
<TR bgcolor = #00B050 style="display:#Testing1">
<TD align="left">Testing1</TD>
<TD align="right">34,267.81</TD>
<TD align="right">59.52</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
<TR bgcolor = #00B050 style="display:#Testing2">
<TD align="left">Testing2</TD>
<TD align="right">99,210.81</TD>
<TD align="right">97.52</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
<TR bgcolor = #CCFFCC style="display:#Testing3">
<TD align="left">Testing3</TD>
<TD align="right">59,190.81</TD>
<TD align="right">57.52</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
<TR bgcolor = #CCFFCC style="display:#Testing4">
<TD align="left">Testing4</TD>
<TD align="right">79,270.51</TD>
<TD align="right">59.52</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
<TR bgcolor = #FFFF99 style="display:#Testing5">
<TD align="left">Testing5</TD>
<TD align="right">98,141.30</TD>
<TD align="right">59.98</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
<TR bgcolor = #FFFF99 style="display:#Testing6">
<TD align="left">Testing6</TD>
<TD align="right">10,011.80</TD>
<TD align="right">61.23</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
<TR bgcolor = #FF0000 style="display:#Testing7">
<TD align="left">Testing7</TD>
<TD align="right">30.00</TD>
<TD align="right">190</TD>
<TD align="right">NA</TD>
<TD align="right">N/A</TD>
</TR>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</DIV>
<STYLE Type ="text/css">
table{border-collapse:collapse}
td{
font-family: Microsoft Sans Serif;
font-size:8.25pt;
}
th{
font-family: Microsoft Sans Serif;
font-size:8.25pt;
width:13em;}
tr{
height:2em}
.RowBorder{border-bottom:none;border-top:none}
.ColumnBorder{border-Right-width:1px}
</STYLE>
</HTML><br/>Disclaimer: This is for reference only.<br/>
<br/>This is just for testing purpose.<br/>

Print a div and its content with css style in ASP.net c#

Hello in my ASP Dot Net c# website i have an html report is there.Now i want to take a printout of the same.So i used Javascript and its showing only the content as a raw information.So how can i display both content and its css .
Javascript
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementsByClassName('widget-content')[0];
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
button Click
<input type="button" onclick="PrintDiv()" value="Print" />
HTML Content
<div class="widget-content">
<div class="invoice-content">
<div class="invoice-head">
<div class="invoice-meta">
<%--Invoice <span class="invoice-number">#96558 </span><span class="invoice-date">Date:
2012-07-15</span>--%>
</div>
<h5 style="margin-left: 40%; height: 20px; font-size: large">
Order Form</h5>
<div class="invoice-to">
<ul>
<li><span>Booking Date:<asp:Label ID="dispbookingDate" runat="server"></asp:Label></span>
<span>Name<asp:Label TextMode="MultiLine" runat="server" ID="dispName"></asp:Label></span>
<span>Address:<asp:Label TextMode="MultiLine" runat="server" ID="dispAddress"></asp:Label></span>
</li>
</ul>
</div>
<div class="invoice-from">
<ul>
<li><span>Order No.<asp:Label ID="dispOrderNo" runat="server"></asp:Label></span> <span>
Wedding Date:<asp:Label runat="server" ID="dispWeddingDate"></asp:Label></span>
<span>Malayalam Date:<asp:Label runat="server" ID="dispWeddingMalayam"></asp:Label></span>
</li>
</ul>
</div>
</div>
<div>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="style1">
Description
</th>
<th class="style2">
Rs.
</th>
<th>
Ps.
</th>
</tr>
</thead>
<tfoot>
<tr>
<th class="total-label" colspan="2">
Total:
</th>
<th class="total-amount">
<asp:Label ID="dispTotal" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Adavance:
</th>
<th class="total-amount">
<asp:Label ID="dispAvance" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Balance:
</th>
<th class="total-amount">
<asp:Label ID="dispBalance" runat="server"></asp:Label>
</th>
</tr>
</tfoot>
<tbody>
<tr>
<td class="style1">
Auditorium Rent
</td>
<td class="style2">
<asp:Label ID="dispRent" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style1">
Dining Hall Rent
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Kathir Mandapam
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Tables and chairs
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Electricity charge for water
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Luxuary Tax
</td>
<td class="style2">
<asp:Label ID="dispLTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Central Service Tax
</td>
<td class="style2">
<asp:Label ID="dispCTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<%-- <p class="amount-word">
Amount in Word: <span>
<asp:Label ID="dispAmountWord" runat="server"></asp:Label></span>
</p>--%>
</div>
<input type="button" onclick="PrintDiv()" value="Print" />
</div>
Have you tried:
popupWin.document.write('<html><head><link href="yourstylesheet.css" rel="stylesheet" type="text/css"></head><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
EDIT: I guess you should also close the body tag in this line
popupWin.document.write('<html><head><link href="yourstylesheet.css" rel="stylesheet" type="text/css"></head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
Your code should be like this:
function PrintDiv() {
var divToPrint = document.getElementsByClassName('widget-content')[0];
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><head><link href="yourstylesheet.css" rel="stylesheet" type="text/css"></head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
popupWin.document.close();
}
</script>
You have to use only one document.write() then it will definitely work try it .... :D

How to print a div in ASP.NET C#

Net c# website i have an html report is there.Now i want to take a printout of the same.So i used Javascript and its showing only the popup box ,not the content.how to solve this issue.
Javascript
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementById('widget-content');
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
button Click
<input type="button" onclick="PrintDiv()" value="Print" />
HTML Content
<div class="widget-content">
<div class="invoice-content">
<div class="invoice-head">
<div class="invoice-meta">
<%--Invoice <span class="invoice-number">#96558 </span><span class="invoice-date">Date:
2012-07-15</span>--%>
</div>
<h5 style="margin-left: 40%; height: 20px; font-size: large">
Order Form</h5>
<div class="invoice-to">
<ul>
<li><span>Booking Date:<asp:Label ID="dispbookingDate" runat="server"></asp:Label></span>
<span>Name<asp:Label TextMode="MultiLine" runat="server" ID="dispName"></asp:Label></span>
<span>Address:<asp:Label TextMode="MultiLine" runat="server" ID="dispAddress"></asp:Label></span>
</li>
</ul>
</div>
<div class="invoice-from">
<ul>
<li><span>Order No.<asp:Label ID="dispOrderNo" runat="server"></asp:Label></span> <span>
Wedding Date:<asp:Label runat="server" ID="dispWeddingDate"></asp:Label></span>
<span>Malayalam Date:<asp:Label runat="server" ID="dispWeddingMalayam"></asp:Label></span>
</li>
</ul>
</div>
</div>
<div>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="style1">
Description
</th>
<th class="style2">
Rs.
</th>
<th>
Ps.
</th>
</tr>
</thead>
<tfoot>
<tr>
<th class="total-label" colspan="2">
Total:
</th>
<th class="total-amount">
<asp:Label ID="dispTotal" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Adavance:
</th>
<th class="total-amount">
<asp:Label ID="dispAvance" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Balance:
</th>
<th class="total-amount">
<asp:Label ID="dispBalance" runat="server"></asp:Label>
</th>
</tr>
</tfoot>
<tbody>
<tr>
<td class="style1">
Auditorium Rent
</td>
<td class="style2">
<asp:Label ID="dispRent" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style1">
Dining Hall Rent
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Kathir Mandapam
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Tables and chairs
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Electricity charge for water
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Luxuary Tax
</td>
<td class="style2">
<asp:Label ID="dispLTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Central Service Tax
</td>
<td class="style2">
<asp:Label ID="dispCTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<%-- <p class="amount-word">
Amount in Word: <span>
<asp:Label ID="dispAmountWord" runat="server"></asp:Label></span>
</p>--%>
</div>
<input type="button" onclick="PrintDiv()" value="Print" />
</div>
In your javascript you are searching for the div with id widget-content
your code: var divToPrint = document.getElementById('widget-content');
but in your html you have <div class="widget-content"> and it has no id, it only has a class.
So you have 2 options.
OPTION 1
Change class to id
OPTION 2
Change your javascript to search for the class like so
var divToPrint = document.getElementsByClassName('widget-content')
NOTE: this will return an array of elements with that class, whether theres only one or more.
So in order to select the one you want; assuming there is only 1 div with this class you do like so:
var divToPrint = document.getElementsByClassName('widget-content')[0]

Webrowser manipulate HTML Table

I'm trying to manipulate a html table open in webbrowser control, this tool will be used ti access a sharepoint page with an autologin option. This far this is what i have:
HtmlElementCollection htmlcol =
wb.Document.GetElementsByTagName("formTextfield277");
for (int i = 0; i < htmlcol.Count; i++)
{
if (htmlcol[i].Name == "portal_id")
{
htmlcol[i].SetAttribute("VALUE",
Properties.Settings.Default.sharepoint_user);
}
else if (htmlcol[i].Name == "password")
{
htmlcol[i].SetAttribute("VALUE",
Properties.Settings.Default.sharepoint_pw);
}
}
This C# code if for manipulate this HTML page:
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%" BORDER="0">
<TR>
<TD CLASS="txtRedBold10" WIDTH="4"> </TD>
<TD CLASS="txtRedBold10" COLSPAN="2" HEIGHT="30">Please log in</TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" WIDTH="4"> </TD>
<TD CLASS="txtBlackReg10">Username:</TD>
<TD><INPUT CLASS="formTextfield277" TYPE="text" NAME="portal_id" VALUE="" VCARD_NAME="vCard.Email" SIZE="28"></TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="3"> </TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="2"> </TD>
<TD CLASS="txtBlackReg10">Please enter your username or E-Mail Address</TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="3"> </TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" WIDTH="4"> </TD>
<TD CLASS="txtBlackReg10">Password:</TD>
<TD><INPUT CLASS="formTextfield277" TYPE="password" NAME="password" SIZE="28" AUTOCOMPLETE="off"></TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="3"> </TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="2"> </TD>
<TD CLASS="txtBlackReg10">Please enter your network or Intranet password</TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="3"> </TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="2"> </TD>
<TD CLASS="txtBlackReg10">
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD><INPUT TYPE="image" HEIGHT="24" WIDTH="20" SRC="images/cp_arrow.gif" VALUE="Log In"
BORDER="0"></TD>
<TD><A CLASS="linkTxtRedBold10" HREF="javascript:signin()"
onClick="saveForm()">Login</A>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD CLASS="txtBlackReg10" COLSPAN="3"> </TD>
</TR>
</TABLE>
Any sugestions?
Thanks in advance!
wb.Document.GetElementsByTagName("input") not wb.Document.GetElementsByTagName("formTextfield277");
HtmlElementCollection inputHtmlCollection = Document.GetElementsByTagName("input");
foreach (HtmlElement anInputElement in inputHtmlCollection)
{
if (anInputElement.Name.Equals("portal_id"))
{
anInputElement.SetAttribute("VALUE", Properties.Settings.Default.sharepoint_user);
}
if (anInputElement.Name.Equals("password"))
{
anInputElement.SetAttribute("VALUE", roperties.Settings.Default.sharepoint_pw);
}
}
hope this help!

Categories

Resources