如何将Datalist(包括图像)导出到Pdf [英] how to Export Datalist (include Images) To Pdf

查看:105
本文介绍了如何将Datalist(包括图像)导出到Pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有..



当我尝试将Datalist或gridview导出为pdf ..ie导出时没有图像!!!



这是我的代码:



 Response.ContentType =application / pdf; 
Response.AddHeader(content-disposition,
attachment; filename = Products.pdf);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

DataList1.DataBind();

DataList1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc =新文件(PageSize.A4,10f,10f,10f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

解决方案

您可能需要确保您的图像是由绝对而非相对引用的你的HTML中的路径。这是因为大多数pdf编写者在解析之前在临时目录中创建了html输入的临时副本,然后导致相对路径失败。


试试这个: -



 iTextSharp.text.Table table = new iTextSharp.text.Table(grdCust_accot_stmt.Columns.Count); 
table.Cellpadding = 2;
table.Width = 100;
grdCust_accot_stmt.AllowPaging = false;
this.grd_Account_stmt();
//将行从GridView传输到表
for(int i = 0; i< grdCust_accot_stmt.Columns.Count; i ++)
{
string cellText = Server.HtmlDecode( grdCust_accot_stmt.Columns [I] .HeaderText);
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml(#e8eff3));
table.AddCell(cell);
}

for(int i = 0; i< grdCust_accot_stmt.Rows.Count; i ++)
{
if(grdCust_accot_stmt.Rows [i] .RowType == DataControlRowType.DataRow)
{
for(int j = 0; j< grdCust_accot_stmt.Columns.Count; j ++)
{
string cellText = Server.HtmlDecode
(grdCust_accot_stmt.Rows [i] .Cells [j] .Text);
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);

//设置交替行的颜色
if(i%2!= 0)
{
cell.BackgroundColor = new iTextSharp.text.Color(System。 Drawing.ColorTranslator.FromHtml( #e8eff3));
}
table.AddCell(cell);
}
}
}
文件pdfDoc =新文件(PageSize.A2,10f,10f,10f,10f);
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType =application / pdf;
Response.AddHeader(content-disposition,attachment;+filename = Reports.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();


这篇文章解释了如何:iTextBox - 以PDF格式转换datalist和图像 [ ^ ]

Hey All ..

when i try to export Datalist or gridview to pdf..ie exported without images !!!

this is my code :

Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
         "attachment;filename=Products.pdf");
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        DataList1.DataBind();
        
        DataList1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();

解决方案

You may need to make sure that your images are referenced by absolute and not relative paths in your html. This is due to the fact that most pdf writers create a temporary copy of your html input in a temp directory before parsing which then causes the relative paths to fail.


Try This:-

iTextSharp.text.Table table = new iTextSharp.text.Table(grdCust_accot_stmt.Columns.Count);
        table.Cellpadding = 2;
        table.Width = 100;
        grdCust_accot_stmt.AllowPaging = false;
        this.grd_Account_stmt();       
        //Transfer rows from GridView to table
        for (int i = 0; i < grdCust_accot_stmt.Columns.Count; i++)
        {
            string cellText = Server.HtmlDecode(grdCust_accot_stmt.Columns[i].HeaderText); 
            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
            cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#e8eff3"));
            table.AddCell(cell);
        }
 
        for (int i = 0; i < grdCust_accot_stmt.Rows.Count; i++)
        {
            if (grdCust_accot_stmt.Rows[i].RowType == DataControlRowType.DataRow)
            {
                for (int j = 0; j < grdCust_accot_stmt.Columns.Count; j++)
                {
                    string cellText = Server.HtmlDecode
                                      (grdCust_accot_stmt.Rows[i].Cells[j].Text);
                    iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
 
                    //Set Color of Alternating row
                    if (i % 2 != 0)
                    {
                        cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#e8eff3"));
                    }
                    table.AddCell(cell);
                }
            }
        }
        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 10f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +"filename=Reports.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();


Here is an article that explains how: iTextBox - Convert datalist and image in PDF[^]


这篇关于如何将Datalist(包括图像)导出到Pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆