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 ...
随机推荐
- tomcat映射路径(应用程序基本目录)的配置方法
tomcat映射路径的配置方法 一.默认配置 位置:/conf 文件夹里的server.xml文件 <Host appBase="webapps"> appBase:可 ...
- Oracle 复合索引设计原理——前缀性和可选性
前缀性: 复合索引的前缀性是指只有当复合索引的第一个字段出现在SQL语句的谓词条件中时,该索引才会被用到.如复合索引为(ename,job,mgr),只要谓词条件中出现第一个字段ename,就可以用复 ...
- HDU 1203 I NEED A OFFER!【01背包】
解题思路:攒下的钱n相当于包的容量,然后是m个学校的申请费用,申请费用相当于每一个物品的消耗,得到offer的概率相当于每一个物品的价值. 因为要求的是至少得到一份0ffer的概率的最大概率,可以转化 ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- 脚本_统计每个远程IP访问本机apache的次数
#!bin/bash#功能:统计每个远程IP访问本机apache的次数#作者:liusingbonawk '{ip[$1]++} END{for(i in ip){print ip[i],i}}' ...
- css3动画机制原理和实战
这段时间喜欢上css3动画效果了,关于这个每个人都有不同的看法,在我个人看来css3在做一些小页面的动画效果还是很好用的,一些简单的小动画要是用js的话,未免浪费. 要是做大一点的话最好js+css3 ...
- 记录——本地minikube安装ubuntu镜像总是报 Back-off restarting failed container问题 -已解决(更新)
1.环境介绍 使用本机系统:macX minikube镜像:安装的阿里云提供的镜像(否则总是提示访问google的api,不FQ无法成功) 虚拟机情况:使用Virtual box 的虚拟机环境 min ...
- CF992E Nastya and King-Shamans_线段树
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 200000 ...
- -ms-,-moz-,-webkit-,-o-含义及各浏览器内核整理
transform:rotate(30deg); //统一标识语句 -ms-transform:rotate(30deg); //-ms代表ie内核识别码 ...
- Codeforces Round #506 (Div. 3) A-C
CF比赛题解(简单题) 简单题是指自己在比赛期间做出来了 A. Many Equal Substrings 题意 给个字符串t,构造一个字符串s,使得s中t出现k次;s的长度最短 如t="c ...