|
|
|
สอบถามการใช้ iTextSharp หน่อยครับ ในกรณี htmlworker ครับ |
|
|
|
|
|
|
|
Code (C#)
//Dummy data for Invoice (Bill).
string companyName = "ASPSnippets";
int orderNo = 2303;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("ProductId", typeof(string)),
new DataColumn("Product", typeof(string)),
new DataColumn("Price", typeof(int)),
new DataColumn("Quantity", typeof(int)),
new DataColumn("Total", typeof(int))});
dt.Rows.Add(101, "Sun Glasses", 200, 5, 1000);
dt.Rows.Add(102, "Jeans", 400, 2, 800);
dt.Rows.Add(103, "Trousers", 300, 3, 900);
dt.Rows.Add(104, "Shirts", 550, 2, 1100);
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringBuilder sb = new StringBuilder();
//Generate Invoice (Bill) Header.
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr><td align='Left' style='background-color: #18B5F0'><b>ทดสอบ หนึ่ง</b></td><td align='Right' style='background-color: #18B5F0'><b>Order Sheet</b></td></tr>");
sb.Append("<tr><td colspan = '2'></td></tr>");
sb.Append("<tr><td><b>Order No: </b>");
sb.Append(orderNo);
sb.Append("</td><td align = 'right'><b>Date: </b>");
sb.Append(DateTime.Now);
sb.Append(" </td></tr>");
sb.Append("<tr><td colspan = '2'><b>Company Name: </b>");
sb.Append(companyName);
sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("<br />");
//Generate Invoice (Bill) Items Grid.
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("<tr><td align = 'right' colspan = '");
sb.Append(dt.Columns.Count - 1);
sb.Append("'>Total</td>");
sb.Append("<td>");
sb.Append(dt.Compute("sum(Total)", ""));
sb.Append("</td>");
sb.Append("</tr></table>");
//Export HTML String as PDF.
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
BaseFont bffont = BaseFont.CreateFont(@"C:\Users\User\Documents\Visual Studio 2012\Projects\MemberChart\MemberChart\Service\ANGSA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontozel = new Font(bffont, 12);
//pdfDoc.Add(new Paragraph("ถ้าใช้อันนี้ผมทำให้มันเป็นภาษาไทยได้", fontozel));
htmlparser.Parse(sr); //แต่ถ้าใช้ html มันอ่านค่าภาษาไทยไม่ได้ครับ
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + orderNo + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
ไม่ทราบว่าถ้าต้องการใช้ iTextSharp Html จะทำให้มันเป็นภาษาไทยอย่างไรครับ
Tag : .NET, Web (ASP.NET), C#
|
ประวัติการแก้ไข 2016-11-13 18:51:13
|
|
|
|
|
Date :
2016-11-13 18:50:04 |
By :
Easy123 |
View :
2223 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|