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 ...
随机推荐
- QT-解除connect
前言:解除关联. 一.新建工程 二.新建部件 在ui设计界面拖入一个line edit,一个label以及两个button按钮 右键“关联”按钮转到槽,选择clicked(),添加如下代码: void ...
- BZOJ 3667 Pollard-rho &Miller-Rabin
论O(1)快速乘和O(logn)快速乘的差距-. //By SiriusRen #include <cstdio> #include <algorithm> using nam ...
- SQL Server 从字符串中提取中文、英文、数字
--[提取中文] IF OBJECT_ID('dbo.fun_getCN') IS NOT NULL DROP FUNCTION dbo.fun_getCN GO create function db ...
- input[type='file']获取上传文件路径案例
最近在项目时,需要获取用户的上传文件的路径,便写了一个demo: <body> <input type="file" name="" valu ...
- 常用几个空格的 Unicode 码
const SPACE_UNICODE = { 'ensp': '\u2002', 'emsp': '\u2003', 'nbsp': '\u00a0' }
- pixhawk入门知识
Pixhawk是一种先进的自动驾驶仪,由PX4开放硬件项目设计和3D机器人制造.它具有来自ST公司先进的处理器和传感器技术,以及NuttX实时操作系统,能够实现惊人的性能,灵活性和可靠性控制任何自主飞 ...
- JS面向对像编程四—— prototype 对象
http://blog.csdn.net/fanwenjieok/article/details/54575560 大部分面向对象的编程语言,都是以“类”(class)作为对象体系的语法基础.Java ...
- 为什么叫Unity3d为脚本语言
初接触Unity,看到大家说的都是工作主要是写脚本语言. 一直纳闷为什么说脚本语言呢,c#可不是脚本语言啊. -- -- 后来释然,说它是脚本语言是因为传统程序都是由代码构成的(像iOS.Androi ...
- Java并发--线程安全策略
1 不可变对象 用不可变对象保证线程安全,是相当于不让线程并发,逃避了并发. 不可变对象就是指一个类的实例化对象不可变.比如String类的实例 主要方法有: 将类声明为final 将所有成员声明为 ...
- IOS - CoreData 增删改查
#pragma mark - Core Data Methods - (void)insertObjectWithFileName:(NSString *)fileName { /** SQL新增记录 ...