標籤:style blog http os 檔案 io for art
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
//需要在項目裡引用ICSharpCode.SharpZipLib.dll和itextsharp.dll
public string TxtFilePath;
public string SavePdfPath;//儲存PDF的路徑
#region 讀取TXT內容
private string ReadXieyi(string FilePath)
{
string xieyi = "";
FileInfo fi = new FileInfo(FilePath);
StreamReader sr = fi.OpenText();
xieyi = sr.ReadToEnd();
sr.Close();
return xieyi;
}
#endregion
#region 建立PDF文檔方法
private void zscsc()
{
Document document = new Document(PageSize.A4);
try
{
//<appSettings>
// <add key="TxtFilePath" value="config/pubcode/"/>
//<!-- 在WEBCOFIG裡面加上的節點( value值是WebUI下的config/pubcode/)包檔案儲存在指定的檔案下 -->
//<appSettings>
string name = Online.WebUI.AccountController.LoginName + "_" + _pic_id + ".pdf";
SavePdfPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["SavePdfPath"] + name;
// object filename = "C://" + name; //filename,檔案儲存路徑
PdfWriter writer = PdfWriter.getInstance(document, new FileStream(SavePdfPath.ToString(), FileMode.Create));
document.addTitle("圖片授權協議");
document.addAuthor("Quanjing.COM");
document.addHeader("Expires", "0");
document.Open();
BaseFont bfSun = BaseFont.createFont(@"c:\windows\fonts\SIMKAI.TTF", BaseFont.IDENTITY_H, false); //調用的字型
Font font = new Font(bfSun, 15);
String text = " 圖片授權協議\n\n";
document.Add(new Paragraph(text, font));
//<appSettings>
// <add key="TxtFilePath" value="config/pubcode/xieyi.txt"/>
//<!-- 在WEBCOFIG裡面加上的節點( value值是WebUI下的config/pubcode/xieyi.txt)這樣的好處是:如果TXT的名稱有改動,直接更改webconfig就行了 -->
//<appSettings>
TxtFilePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["TxtFilePath"];
font = new Font(bfSun, 10);
text = ReadXieyi(TxtFilePath); //(@"C:\xieyi.txt");讀取TXT檔案的路徑" 圖片授權TXT檔案協議內容";
document.Add(new Paragraph(text, font));
//插入圖片
string strCatalogId;
DataTable dt = Online.Business.Production.bizDownPro.GetCatalogID(_pic_id);
strCatalogId = dt.Rows[0][0].ToString();
string FileNameUrl = "http://images.com/" + strCatalogId.ToLower() + "/thu/" + _pic_id.ToLower() + ".jpg";// "http://images.com/west004/thu/babf00254.jpg";//圖片所在路徑
iTextSharp.text.Image jpeg = iTextSharp.text.Image.getInstance(new Uri(FileNameUrl));
document.Add(new Paragraph("\n\n授權圖片:", font));
document.Add(jpeg);
//插入圖片編號
document.Add(new Paragraph("\n圖片編號:"+ _pic_id, font));
string ActivateCode;
ActivateCode = _pic_id + Online.WebUI.AccountController.LoginName; //用圖片編號加上登陸名產生MD5編碼,只取前25位
string Md5Code;
Md5Code = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(ActivateCode, "MD5").ToLower().Substring(0, 20); //加密MD5,只取前20位//"QJ-QQQQW-EEEER-TTTTY-UUUUN";
string code;
code = Md5Code.Substring(0, 5);//每5位之間用"-"分開
code = code + "-" + Md5Code.Substring(5, 5);
code = code + "-" + Md5Code.Substring(10, 5);
code = code + "-" + Md5Code.Substring(15, 5);
code = "QJ-" + code.ToString().ToUpper();
document.Add(new Paragraph("\n授權碼:"+ code, font));//code是授權碼
}
catch (DocumentException de)
{
Response.Write(de.Message);
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
document.Close();
}
#endregion
//按鈕事件
protected void Button2_Click(object sender, EventArgs e)
{
string name = Online.WebUI.AccountController.LoginName + "_" + _pic_id + ".pdf";
SavePdfPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["SavePdfPath"] + name;
if (!File.Exists(SavePdfPath.ToString()))
{
zscsc();//建立PDF文檔
ReadDownData();//下載TXT檔案
}
else
{
ReadDownData();//下載PDF檔案
}
}
//讀取TXT檔案並下載 PDF檔案
public void ReadDownData()
{
string name = Online.WebUI.AccountController.LoginName + "_" + _pic_id + ".pdf";
SavePdfPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["SavePdfPath"] + name;
//讀取檔案,並寫入到用戶端響應
FileStream fs = new FileStream(SavePdfPath.ToString(), FileMode.Open, FileAccess.Read);
byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Flush();
fs.Close();
//File.Delete(filename.ToString()); 下載之後不執行刪除
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";//ContentType;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", b.Length.ToString());
Response.OutputStream.Write(b, 0, b.Length);
Response.Flush();
Response.End();
}
轉載 http://www.idai.com.cn/blog/article.asp?id=489