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