项目需求(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文档的更多相关文章

  1. 自动生成并导出word文档

    今天很荣幸又破解一现实难题:自动生成并导出word文档 先看页面效果: word效果: 代码: 先搭建struts2项目 创建action,并在struts.xml完成注册 <?xml vers ...

  2. 无需编译、快速生成 Vue 风格的文档网站

    无需编译.快速生成 Vue 风格的文档网站 https://docsify.js.org/#/#coverpage https://github.com/QingWei-Li/docsify/

  3. 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理

    http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...

  4. 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档

    对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...

  5. 基于数据库的自动化生成工具,自动生成JavaBean、数据库文档、框架代码等(v5.8.8版)

    TableGo v5.8.8版震撼发布,此次版本更新如下:          1.新增两个扩展字段,用于生成自定义模板时使用.          2.自定义模板新增模板目录,可以选择不同分类目录下的模 ...

  6. SpringBoot入门教程(二十)Swagger2-自动生成RESTful规范API文档

    Swagger2 方式,一定会让你有不一样的开发体验:功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能:及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档 ...

  7. 生成表结构数据库文档sql语句

    CREATE PROCEDURE [dbo].[生成表结构数据库文档]ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from ...

  8. 用python批量生成简单的xml文档

    最近生成训练数据时,给一批无效的背景图片生成对应的xml文档,我用python写了一个简单的批量生成xml文档的demo,遇见了意外的小问题,记录一下. 报错问题为:ImportError: No m ...

  9. apidoc 生成Restful web Api文档

    在服务器项目开发过程中,总会牵扯到接口文档的设计与编写,之前使用的都是office写一个文档,不够直观.下面介绍一种工具生成Apidoc.,该工具是Nodejs的模块,请务必在使用前安装好nodejs ...

随机推荐

  1. zookeeper 集群部署

    参考: https://www.cnblogs.com/linuxprobe/p/5851699.html

  2. python 对象转字典

    从数据库中取出的数数据是 对象类型的,不能直接展示出来,需要转成字典类型,然后转成json 字符串,传给前端: data = {} data.update(obj.__dict__) print(da ...

  3. Java6及以上版本对synchronized的优化

    目录 1.概述 2.实现同步的基础 3.实现方式 4.Java对象头(存储锁类型) 5.优化后synchronized锁的分类 6.锁的升级(进化) 6-1.偏向锁 6-2.轻量级锁 6-3.锁的比较 ...

  4. NodeJs命令

    cd命令,就是change directory的缩写,表示更改当前目录 cls命令,清屏.清屏幕命令(CLS,CLear Screen) tab键,自动补全. 上键,提示最近的命令   在cmd窗口 ...

  5. 【收藏】UICrawler

    基于 Appium 的 App UI 遍历 & Monkey 工具 (支持操作步骤回放) UICrawler https://github.com/lgxqf/UICrawler 基于Appi ...

  6. [leetcode]76. Minimum Window Substring最小字符串窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. [leetcode]42. Trapping Rain Water雨水积水问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. makefile中一些编译器选项

    Libraries Static Libraries a collection of ordinary object files (目标文件的集合) loaded at program link ti ...

  9. sql语句-单表查询

    一:单表查询 CREATE TABLE `Score`( `s_id` ), `c_id` ), `s_score` ), PRIMARY KEY(`s_id`,`c_id`) ); ); ); ); ...

  10. Map 数据结构

    JavaScript 的对象(Object),本质上是键值对的集合(Hash 结构),但是传统上只能用字符串当作键.这给它的使用带来了很大的限制. 为了解决这个问题,ES6 提供了 Map 数据结构. ...