c# iTextSharp导出PDF
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的更多相关文章
- ITextSharp导出PDF表格和图片(C#)
文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...
- 利用ITextSharp导出PDF文件
最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
- C# 利用ITextSharp导出PDF文件
最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
- iTextSharp导出PDF模板(报告)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;us ...
- 使用iTextSharp导出PDF
/// <summary> /// 导出至PDF /// </summary> /// <param name="dt">数据源</par ...
- C#&.Net干货分享- iTextSharp导出数据源到PDF
namespace Frame.ITextSharp{ /// <summary> /// iTextSharp导出数据源到PDF /// </summary> ...
- 用Itextsharp 组件导出PDF 的文档的方法
Itextsharp 是一个很强大,开源的,轻量级的 PDF 生成组件,官方网上好像没有相应的API 说明文档,以下是在工作中使用的心得与体会,并附上源码,功能包含了pdf 的创建,table 的创建 ...
- .Net导出pdf文件,C#实现pdf导出
最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本, ...
- 根据路径获得文件名以及Aspose.Cells.dll操作excel 以及使用iTextSharp.text.pdf.PdfReader.dll对PDF的操作
string result = Regex.Match(str,@"[^\\]+$").Value;//正则表达式 this.listBox1.Items.Add(Path.Get ...
随机推荐
- 服务器未能识别http头soapaction的值
公司真是坑的一比 ,连接PDA报出这个错误 网上找的解决方案: 加什么wsdl http://www.cnblogs.com/dengxinglin/archive/2012/05/02/247868 ...
- BZOJ 2287 DP+容斥
思路: 先处理出来f[j]表示这i个物品都可用 填满容量j的方案数 容斥一发 处理出来g[j]=g[j-w[i]] 表示i不能用的时候 填满容量j的方案数 //By SiriusRen #includ ...
- FLAG_DISMISS_KEYGUARD & FLAG_SHOW_WHEN_LOCKED
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD): 这一个标志的意思是去掉锁屏界面,但这对安全锁(图案或者密 ...
- HD-ACM算法专攻系列(23)——Crixalis's Equipment
题目描述: AC源码:此次考察贪心算法,解题思路:贪心的原则是使留下的空间最大,优先选择Bi与Ai差值最大的,至于为什么?这里用只有2个设备为例,(A1,B1)与(A2,B2),假设先搬运A1,搬运的 ...
- EF Code First 使用 代码优先迁移(二)
第一步:如果不是建立的MVC项目,可能需要在控制台输入 :Install-Package EntityFramework 删除之后在执行Enable-Migrations 第二步:添加你需要修改的属性 ...
- art-template模板渲染及其过滤器
原生语法 使用原生语法,需要导入template-native.js文件.在HTML中定义模板,注意模板的位置,不要放到被渲染区域,防止模板丢失. <script id="tpl&qu ...
- wampserver的安装及使用配置方法
在安装wampserver的过程中,根据本人在安装过程中遇到的问题,在此做个总结,与小伙伴们分享一下下~~~. 1. 何处获得Wamp ★ 在自己的Windows电脑上Web服务器软件 - Wamp. ...
- 利用 js 获取地址栏参数
1.aspx页面,进行页面跳转 window.location.href = "ProviderInfo.aspx?ProviderID="+ProviderID; 2.Provi ...
- StatusBarUtils工具类
import android.app.Activity; import android.app.Dialog; import android.content.Context; import andro ...
- zookeeper+kafka集群搭建
一.ZK集群安装. 解压安装包后进入conf目录,conf/zoo_sample.cfg拷贝一份命名为zoo.cfg,同时也放在conf下面. zookeeper配置文件: # The number ...