欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用iTextSharp+ZXing.Net+FreeSpire.PDF生成和打印pdf文档

程序员文章站 2022-05-08 07:54:29
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.Net+FreeSpire.PDF三个类库实现了生成pdf、生成条形码和打印pdf功能。 首先在项目作用使用NuGet获取这三个类库的引用。 其次把C:\Windows\Fonts里面的微软雅黑字体 ......

项目需求(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();
        }