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();
        }

  

posted @ 2019-04-20 09:52  踏平扶桑  阅读(2222)  评论(1编辑  收藏  举报