项目需求(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. scp: command not found

    scp 不能用? [root@doc]# scp jdk-8u144-linux-x64.tar.gz root@10.10.10.17:/root/ root@10.10.10.17's passw ...

  2. git自定义项目钩子和全局钩子

    钩子介绍 自定义钩子分为:项目钩子和全局钩子 自定义全局钩子: 全局钩子目录结构: (注意:excludes目录结构是我们自定义的目录,规则逻辑在update.d/update.py脚本里实现的,非g ...

  3. leetcode15

    class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); Li ...

  4. JavaScript: RegExp check UserName

    Function : We can use regular expressions to check user input data formats. Homework: Check user inp ...

  5. Sql Server数据库之四个增删改查

    一.数据库的增删改查 1.新建数据库 create database students on primary ( name="students_data",--主数据文件的逻辑名 ...

  6. AR涂涂乐

    <1> 涂涂乐着色 https://blog.csdn.net/begonia__z/article/details/51282932 http://www.manew.com/blog- ...

  7. BM递推

    从别的大佬处看到的模板 #include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f ...

  8. scrapy+redis去重实现增量抓取

    class ProjectnameDownloaderMiddleware(object): # Not all methods need to be defined. If a method is ...

  9. HDU 5828 Rikka with Sequence(线段树区间加开根求和)

    Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...

  10. java 期末考试复习

      //Scanner这样写? Scanner input = new Scanner(System.in); //不断获得下一个单词 names[i] = toTitleCase(input.nex ...