C# PDF打印
C#中使用iTextSharp生成并下载PDF很方便。
首先要将iTextSharp的dll下载并引入项目
主要分成两部分,一部分是PDF的Document生成,另一部分就是将Document输出到页面
这里分别列出aspx页和MVC中ActionResult的下载方式
①aspx
工具类(只是提供Document的输出)
using System.Web;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf; namespace Common
{
public class PdfHelper
{
public event DocRenderHandler DocRenderEvent;
public delegate void DocRenderHandler(ref Document Doc); public void DownLoadPDF(string FileName, HttpContext context)
{
Document Doc = new Document();
HttpResponse Response = context.Response; using (MemoryStream Memory = new MemoryStream())
{
PdfWriter PdfWriter = PdfWriter.GetInstance(Doc, Memory);
if (DocRenderEvent != null)
DocRenderEvent(ref Doc); #region 文件输出及相关设置
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
Response.ContentType = "application/pdf";
Response.OutputStream.Write(Memory.GetBuffer(), , Memory.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
#endregion
}
Response.End();
}
}
}
下载页面
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text; namespace MvcMovie.Common
{
public partial class PdfLoad : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
string FileName = DateTime.Now.ToShortTimeString() + ".pdf";
PdfHelper ph = new PdfHelper();
ph.DocRenderEvent += RenderPdfDoc;
ph.DownLoadPDF(FileName, this.Context);
} private void RenderPdfDoc(ref Document Doc)
{
Doc.SetPageSize(PageSize.A4);
Doc.SetMargins(, , , ); #region 相关元素准备
BaseFont bfChinese = BaseFont.CreateFont(@"C:\windows\fonts\simsun.ttc,1", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
Font Font16 = new Font(bfChinese, );
Font Font14 = new Font(bfChinese, );
Font Font12 = new Font(bfChinese, );
Font Font12Bold = new Font(bfChinese, , Font.BOLD);
Font Font12Italic = new Font(bfChinese, , Font.BOLDITALIC);
Font Font10Bold = new Font(bfChinese, , Font.BOLD); Paragraph parag;
Chunk chunk;
PdfPTable table;
#endregion #region 文件标题
Doc.Open();
Doc.AddAuthor("TiestoRay");
Doc.AddTitle("相关部分发布的重要通知");
#endregion #region 正文
parag = new Paragraph("------通知------", Font16);
parag.Alignment = Element.ALIGN_CENTER;
Doc.Add(parag); parag = new Paragraph();
parag.SetLeading(20f, 1f);
parag.Add(new Chunk("曾经沧海难为水,心有灵犀一点通", Font12Italic));
Doc.Add(parag); parag = new Paragraph();
parag.Add(new Chunk("取次花丛懒回顾,得来全不费工夫", Font10Bold));
Doc.Add(parag); parag = new Paragraph();
parag.Add(new Chunk(" " + DateTime.Now.ToLongDateString(), Font12));
Doc.Add(parag); parag = new Paragraph();
parag.SetLeading(1f, 1f);
chunk = new Chunk(new StringBuilder().Insert(, " ", ).Append("Come On!").ToString(), Font12);
chunk.SetUnderline(-18f, 1.4f);
parag.Add(chunk);
parag.Alignment = Element.ALIGN_JUSTIFIED_ALL;
Doc.Add(parag); Doc.NewPage();//换页 table = new PdfPTable(new float[] { , , });
table.WidthPercentage = 100f;
table.AddCell(new Phrase("英语老师签字:\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n", Font12));
table.AddCell(new Phrase("同桌签字:", Font12));
table.AddCell(new Phrase("本人签字:", Font12));
Doc.Add(table); parag = new Paragraph();
parag.SetLeading(-30f, 1.4f);
parag.Add(new Chunk(new StringBuilder().Insert(, " ", ).ToString() + "签字日期:", Font12));
Doc.Add(parag);
Doc.Close();
#endregion
}
}
}
②MVC3(以上)版本
写一个继承自ActionResult的Result类,并且将Document输出写到ExecuteResult中
using System.Web;
using System.Web.Mvc;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf; namespace Common
{
public class PdfResult:ActionResult
{
private string FileName;
public event DocRenderHandler DocRenderEvent;
public delegate void DocRenderHandler(ref Document Doc); public PdfResult(string FileName)
{
this.FileName = FileName;
} /// <summary>
/// 向页面输出时才会执行该方法
/// </summary>
public override void ExecuteResult(ControllerContext context)
{
Document Doc = new Document();
using (MemoryStream Memory = new MemoryStream())
{
PdfWriter PdfWriter = PdfWriter.GetInstance(Doc, Memory);
if (DocRenderEvent != null)
DocRenderEvent(ref Doc); #region 文件输出及相关设置
HttpResponseBase Response = context.HttpContext.Response;
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
Response.ContentType = "application/pdf";
Response.OutputStream.Write(Memory.GetBuffer(), , Memory.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
#endregion
}
context.HttpContext.Response.End();
}
}
}
②调用
using System;
using System.Web.Mvc;
using MvcMovie.Common;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text; namespace MvcMovie.Controllers
{
public class HomeController : Controller
{
public ActionResult Index(string name,int? id)
{
ViewBag.Message = "欢迎使用 ASP.NET MVC!";
return View();
} public ViewResult About()
{
return View("About");
} public ActionResult DownLoadPdf(int ID)
{
string FileName = DateTime.Now.ToShortTimeString()+".pdf";
PdfResult pr = new PdfResult(FileName);
pr.DocRenderEvent += RenderPdfDoc;
return pr;
} private void RenderPdfDoc(ref Document Doc)
{
//TO DO
//内容同上
}
}
}
C# PDF打印的更多相关文章
- 开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)
在这个.NET组件的介绍系列中,受到了很多园友的支持,一些园友(如:数据之巅. [秦时明月]等等这些大神 )也给我提出了对应的建议,我正在努力去改正,有不足之处还望大家多多包涵.在传播一些简单的知识的 ...
- 基于iTextSharp的PDF操作(PDF打印,PDF下载)
基于iTextSharp的PDF操作(PDF打印,PDF下载) 准备 1. iTextSharp的简介 iTextSharp是一个移植于java平台的iText项目,被封装成c#的组件来用于C#生成P ...
- NetSuite实现pdf打印中的条形码的功能
2020-11-27 提起NS,在程序员这一块应该不怎么被人知道,算是比较小众的一门技术了,毕竟Netsuite兴起的时间算不上早,进入中国的时间更晚,除了从事这一块的程序员,可能都没有见过,恰好我是 ...
- .Net下的PDF打印
简单研究了一下.Net下的PDF打印,一路发现了很多小坑. 第三方组件 这里使用的解析PDF的组件是mupdf,特点和C#调用在 这里 有介绍. 实现的功能 支持页面大小.边距.打印机选择.打印机dp ...
- 驰骋CCFlow开源工作流程引擎如何设置PDF打印
前言 经常有驰骋CCFlow爱好者朋友提问关于打印相关问题.在这篇博文中大家介绍一下工作流引擎CCFlow的HTML打印和PDF打印,针对Java版本和.NET版本有不同的操作步骤,包括开关设置.水印 ...
- Java 创建PDF打印小册子
概述 PDF打印小册子是指将PDF格式文档在打印成刊物前需要提前进行的页面排版,以便在打印后装订成册.下面以Java代码展示如何来实现.这里调用Free Spire.PDF for Java中的Pdf ...
- 重命名PDF打印文件名
Odoo系统默认打印出来的PDF文件都是以当前文档模型对象对应的模板文件名命名的,对用户来说,这样的命名很不友好. 我们希望能够将打印出来的文件名以单号命名,下面是实现这种目的的方法. 在report ...
- 两页pdf打印为一页,并且放大(打印英文pdf常用)
多很英文书籍都是小书,若我们直接打印它的pdf会很厚,比如我要打印一本 thinking in C++,就要800+页.不如把两页打成一页.但是打成一页之后又太小了,需要放大.具体方法如下: 前提 ...
- pdf打印乱码问题
问题: 使用Adobe Reader将一份pdf文件通过我的虚拟打印机输出后(输出的是中间文件,等同于EMF文件),查看的时候发现有时候是乱码.最简单的必现步骤: 1.使用Adobe Reader打开 ...
- java原装代码完成pdf在线预览和pdf打印及下载
这是我在工作中,遇到这样需求,完成需求后,总结的成果,就当做是工作笔记,以免日后忘记,当然,能帮助到别人是最好的啦! 下面进入正题: 前提准备: 1. 项目中至少需要引入的jar包,注意版本: a) ...
随机推荐
- C#语法糖之 cache操作类 asp.net
因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性. 所以都统一继承了IHttpStorageObject abstract class 来保函数风格的统一 , ...
- IOS开发UI基础 UIAlertView的属性
UIAlertView1.Title获取或设置UIAlertView上的标题. 2.Message获取或设置UIAlertView上的消息 UIAlertView *alertView = [[UIA ...
- Gradle学习系列之九——自定义Task类型
在本系列的上篇文章中,我们学习了多Project构建,在本篇文章中,我们将学到如何自定义Task类型. 请通过以下方式下载本系列文章的Github示例代码: git clone https://git ...
- github生成燃尽图
一. 前期准备工作. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8添加标签 二. 修改进度 2.1进入milestone,编辑 可以看到上面有bug标签,进入并解决 ...
- 重构第23天 引用参数对象(Introduce Parameter Object)
理解:有时候我们的一个方法,需要很多个参数,太多参数,不易阅读和理解,我们就可以把多个参数封装成一个对象. 详解: 重构前代码: public class Registration { public ...
- Winform开发框架的业务对象统一调用方式
在这个纷繁的社会里面,统一性的特点能够带来很多高效的产出.牢固的记忆,这种特征无论对于企业.个人的开发工作,知识的传承都有着非常重要的作用,Winfrom框架本身就是基于这个理念而生,从统一的数据库设 ...
- Mongodb:修改文档结构后出现错误:Element '***' does not match any field or property of class ***.
Mongodb:修改文档结构后出现错误:Element '***' does not match any field or property of class ***. Mongodb是一种面向文档的 ...
- sencha gridpanel 单元格编辑
{ xtype: 'gridpanel', region: 'north', height: 150, title: 'My Grid Panel', store: 'A_Test_Store', c ...
- 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient
[源码下载] 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient 作者:webabcd 介绍重新想象 Windows 8.1 Store ...
- 轻量级SaaS在线作图工具ProcessOn
俗话说“一图胜千言”,在办公应用领域,流程图是一个非常好的表现企业业务流程或工作岗位规范等内容的展现形式,比如去给客户做调研,回来后都要描述出客户的关键业务流程,谁.什么时候.在什么地方.负责什么事情 ...