using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using iTextSharp.text;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text.pdf; namespace Bmw.Web.BLL
{
public class OutPDF
{
private static System.Drawing.Bitmap bitmap;
private static string url;
private static int w = 760, h = 900;
public static void setBitmap()
{
using (WebBrowser wb = new WebBrowser())//应该是new的问题?。。
{
wb.Width = w;
wb.Height = h;
wb.ScrollBarsEnabled = false; wb.Navigate(url);
//确保页面被解析完全
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
bitmap = new System.Drawing.Bitmap(w, h);
wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, w, h));
wb.Dispose();
}
}
public static void CreatPdf()
{
Document doc = new Document(PageSize.A4, 9, 18, 36, 36);//左右上下
MemoryStream ms = new MemoryStream();
try
{
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
writer.CloseStream = false;
doc.Open();
url = "http://localhost:5026/Orders/SeeOneOrder?Order_Id=31";
Thread thread = new Thread(new ThreadStart(setBitmap));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
while (thread.IsAlive)
Thread.Sleep(100);
bitmap.Save("t.bmp"); iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Bmp);
img.ScalePercent(75);//560 630
doc.Add(img);
}
catch (Exception err)
{
throw new Exception(err.Message);
}
finally
{
doc.Close();
//using (FileStream fs = new FileStream("out.pdf", FileMode.Create))
//{
ms.Position = 0;
byte[] bit = new byte[ms.Length];
ms.Read(bit, 0, (int)ms.Length);
//fs.Write(bit, 0, bit.Length);
//}
ViewPdf(ms);
}
}
private static void ViewPdf(Stream fs)
{ HttpContext.Current.Response.Clear();
//中文名的话
//Response.AppendHeader("Content-Disposition", "attachment;filename=" +
// HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
HttpContext.Current.Response.AddHeader("Content-Length", fs.Length.ToString());
HttpContext.Current.Response.ContentType = "application/pdf";
long fileLength = fs.Length;
int size = 10240;//10K一--分块下载,10K为1块
byte[] readData = new byte[size];
if (size > fileLength)
size = Convert.ToInt32(fileLength);
long fPos = 0;
bool isEnd = false;
while (!isEnd)
{
if ((fPos + size) >= fileLength)
{
size = Convert.ToInt32(fileLength - fPos);
isEnd = true;
}
readData = new byte[size];
fs.Position = fPos;
fs.Read(readData, 0, size);
HttpContext.Current.Response.BinaryWrite(readData);
HttpContext.Current.Response.OutputStream.Flush();
fPos += size;
}
fs.Close();
HttpContext.Current.Response.OutputStream.Close();
HttpContext.Current.Response.End();//非常重要,没有这句的话,页面的HTML代码将会保存到文件中
HttpContext.Current.Response.Close();
}
}
}

  

最新文章

  1. 截取QueryString 通过截取?和& 小写
  2. 用Win7自带的磁盘管理工具给硬盘分区
  3. URAL 2080 Wallet 莫队算法
  4. git subtree有效管理公共第三方lib
  5. ligerui grid行编辑示例
  6. Erlang 开发者的福音:IntelliJ IDEA 的 Erlang 插件
  7. Windows环境下Android NDK环境搭建
  8. Javascript语言精粹之Array常用方法分析
  9. POJ 3071-Football(可能性dp)
  10. cookie、session、sessionid的区别
  11. Google分析language垃圾信息
  12. 功能整合(一):滚动条的变相隐藏、js控制div的渐变显示、滚动条监听
  13. PDFSharp生成PDF.
  14. 消息中间件activemq-5.14.1安全验证配置
  15. Netty学习第六节实例一步学习
  16. Java对象的初始化顺序
  17. [Linux] find文件查找和grep文件内容查找
  18. 【springmvc+mybatis项目实战】杰信商贸-2.数据库配置
  19. 【ospf-路由过滤】
  20. oracle数据库的备份与还原

热门文章

  1. 关于jquery文件上传插件 uploadify 3.1的使用
  2. ubuntu下谷歌浏览器字体模糊解决方案
  3. ubuntu下安装VMware
  4. vue-cli搭建项目结构及引用bootstrap
  5. 在Windows环境下使用短信猫收发短信的简单配置:
  6. Vue学习之路第四篇:v-html指令
  7. js数组的一些骚操作 (用一行代码实现)
  8. 小学生绞尽脑汁也学不会的python(初识面对对象)
  9. C# .net IDE Rider入门
  10. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] B】Battleship