设为首页 加入收藏

TOP

C#使用iTextSharp+ZXing.Net+FreeSpire.PDF生成和打印pdf文档(一)
2019-09-17 18:56:04 】 浏览:57
Tags:使用 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,
                        MinimumH
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Unity3D开发游戏世界天空盒 下一篇Websocket-Sharp获取客户端IP地址..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目