HTML to PDF convert using Aspose.Pdf-iTextSharp
- Get link
- X
- Other Apps
| private string ExportGridToPDF(string id)
{
try
{
string Err = string.Empty;
DataTable dt = (DataTable)Session["Data"];
var imageUrl = "";
StringBuilder sb = new StringBuilder();
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
var seblLogo = baseUrl + "/img/sebl.png";
sb.Append("");
if (dt.Rows.Count > 0)
{
sb = new StringBuilder();
sb.Append("<header class='clearfix' style='padding:50px'>");
sb.Append("<div id='bodyproject' >");
sb.Append("<table width='700px' >");
sb.Append("<tr>");
sb.Append("<td style='vertical-align: top; ' width='130px' height='100px'> <span style='padding-top: -25px;font-weight: normal;font-size: 11px;'></span>");
sb.Append("<div style='opacity: 0.5;'><img style='opacity: 0.5' src='" + seblLogo + "' width='40' height='40' alt='logo'/> </div>");
sb.Append("</td>");
sb.Append("<td width='500px' style='vertical-align: top;text-align: center' height='100px'><span style='font-weight: normal;font-size: 11px;'></span>");
sb.Append("<div style='font-size: 14px;font-weight: bold;'>COMPANY NAME</div>");
sb.Append("<div style='font-size: 14px;font-weight: bold;'>DIVISION</div>");
sb.Append("<div style='font-size: 12px; font-weight: bold;'> HEAD OFFICE, DHAKA</div>");
sb.Append("<div style='font-size: 12px;font-weight: bold;' >APPLICATION FORM</div>");
sb.Append("<div style='font-size: 12px;font-weight: normal;'>Instruction Id:" + dt.Rows[0]["batch_id"].ToString() + "</div>");
sb.Append("<div style='font-size: 12px;'>Created Date:" + dt.Rows[0]["create_date"].ToString() + "</div>");
sb.Append("</td>");
sb.Append("<td width='50px' height='100px'> <span style='font-weight: normal;font-size: 11px;'></span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table >");
sb.Append("<div id='company' >");
sb.Append("</div>");
sb.Append("</div>");
sb.Append("</header>");
sb.Append("<main>");
sb.Append("<div id='bodyprojectc'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Applicant's(Remitter) Information</span> </div>");
sb.Append("<table >");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'> <span style='font-weight: normal;font-size: 11px;'>Name of Applicant :</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["applicant_name"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px'>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Your Reference No :</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["reference_no"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Debit A/C No:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>("+ dt.Rows[0]["debit_account_currency"].ToString() + ") " + dt.Rows[0]["debit_accountno"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' >");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Charge Debit A/C No:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>(" + dt.Rows[0]["charge_debit_accountno_currency"].ToString() + ") " + dt.Rows[0]["charge_debit_accountno"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("</div>");
sb.Append("<div id='bodyproject1'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Remittance Amount</span> </div>");
sb.Append("<table >");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'> <span style='font-weight: normal;font-size: 11px;'>Currency of Remittance:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["currency_of_remittance"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px'>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Amount to be Remmitted:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>(" + dt.Rows[0]["amount_to_be_remitted_currency"].ToString() + ") " + dt.Rows[0]["amount_to_be_remitted_amount"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Charge Type:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["charge_type"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px' >");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Value Date:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["value_date"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("</div>");
sb.Append("<div id='bodyproject2'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Recipient's Information</span> </div>");
sb.Append("<table >");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Account No/BAN:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["beneficiary_accountno"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px'>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Bank Name:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["recipient_bank_name"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Country:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["recipient_bank_country"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px'>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>SWIFT/Clearing Code:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["recipient_bank_swift_code"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("<table > ");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Address:</span>");
sb.Append("</td>");
sb.Append("<td width='350px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["recipient_bank_address"].ToString() + "</span>");
sb.Append("</td>");
//sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'></span>");
//sb.Append("</td>");
//sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'></span>");
//sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("<table > ");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Account Name & Address:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["recipient_account_name_address"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px'><span style='font-weight: normal;font-size: 11px;'></span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Recipient Country:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>"+ dt.Rows[0]["recipient_country"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("</div>");
sb.Append("<div id='bodyproject3'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Intermediary Bank Information(Optional)</span> </div>");
sb.Append("<table >");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Bank Name:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["intermediary_bank_name"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' >");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Country:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["intermediary_bank_country"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Swift/Clearing Code:</span>");
sb.Append("</td>");
sb.Append("<td width='160px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["intermediary_swift_code"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='80px' height='30px' >");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>Address:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'><span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["intermediary_address"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("</div>");
DataTable dtFiles = (DataTable)Session["DataFile"];
sb.Append("<div id='bodyproject4'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Payment Details</span> </div>");
sb.Append("<div height='30px'><span style='font-weight: normal;font-size: 12px;'>" + dt.Rows[0]["payment_details"].ToString() + "</span> </div>");
sb.Append("</div>");
sb.Append("<div id='bodyproject4'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>File List</span> </div>");
var baseUlr = new clsCommon().CLIENT_BASE_URL;
var file_path = "";
for (int i = 0; i < dtFiles.Rows.Count; i++)
{
file_path = baseUlr + dtFiles.Rows[i]["file_path"].ToString();
//sb.Append("<div height='30px'><span style='font-weight: normal;font-size: 12px;'>" + (i + 1) + ". " + dtFiles.Rows[0]["filetitle"].ToString() + "</span> </div>");
sb.Append("<div height='30px'><a style='font-weight: normal;font-size: 12px;color:black' target='_blank' download='" + dtFiles.Rows[i]["filetitle"].ToString() + "' href='" + file_path + "'>"+ (i + 1) +"."+ dtFiles.Rows[i]["filetitle"].ToString() + " </a></div>");
}
sb.Append("</div>");
sb.Append("<div id='bodyproject5'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Purpose of Remittance</span> </div>");
sb.Append("<table >");
sb.Append("<tr>");
sb.Append("<td width='150px' height='30px'> <span style='font-weight: normal;font-size: 11px;'>" + dt.Rows[0]["purpose_of_remittance"].ToString() + "</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'> <span style='font-weight: normal;font-size: 11px;'>:</span>");
sb.Append("</td>");
sb.Append("<td width='150px' height='30px'> <span style='font-weight: normal;font-size: 11px;'></span>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("</div>");
DataTable dtSignature = (DataTable)Session["DataSignature"];
sb.Append("<div id='bodyproject6'>");
sb.Append("<div><span style='font-weight: bold;font-size: 12px;'>Applicant Signature</span> </div>");
for (int i = 0; i < dtSignature.Rows.Count; i++)
{
Guid id_url = Guid.NewGuid();
var path = Server.MapPath("~/TempImage/" + id_url + ".jpg");
baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
Base64ToImage(dtSignature.Rows[0]["signature_image_url"].ToString()).Save(path);
imageUrl = baseUrl + "/TempImage/" + id_url + ".jpg";
sb.Append("<img src='" + imageUrl + "' width='100' height='100' alt='logo'/>");
imageUrl = path;
}
sb.Append("</div>");
sb.Append("</main>");
sb.Append("<footer>");
sb.Append(".");
sb.Append("</footer>");
}
String htmlText = sb.ToString();
string fname = Guid.NewGuid() + ".pdf";
using (var stream = new FileStream(Server.MapPath(Path.Combine("~/TempImage/", fname)), FileMode.Create))
{
using (var document = new Document(PageSize.A4))
{
var writer = PdfWriter.GetInstance(document, stream);
document.Open();
// instantiate custom tag processor and add to `HtmlPipelineContext`.
var tagProcessorFactory = iTextSharp.tool.xml.html.Tags.GetHtmlTagProcessorFactory();
tagProcessorFactory.AddProcessor(
new TableDataProcessor(),
new string[] { HTML.Tag.TD }
);
var htmlPipelineContext = new HtmlPipelineContext(null);
htmlPipelineContext.SetTagFactory(tagProcessorFactory);
var pdfWriterPipeline = new PdfWriterPipeline(document, writer);
var htmlPipeline = new HtmlPipeline(htmlPipelineContext, pdfWriterPipeline);
// get an ICssResolver and add the custom CSS
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
string CSS = @"
body {font-size: 12px;}
.header{border: none;}
table { page-break-inside:auto }
div { page-break-inside:avoid; } /* This is the key */
thead { display:table-header-group }
tfoot { display:table-footer-group }
";
cssResolver.AddCss(CSS, "utf-8", true);
var cssResolverPipeline = new CssResolverPipeline(
cssResolver, htmlPipeline
);
var worker = new XMLWorker(cssResolverPipeline, true);
var parser = new XMLParser(worker);
using (var stringReader = new StringReader(htmlText))
{
parser.Parse(stringReader);
}
}
}
return "/TempImage/"+fname;
}
catch (Exception ex)
{
return "";
//throw ex;
//lblmessage.Text = ex.ToString();
}
}
private class TableDataProcessor : iTextSharp.tool.xml.html.table.TableData
{
/*
* a **very** simple implementation of the CSS writing-mode property:
* https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode
*/
bool HasWritingMode(IDictionary<string, string> attributeMap)
{
bool hasStyle = attributeMap.ContainsKey("style");
return hasStyle
&& attributeMap["style"].Split(new char[] { ';' })
.Where(x => x.StartsWith("writing-mode:"))
.Count() > 0
? true : false;
}
public override IList<IElement> End(
IWorkerContext ctx,
Tag tag,
IList<IElement> currentContent)
{
var cells = base.End(ctx, tag, currentContent);
var attributeMap = tag.Attributes;
if (HasWritingMode(attributeMap))
{
var pdfPCell = (PdfPCell)cells[0];
// **always** 'sideways-lr'
pdfPCell.Rotation = 90;
}
return cells;
}
}
Have to use this lib:
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;
|
- Get link
- X
- Other Apps
Comments
Post a Comment