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();
}
}
}

  

c# iTextSharp导出PDF的更多相关文章

  1. ITextSharp导出PDF表格和图片(C#)

    文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...

  2. 利用ITextSharp导出PDF文件

    最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...

  3. C# 利用ITextSharp导出PDF文件

    最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...

  4. iTextSharp导出PDF模板(报告)

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;us ...

  5. 使用iTextSharp导出PDF

    /// <summary> /// 导出至PDF /// </summary> /// <param name="dt">数据源</par ...

  6. C#&.Net干货分享- iTextSharp导出数据源到PDF

    namespace Frame.ITextSharp{    /// <summary>    /// iTextSharp导出数据源到PDF    /// </summary> ...

  7. 用Itextsharp 组件导出PDF 的文档的方法

    Itextsharp 是一个很强大,开源的,轻量级的 PDF 生成组件,官方网上好像没有相应的API 说明文档,以下是在工作中使用的心得与体会,并附上源码,功能包含了pdf 的创建,table 的创建 ...

  8. .Net导出pdf文件,C#实现pdf导出

    最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本, ...

  9. 根据路径获得文件名以及Aspose.Cells.dll操作excel 以及使用iTextSharp.text.pdf.PdfReader.dll对PDF的操作

    string result = Regex.Match(str,@"[^\\]+$").Value;//正则表达式 this.listBox1.Items.Add(Path.Get ...

随机推荐

  1. HTML5+Spring-MVC实现上传图片本地保存

    以下就是具体的代码: 1.在jsp页面中的代码: <span style="font-size:24px;"><form method="post&qu ...

  2. Java NIO(二)缓冲区

    概念 缓冲区:一个用于特定基本数据类型的容器,由java.nio包定义的所有缓冲区都是Buffer抽象类的子类.其作用于与NIO的通道进行交互,数据从通道读入缓冲区,数据从缓冲区写入通道 Buffer ...

  3. 多帧图片转gif

    示例 工具photosh cc2017 1: 文件--> 脚本--> 将文件载入堆栈--> 选择文件-->勾选窗口的时间轴-->底部 从图层建立帧--> 设置时间延 ...

  4. Android设计模式——单例模式

    1.单例模式就是确保一个类,只有一个实例化对象,而且自行实例化并向整个系统提供这个实例. 2.使用场景: 确保某个类,有且只有一个对象,避免产生对个对象,消耗过多的资源. 2.实现单例模式的重要点: ...

  5. jsp错误页面的处理

    局部的错误处理 1,errorpage 在错误页面指令page中声明errorpage="要显示的页面地址" 在要显示的页面page中声明iserrorpage="tru ...

  6. Pyhton二级操作题练习

    # 1.编写一个python程序,输入两个数,比较它们的大小并输出其中较大者. num1 = input('请输入数字X:') num2 = input('请输入数字Y:') if num1.isde ...

  7. HDU 1757 A Simple Math Problem( 矩阵快速幂 )

    <font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...

  8. Python 绘图与可视化 matplotlib 填充fill和fill_between

    参考链接:https://blog.csdn.net/You_are_my_dream/article/details/53457960 fill()填充函数曲线与坐标轴之间的区域: x = np.l ...

  9. static和extern的作用域--题目

    #include <stdio.h> ; int main(void) { , sum = , count = ; ,count++) // count = 2 { ; count++; ...

  10. n&(n-1)位运算的妙用

    一.n-1发生了什么 ①.二进制数n,n-1后,如果最后一位是0,将向前一位借2,2-1=1.最后一位为1.如果前一位为0,将继续向前一位借2,加上本身少掉的1.则变为1.一直遇到1.减为0. 所以 ...