asp.net jpg 转 PDF 并提供下载
时间:2010-09-27 来源:kklldog
1 /// <summary>
2 /// 在服务器上生成一个PDF COPY
3 /// </summary>
4 private void CreatePDF()
5 {
6 var bytes = GetData();
7
8 int width = bytes.ToImage().Width;
9 int height = bytes.ToImage().Height;
10 Document document = new Document(new Rectangle(width, height), 0, 0, 0, 0);
11 using (var stream = new FileStream(MapPath("~/views") + "/print.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
12 {
13 PdfWriter.GetInstance(document, stream);
14 document.Open();
15 using (Stream imageStream = new MemoryStream(bytes))
16 {
17 var image = iTextSharp.text.Image.GetInstance(imageStream);
18 document.Add(image);
19 }
20 document.Close();
21 }
22 }
23
24
25 /// <summary>
26 /// 获取缓存中的图片数据
27 /// </summary>
28 /// <returns></returns>
29 private byte[] GetData()
30 {
31 WebClient client = new WebClient();
32 string id = Request.QueryString[0];
33 string url = "http://" + Request.Url.Authority + "/GetImagePrint.ashx?id=" + id;
34 WebClient wc = new WebClient();
35
36 var bytes = wc.DownloadData(url);
37 return bytes;
38 }
39
40
41 public byte[] StreamToBytes(Stream stream)
42 {
43 byte[] bytes = new byte[stream.Length];
44 stream.Read(bytes, 0, bytes.Length);
45
46 // 设置当前流的位置为流的开始
47 stream.Seek(0, SeekOrigin.Begin);
48 return bytes;
49 }
50
51 /// <summary>
52 /// 下载到本地
53 /// </summary>
54 /// <param name="bytes"></param>
55 /// <param name="fileType"></param>
56 public void Download(byte[] bytes ,string fileType)
57 {
58 Response.Buffer = true;
59 // Page.Response.Clear();//清除缓冲区所有内容
60 Page.Response.ContentType = "application/octet-stream";
61 Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("print."+fileType));
62 byte[] file = bytes;
63 Response.BinaryWrite(file);
64 Response.Flush();
65 Response.End();
66 }
2 /// 在服务器上生成一个PDF COPY
3 /// </summary>
4 private void CreatePDF()
5 {
6 var bytes = GetData();
7
8 int width = bytes.ToImage().Width;
9 int height = bytes.ToImage().Height;
10 Document document = new Document(new Rectangle(width, height), 0, 0, 0, 0);
11 using (var stream = new FileStream(MapPath("~/views") + "/print.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
12 {
13 PdfWriter.GetInstance(document, stream);
14 document.Open();
15 using (Stream imageStream = new MemoryStream(bytes))
16 {
17 var image = iTextSharp.text.Image.GetInstance(imageStream);
18 document.Add(image);
19 }
20 document.Close();
21 }
22 }
23
24
25 /// <summary>
26 /// 获取缓存中的图片数据
27 /// </summary>
28 /// <returns></returns>
29 private byte[] GetData()
30 {
31 WebClient client = new WebClient();
32 string id = Request.QueryString[0];
33 string url = "http://" + Request.Url.Authority + "/GetImagePrint.ashx?id=" + id;
34 WebClient wc = new WebClient();
35
36 var bytes = wc.DownloadData(url);
37 return bytes;
38 }
39
40
41 public byte[] StreamToBytes(Stream stream)
42 {
43 byte[] bytes = new byte[stream.Length];
44 stream.Read(bytes, 0, bytes.Length);
45
46 // 设置当前流的位置为流的开始
47 stream.Seek(0, SeekOrigin.Begin);
48 return bytes;
49 }
50
51 /// <summary>
52 /// 下载到本地
53 /// </summary>
54 /// <param name="bytes"></param>
55 /// <param name="fileType"></param>
56 public void Download(byte[] bytes ,string fileType)
57 {
58 Response.Buffer = true;
59 // Page.Response.Clear();//清除缓冲区所有内容
60 Page.Response.ContentType = "application/octet-stream";
61 Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("print."+fileType));
62 byte[] file = bytes;
63 Response.BinaryWrite(file);
64 Response.Flush();
65 Response.End();
66 }
相关阅读 更多 +