public void PDFYapsana(string dadi, string html)
{
try
{
Byte[] bytes;
MemoryStream ms = new MemoryStream();
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
var htmlText = "";
try
{
//Our sample HTML and CSS
htmlText = html;//@"<div style='height:50px'> SELAMLAR <br/></div>";
var example_css = @"
.zui-table {
border: solid 1px #DDEEEE;
border-collapse: collapse;
border-spacing: 0;
font: normal 13px Arial, sans-serif;
}
.zui-table thead th {
background-color: #DDEFEF;
border: solid 1px #DDEEEE;
color: #336B6B;
padding: 10px;
text-align: left;
text-shadow: 1px 1px 1px #fff;
}
.zui-table tbody td {
border: solid 1px #DDEEEE;
color: #333;
padding: 10px;
text-shadow: 1px 1px 1px #fff;
}";
MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css));
MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText));
doc.Open();
iTextSharp.tool.xml.XMLWorkerHelper aa = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
writer.Open();
doc.Open();
aa.ParseXHtml(writer, doc, msHtml, msCss);
doc.Close();
bytes = ms.ToArray();
var testFile = HttpContext.Current.Server.MapPath("Ups/" + dadi + ".pdf");
// Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
}
catch (Exception exi)
{
}
}
catch (Exception ex)
{
}
}
/* Kullanımı
string h = @"
<html><body>
<table class='zui-table'>
<thead><tr><th></th><th>QR</th><th>USER</th><th>PROJECT</th></tr></thead>
<tbody>
<tr><td>1</td><td><img src=""https://www.com/QRS/110.jpg"" height='80px'/></td><td>cb1</td><td>ZONE1</td></tr>
</tbody></table></body></html>";
PDFYapsana("b", h);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
//Response.TransmitFile("https://www.com/UPS/b.pdf");
Response.TransmitFile(Server.MapPath(".")+ "/UPS/b.pdf");
Response.End();
*/
---------------------------------------
//Export Datatable to PDF
public void DT2PDF(DataTable dt)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//rpt_kayitlar.RenderControl(hw);
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
//hw. = "";
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
----------------------------------------
//Export Repeater to Excel
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=RepeaterExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
foreach (RepeaterItem item in rpt_kayitlar.Items)
{
List<System.Web.UI.Control> controls = new List<System.Web.UI.Control>();
foreach (System.Web.UI.Control control in item.Controls)
{
controls.Add(control);
}
int i = 0;
foreach (System.Web.UI.Control control in controls)
{
switch (control.GetType().Name)
{
case "CheckBox":
//item.Controls.AddAt(i + 1, new Literal { Text = (control as TextBox).Text });
item.Controls.RemoveAt(i);
break;
case "Button":
//item.Controls.AddAt(i + 1, new Literal { Text = (control as TextBox).Text });
item.Controls.RemoveAt(i);
break;
case "LinkButton":
//item.Controls.AddAt(i + 1, new Literal { Text = (control as TextBox).Text });
item.Controls.RemoveAt(i);
break;
}
i++;
}
}
rpt_kayitlar.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
----------------------------------------
protected void PrintExcel(object sender, EventArgs e)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=file.xls");
Response.ContentType = "application/vnd.ms-excel";
string filePath = Server.MapPath("~/PDF/Capture.PNG.png");
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(filePath);
string imagepath = string.Format("<img src='{0}' width='{1}' height='{2}'/>", filePath, imgPhoto.Width, imgPhoto.Height);
var sb = new StringBuilder();
RepeaterItem item1;
sb.Append("<table border='1'>");
sb.Append("<tr>");
sb.Append("<th>");
sb.Append("Kasa Numarası");
sb.Append("<th>");
sb.Append("Cari No");
sb.Append("<th>");
sb.Append("Cari Hesap");
sb.Append("<th>");
sb.Append("Tarih");
sb.Append("<th>");
sb.Append("Açıklama");
sb.Append("<th>");
sb.Append("ProjeKodu");
sb.Append("<th>");
sb.Append("Tutar");
sb.Append("</th></th></th></th></th></th></th></tr>");
foreach (RepeaterItem item in rpt_kayitlar.Items)
{
sb.Append("<tr>");
sb.Append("<td>");
sb.Append(imagepath);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label2")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label3")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label4")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label5")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label6")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label7")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label8")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label9")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label10")).Text);
sb.Append("<td>");
sb.Append(((Label)item.FindControl("Label11")).Text);
sb.Append("</td></td></td></td></td></td></td></td></td></td></td></tr>");
imgPhoto.Dispose();
}
sb.Append("</table>");
Response.ContentType = "application/vnd.xls";
Response.Write(sb.ToString());
imgPhoto.Dispose();
Response.Flush();
Response.End();
}
------------------------
print html table to print
<script type='text/javascript'>
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--> <meta charset="utf-8"></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
------------------------------
JS ile print only div
function printData() {
var divToPrint = document.getElementById("gridi");
newWin = window.open("", "newWin");
newWin.document.write(divToPrint.outerHTML);
setTimeout(function () {
newWin.print();
}, 1000)
// newWin.close();
}