情况是这样的。我在一个datalist里面绑定了一些列图片。
绑定代码是这样的
<IFRAME style="height:300px;width:300px" id="topFrame"name="topFrame" src='Default.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>' frameBorder="no" scrolling="no">
</IFRAME>
也就是说datalist显示的图片是动态生成,现在我想把datalist里面的这些图片导出来保存在一个文件中,大家给个思路。谢谢诶。
绑定代码是这样的
<IFRAME style="height:300px;width:300px" id="topFrame"name="topFrame" src='Default.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>' frameBorder="no" scrolling="no">
</IFRAME>
也就是说datalist显示的图片是动态生成,现在我想把datalist里面的这些图片导出来保存在一个文件中,大家给个思路。谢谢诶。
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
using System.IO;/// <summary>
/// pdf 的摘要说明
/// 须要引用itextsharp.dll
/// </summary>
public class pdf
{
public pdf()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public static void createPDF(string title,string content,string fileName)
{
//生成pdf文件
DataTable table = new DataTable("pdfTable");
DataRow dr = null;
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(System.Web.HttpContext.Current.Server.MapPath("../../upFile/" + fileName + ".pdf"), FileMode.Create));
document.Open();
//设置标题
BaseFont bfSun = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font_Song_Bold = new Font(bfSun, 14, Font.BOLD);
font_Song_Bold.SetStyle("Italic");
//构建一个段落实例
Paragraph phTitle = new Paragraph();
//构建块"chunkTitle"
Chunk chunkTitle = new Chunk(title, font_Song_Bold);
//向块中追加内容
//chunkTitle.Append("Hello Word");
//将块加入到段落中
phTitle.Add(chunkTitle);
//设定段落的间距
phTitle.Leading = 20;
//段落左对其
phTitle.Alignment = Element.ALIGN_CENTER;
//将段落添加到pdf文档中显示出来
document.Add(phTitle); //内容
BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0)); Regex reg = new Regex("<.*?>", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(content);
foreach (Match mt in mc)
{
if (mt.Value.IndexOf("src") < 0)
{
if (mt.Value.ToUpper() != "<BR>" && mt.Value.ToUpper() != "<BR/>" && mt.Value.ToUpper() != "<BR />" && mt.Value.IndexOf("href") < 0 && mt.Value.ToUpper() != "</A>" && mt.Value.IndexOf("<TABLE") < 0 && mt.Value.ToUpper() != "</TABLE>" && mt.Value.ToUpper() != "<TR>" && mt.Value.ToUpper() != "</TR>" && mt.Value.IndexOf("<TD") < 0 && mt.Value.ToUpper() != "</TD>")
{
content = content.Replace(mt.Value, "");
}
}
} //Regex regTable = new Regex("<TABLE.*?</TABLE>", RegexOptions.IgnoreCase);
//MatchCollection mcTable = regTable.Matches(content);
//foreach (Match mt in mcTable)
//{
// if (mt.Value.IndexOf("src") < 0)
// {
// if (mt.Value.ToUpper() != "<BR>" && mt.Value.ToUpper() != "<BR/>" && mt.Value.ToUpper() != "<BR />" && mt.Value.IndexOf("href") < 0 && mt.Value.ToUpper() != "</A>" && mt.Value.ToUpper() != "<TABLE" && mt.Value.ToUpper() != "</TABLE>" && mt.Value.ToUpper() != "<TR>" && mt.Value.ToUpper() != "</TR>" && mt.Value.ToUpper() != "<TD" && mt.Value.ToUpper() != "</TD>")
// {
// content = content.Replace(mt.Value, "");
// }
// }
//} string[] path = content.Replace(" ", " ").Replace("<STRONG>", "<B>").Replace("</STRONG>", "</B>").Split('>'); if (path.Length > 1)
{
for (int i = 0; i < path.Length - 1; i++)
{
string strFont = path[i].Substring(0, path[i].IndexOf('<'));
document.Add(new Paragraph(strFont, fontChinese)); string strPic = path[i].Substring(path[i].IndexOf('<'));
Regex rg = new Regex("(?<=src=['\"]?)([^'\"]*)(?=['\"]?)", RegexOptions.IgnoreCase);
MatchCollection mc1 = rg.Matches(strPic);
foreach (Match mt in mc1)
{
if (mt.Value != "" && mt.Value != null)
{
if (mt.Value.Length > 0)
{
iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath(mt.Value));
document.Add(jpeg);
}
}
}
}
}
else
{
document.Add(new Paragraph(content, fontChinese));
}
document.Close();
}
}