|
|
@@ -1,6 +1,7 @@
|
|
|
using SharpCompress.Common;
|
|
|
-using Spire.Barcode;
|
|
|
+//using Spire.Barcode;
|
|
|
using Spire.Pdf;
|
|
|
+using Spire.Pdf.Barcode;
|
|
|
using Spire.Pdf.Graphics;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
@@ -8,6 +9,7 @@ using System.Drawing;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using ThoughtWorks.QRCode.Codec;
|
|
|
using static System.Formats.Asn1.AsnWriter;
|
|
|
|
|
|
namespace Business.Core.Utilities
|
|
|
@@ -20,9 +22,11 @@ namespace Business.Core.Utilities
|
|
|
/// <summary>
|
|
|
/// 生成二维码
|
|
|
/// </summary>
|
|
|
- /// <param name="filePath"></param>
|
|
|
- /// <param name="context"></param>
|
|
|
- public void GenerateQrCode(string filePath, string context)
|
|
|
+ /// <param name="filePath">文件路径</param>
|
|
|
+ /// <param name="context">二维码内容</param>
|
|
|
+ /// <param name="x">x轴偏移量</param>
|
|
|
+ /// <param name="y">y轴偏移量</param>
|
|
|
+ public void GenerateQrCode(string filePath, string context,int x,int y)
|
|
|
{
|
|
|
PdfDocument pdf = new PdfDocument();
|
|
|
//读取文件
|
|
|
@@ -30,29 +34,26 @@ namespace Business.Core.Utilities
|
|
|
PdfPageBase pb = pdf.Pages.Add();
|
|
|
pdf.Pages.Remove(pb);
|
|
|
|
|
|
- //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码图形
|
|
|
- Spire.Barcode.BarcodeSettings settings = new BarcodeSettings();
|
|
|
- settings.Type = BarCodeType.QRCode;
|
|
|
- settings.Data = context;
|
|
|
- settings.Data2D = context;
|
|
|
- settings.X = 0.7f;
|
|
|
- settings.LeftMargin = 0;
|
|
|
- settings.ShowText = false;
|
|
|
- settings.AutoResize=true;
|
|
|
- settings.ShowCheckSumChar = false;
|
|
|
- settings.QRCodeECL = QRCodeECL.Q;
|
|
|
- settings.QRCodeDataMode = QRCodeDataMode.Numeric;
|
|
|
- Spire.Barcode.BarCodeGenerator generator = new BarCodeGenerator(settings);
|
|
|
- Image image = generator.GenerateImage(new Size(20,20));
|
|
|
-
|
|
|
- //绘制二维码图形到PDF
|
|
|
- PdfImage pdfImage = PdfImage.FromImage(image);
|
|
|
+ //生成二维码
|
|
|
+ QRCodeEncoder qRCode = new QRCodeEncoder();
|
|
|
+ qRCode.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
|
|
|
+ qRCode.QRCodeScale = 2; //大小(值越大生成的二维码图片像素越高)-值越大,二维码图片越大
|
|
|
+ qRCode.QRCodeVersion = 0; //版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
|
|
|
+ qRCode.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //错误效验、错误更正(有4个等级)
|
|
|
+ qRCode.QRCodeBackgroundColor = Color.White; //背景色
|
|
|
+ qRCode.QRCodeForegroundColor = Color.Black; //前景色
|
|
|
+ System.Drawing.Bitmap bt = qRCode.Encode(context, Encoding.UTF8);
|
|
|
+
|
|
|
+ //循环每一页,绘制二维码图形到PDF
|
|
|
+ PdfImage pdfImage = PdfImage.FromImage(bt);
|
|
|
for (int i = 0; i < pdf.Pages.Count; i++)
|
|
|
{
|
|
|
var page = pdf.Pages[i];
|
|
|
- page.Canvas.DrawImage(pdfImage, page.Size.Width - pdfImage.Width + 9, 0);
|
|
|
+ //位于页面右上角
|
|
|
+ //page.Canvas.DrawImage(pdfImage, page.Size.Width - pdfImage.Width - 20, 10);
|
|
|
+ //自定义位置
|
|
|
+ page.Canvas.DrawImage(pdfImage, x, y);
|
|
|
}
|
|
|
-
|
|
|
//保存文档
|
|
|
pdf.SaveToFile(filePath);
|
|
|
}
|
|
|
@@ -60,9 +61,11 @@ namespace Business.Core.Utilities
|
|
|
/// <summary>
|
|
|
/// 生成条形码
|
|
|
/// </summary>
|
|
|
- /// <param name="filePath"></param>
|
|
|
- /// <param name="context"></param>
|
|
|
- public void GenerateBarcode(string filePath, string context, string savePath)
|
|
|
+ /// <param name="filePath">文件路径</param>
|
|
|
+ /// <param name="context">挑衅码内容</param>
|
|
|
+ /// <param name="x">x轴偏移量</param>
|
|
|
+ /// <param name="y">y轴偏移量</param>
|
|
|
+ public void GenerateBarcode(string filePath, string context, int x, int y)
|
|
|
{
|
|
|
PdfDocument pdf = new PdfDocument();
|
|
|
//读取文件
|
|
|
@@ -70,28 +73,104 @@ namespace Business.Core.Utilities
|
|
|
PdfPageBase pb = pdf.Pages.Add();
|
|
|
pdf.Pages.Remove(pb);
|
|
|
|
|
|
- //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建条形码图形
|
|
|
- Spire.Barcode.BarcodeSettings settings = new BarcodeSettings();
|
|
|
- settings.Type = BarCodeType.Codabar;
|
|
|
- settings.Data = context;
|
|
|
- settings.Data2D = context;
|
|
|
- settings.ShowText = false;
|
|
|
- settings.ShowTopText = false;
|
|
|
- settings.ShowTextOnBottom = false;
|
|
|
- settings.ShowCheckSumChar = false;
|
|
|
- Spire.Barcode.BarCodeGenerator generator = new BarCodeGenerator(settings);
|
|
|
- Image image = generator.GenerateImage(new Size(40,20));
|
|
|
+ PdfCode39Barcode pdfCode = new PdfCode39Barcode(context);
|
|
|
+ pdfCode.BarcodeToTextGapHeight = 2f;
|
|
|
+ pdfCode.TextDisplayLocation = TextLocation.Bottom;
|
|
|
+ pdfCode.BarHeight = 40;
|
|
|
+ pdfCode.NarrowBarWidth = 0.5f;
|
|
|
+ pdfCode.TextColor = Color.Black;
|
|
|
|
|
|
//绘制二维码图形到PDF
|
|
|
- PdfImage pdfImage = PdfImage.FromImage(image);
|
|
|
for (int i = 0; i < pdf.Pages.Count; i++)
|
|
|
{
|
|
|
var page = pdf.Pages[i];
|
|
|
- page.Canvas.DrawImage(pdfImage, page.Size.Width / 2 - pdfImage.Width / 2, page.Size.Height - pdfImage.Height);
|
|
|
+ //位于页面底部中间
|
|
|
+ //pdfCode.Draw(page, new Point((int)(page.Size.Width / 2 - pdfCode.Size.Width / 2), (int)(page.Size.Height - pdfCode.Size.Height)));
|
|
|
+ //自定义位置
|
|
|
+ pdfCode.Draw(page, new Point(x, y));
|
|
|
}
|
|
|
|
|
|
//保存文档
|
|
|
- pdf.SaveToFile(savePath);
|
|
|
+ pdf.SaveToFile(filePath);
|
|
|
}
|
|
|
+
|
|
|
+ #region spire.barcode生成二维码和条形吗
|
|
|
+ ///// <summary>
|
|
|
+ ///// 生成二维码
|
|
|
+ ///// </summary>
|
|
|
+ ///// <param name="filePath"></param>
|
|
|
+ ///// <param name="context"></param>
|
|
|
+ //public void GenerateQrCode(string filePath, string context)
|
|
|
+ //{
|
|
|
+ // PdfDocument pdf = new PdfDocument();
|
|
|
+ // //读取文件
|
|
|
+ // pdf.LoadFromFile(filePath);
|
|
|
+ // PdfPageBase pb = pdf.Pages.Add();
|
|
|
+ // pdf.Pages.Remove(pb);
|
|
|
+
|
|
|
+ // //使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码图形
|
|
|
+ // Spire.Barcode.BarcodeSettings settings = new BarcodeSettings();
|
|
|
+ // settings.Type = BarCodeType.QRCode;
|
|
|
+ // settings.Data = context;
|
|
|
+ // settings.Data2D = context;
|
|
|
+ // settings.X = 0.7f;
|
|
|
+ // settings.LeftMargin = 0;
|
|
|
+ // settings.ShowText = false;
|
|
|
+ // settings.AutoResize=true;
|
|
|
+ // settings.ShowCheckSumChar = false;
|
|
|
+ // settings.QRCodeECL = QRCodeECL.Q;
|
|
|
+ // settings.QRCodeDataMode = QRCodeDataMode.Numeric;
|
|
|
+ // Spire.Barcode.BarCodeGenerator generator = new BarCodeGenerator(settings);
|
|
|
+ // Image image = generator.GenerateImage(new Size(20,20));
|
|
|
+
|
|
|
+ // //绘制二维码图形到PDF
|
|
|
+ // PdfImage pdfImage = PdfImage.FromImage(image);
|
|
|
+ // for (int i = 0; i < pdf.Pages.Count; i++)
|
|
|
+ // {
|
|
|
+ // var page = pdf.Pages[i];
|
|
|
+ // page.Canvas.DrawImage(pdfImage, page.Size.Width - pdfImage.Width + 9, 0);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // //保存文档
|
|
|
+ // pdf.SaveToFile(filePath);
|
|
|
+ //}
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// 生成条形码
|
|
|
+ ///// </summary>
|
|
|
+ ///// <param name="filePath"></param>
|
|
|
+ ///// <param name="context"></param>
|
|
|
+ //public void GenerateBarcode(string filePath, string context, string savePath)
|
|
|
+ //{
|
|
|
+ //PdfDocument pdf = new PdfDocument();
|
|
|
+ ////读取文件
|
|
|
+ //pdf.LoadFromFile(filePath);
|
|
|
+ //PdfPageBase pb = pdf.Pages.Add();
|
|
|
+ //pdf.Pages.Remove(pb);
|
|
|
+
|
|
|
+ ////使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建条形码图形
|
|
|
+ //Spire.Barcode.BarcodeSettings settings = new BarcodeSettings();
|
|
|
+ //settings.Type = BarCodeType.Codabar;
|
|
|
+ //settings.Data = context;
|
|
|
+ //settings.Data2D = context;
|
|
|
+ //settings.ShowText = false;
|
|
|
+ //settings.ShowTopText = false;
|
|
|
+ //settings.ShowTextOnBottom = false;
|
|
|
+ //settings.ShowCheckSumChar = false;
|
|
|
+ //Spire.Barcode.BarCodeGenerator generator = new BarCodeGenerator(settings);
|
|
|
+ //Image image = generator.GenerateImage(new Size(40,20));
|
|
|
+
|
|
|
+ ////绘制二维码图形到PDF
|
|
|
+ //PdfImage pdfImage = PdfImage.FromImage(image);
|
|
|
+ //for (int i = 0; i < pdf.Pages.Count; i++)
|
|
|
+ //{
|
|
|
+ // var page = pdf.Pages[i];
|
|
|
+ // page.Canvas.DrawImage(pdfImage, page.Size.Width / 2 - pdfImage.Width / 2, page.Size.Height - pdfImage.Height);
|
|
|
+ //}
|
|
|
+
|
|
|
+ ////保存文档
|
|
|
+ //pdf.SaveToFile(savePath);
|
|
|
+ //}
|
|
|
+ #endregion
|
|
|
}
|
|
|
}
|