C#使用iTextSharp+ZXing.Net+FreeSpire.PDF生成和打印pdf文档
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.Net+FreeSpire.PDF三个类库实现了生成pdf、生成条形码和打印pdf功能。
首先在项目作用使用NuGet获取这三个类库的引用。
其次把C:\Windows\Fonts里面的微软雅黑字体复制到bin\debug\Fonts目录下
以下为实现代码:
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using iTextSharp.text;
using iTextSharp.text.pdf;
using it = iTextSharp.text;
using System.Drawing.Imaging; //打印确认表
private void Btn_print_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\PDFs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//使用微软雅黑字体,解决中文无法显示,注意msyh.ttc后面的,0是必须的
BaseFont baseFont = BaseFont.CreateFont(Application.StartupPath + "\\Fonts\\msyh.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
it.Font font = new it.Font(baseFont, 14, it.Font.BOLD, it.BaseColor.BLACK);
using (var doc1 = new Document(PageSize.A4, 5, 5, 35, 5))
{
PdfWriter.GetInstance(doc1, new FileStream(path + "\\Doc1.pdf", FileMode.Create));
doc1.Open();
//生成条形码
var bmp = ZXingHelper.GenerateBarcode("12345678900000");
var txm = it.Image.GetInstance(System.Drawing.Image.FromHbitmap(bmp), ImageFormat.Bmp);
//txm.ScalePercent(24f);
txm.ScaleAbsoluteHeight(40);
txm.SetAbsolutePosition(doc1.PageSize.Width - 220, doc1.PageSize.Height - 32);
doc1.Add(txm);//把条形码放到页面右上角 PdfPTable table = new PdfPTable(7); PdfPCell cell = new PdfPCell(new Phrase("X X X X 验 证 意 见 确 认 处 理 记 录 表", font))
{
Colspan = 7,
Border = 0,
MinimumHeight = 30,
HorizontalAlignment = 1
};
table.AddCell(cell); it.Font fontCell = new it.Font(baseFont, 10, it.Font.NORMAL, it.BaseColor.BLACK);
float[] cellWidths = new float[] { 10, 35, 35, 45, 40, 65, 40 };
table.SetWidths(cellWidths); cell = new PdfPCell(new Phrase("县区: XX: XX: XXXX: 日期: 月 日 时间: ", fontCell))
{
Colspan = 7,
Border = 0,
HorizontalAlignment = 0,
MinimumHeight = 15
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("座\r\n号", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5,
MinimumHeight = 35
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("姓 名", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("xx号", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("XX(通过/未通过/未验证)", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("XXXX(采集/未采集)", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("验证意见(XX/XX/XXXX/XXXX)", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
};
table.AddCell(cell); cell = new PdfPCell(new Phrase("XXXX确认处理情况)", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
};
table.AddCell(cell); for (var i = 1; i < 31; i++)
{
table.AddCell(new PdfPCell(new Phrase(i.ToString(), fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5,
MinimumHeight = 17
});
table.AddCell(new PdfPCell(new Phrase("正大·光明龑", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
});
table.AddCell(new PdfPCell(new Phrase(" ", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
});
table.AddCell(new PdfPCell(new Phrase(" ", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
});
table.AddCell(new PdfPCell(new Phrase(" ", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
});
table.AddCell(new PdfPCell(new Phrase(" ", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
});
table.AddCell(new PdfPCell(new Phrase(" ", fontCell))
{
HorizontalAlignment = 1,
VerticalAlignment = 5
});
} fontCell = new it.Font(baseFont, 12, it.Font.NORMAL, it.BaseColor.BLACK);
cell = new PdfPCell(new Phrase("XXXXX签字: XXXXX丙签字: ", fontCell))
{
Colspan = 7,
Border = 0,
MinimumHeight = 20,
HorizontalAlignment = 0
};
table.AddCell(cell); it.Font fontFooter = new it.Font(baseFont, 10, it.Font.NORMAL, it.BaseColor.BLACK);
PdfPCell cellFooter = new PdfPCell(new Phrase("说明:1.文字描述文字描述文字描述文字描述文字描述文字描述文字描述\r\n2、文字描述文字描述文字描述文字描述文字描述文字描述文字描述文字描述文字描述文字描述;3、文字描述文字描述文字描述文字描述文字描述文字描述文字描述文字描述,存档备查。", fontFooter))
{
Colspan = 7,
Border = 0,
MinimumHeight = 40,
Left = 0,
PaddingLeft = 0,
HorizontalAlignment = 0//0=Left, 1=Centre, 2=Right
};
table.AddCell(cellFooter);
doc1.Add(table);
doc1.Close();
}
if (MessageBox.Show("生成成功,是否打印?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument(path + "\\Doc1.pdf");
pdf.Print();
} }
条形码生成的代码
public static IntPtr GenerateBarcode(string barcode)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_128;
writer.Options = new ZXing.Common.EncodingOptions
{
Height = 100,
Width = 200,
PureBarcode = false
}; Bitmap bitmap = writer.Write(barcode);
return bitmap.GetHbitmap();
}
C#使用iTextSharp+ZXing.Net+FreeSpire.PDF生成和打印pdf文档的更多相关文章
- 自动生成并导出word文档
今天很荣幸又破解一现实难题:自动生成并导出word文档 先看页面效果: word效果: 代码: 先搭建struts2项目 创建action,并在struts.xml完成注册 <?xml vers ...
- 无需编译、快速生成 Vue 风格的文档网站
无需编译.快速生成 Vue 风格的文档网站 https://docsify.js.org/#/#coverpage https://github.com/QingWei-Li/docsify/
- 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理
http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...
- 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档
对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...
- 基于数据库的自动化生成工具,自动生成JavaBean、数据库文档、框架代码等(v5.8.8版)
TableGo v5.8.8版震撼发布,此次版本更新如下: 1.新增两个扩展字段,用于生成自定义模板时使用. 2.自定义模板新增模板目录,可以选择不同分类目录下的模 ...
- SpringBoot入门教程(二十)Swagger2-自动生成RESTful规范API文档
Swagger2 方式,一定会让你有不一样的开发体验:功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能:及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档 ...
- 生成表结构数据库文档sql语句
CREATE PROCEDURE [dbo].[生成表结构数据库文档]ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from ...
- 用python批量生成简单的xml文档
最近生成训练数据时,给一批无效的背景图片生成对应的xml文档,我用python写了一个简单的批量生成xml文档的demo,遇见了意外的小问题,记录一下. 报错问题为:ImportError: No m ...
- apidoc 生成Restful web Api文档
在服务器项目开发过程中,总会牵扯到接口文档的设计与编写,之前使用的都是office写一个文档,不够直观.下面介绍一种工具生成Apidoc.,该工具是Nodejs的模块,请务必在使用前安装好nodejs ...
随机推荐
- 【原】The Linux Command Line - Workiing with commands
● type – Indicate how a command name is interpreted● which – Display which executable program will b ...
- javascript中正则表达式中的 match,exec,test,replace 之我理解
这个正则 ($&) 的语法: https://msdn.microsoft.com/library/3k9c4a32(v=vs.94).aspx 在ECMAScript中对这几个的说明: ma ...
- cdnbest里站点域名不同步到节点,报400错误的一般原因
报400错误一般是站点里的域名没有同步到节点上面的原因,产生的原因一般是下面两点原因: 1.检查节点列表如下图所示的状态是否打钩,这是节点和主控的通信状态,打叉表示连接有问题 这里打叉的几种原因(1) ...
- Java 学习笔记提高篇
Java笔记(提高篇)整理 主要内容: 面向对象 异常 数组 常用类 集合 IO流 线程 反射 Socket编程 1. 面向对象 1.1包 用来管理Java中的类, 类似文件夹管理文件一样. 因 ...
- Html利用CSS布局技巧
单列布局水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的 ...
- hdu 4714 树+DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...
- yum安装命令:遇到的问题报错如下: File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: 通过看报错可以了解到是使用了python2的语法,所以了解到当前yum使用的Python2,因为我单独安装了python3,且python3设置为默认版本了,所以导致语法问题 解决方法: 使用python2.6 yum install
1.安装zip yum install -y unzip zip 2.安装lrszs yum -y install lrzsz 3.安装scp 遇到下面的问题: 结果提示: No package sc ...
- 42 【docker】run命令
最常用的两个option是,网络端口映射,和文件共享 最基本的启动命令(从image创建一个container并启动):docker run -d <image-name> -d:表示守护 ...
- 244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...
- 【转载】重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...