|
|
@@ -22,11 +22,40 @@ namespace Business.Core.Utilities
|
|
|
/// <summary>
|
|
|
/// 生成二维码
|
|
|
/// </summary>
|
|
|
+ /// <param name="context">二维码内容</param>
|
|
|
+ public Image GenerateQrCode(string context)
|
|
|
+ {
|
|
|
+ //使用ThoughtWorks.QRCode.Codec生成二维码
|
|
|
+ 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);
|
|
|
+
|
|
|
+ //bt.Save("二维码文件保存路径");//如果需要保存二维码,则返回文件路径即可
|
|
|
+ return bt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 生成条形码
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context">条形码内容</param>
|
|
|
+ public void GenerateBarcode(string context)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Pdf文档添加二维码
|
|
|
+ /// </summary>
|
|
|
/// <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)
|
|
|
+ public void PdfAddQrCode(string filePath, string context,int x,int y)
|
|
|
{
|
|
|
PdfDocument pdf = new PdfDocument();
|
|
|
//读取文件
|
|
|
@@ -35,22 +64,13 @@ namespace Business.Core.Utilities
|
|
|
pdf.Pages.Remove(pb);
|
|
|
|
|
|
//生成二维码
|
|
|
- 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);
|
|
|
-
|
|
|
+ Image image = GenerateQrCode(context);
|
|
|
+
|
|
|
//循环每一页,绘制二维码图形到PDF
|
|
|
- PdfImage pdfImage = PdfImage.FromImage(bt);
|
|
|
+ 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 - 20, 10);
|
|
|
//自定义位置
|
|
|
page.Canvas.DrawImage(pdfImage, x, y);
|
|
|
}
|
|
|
@@ -59,118 +79,37 @@ namespace Business.Core.Utilities
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 生成条形码
|
|
|
+ /// Pdf文档添加条形码
|
|
|
/// </summary>
|
|
|
/// <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)
|
|
|
+ public void PdfAddBarcode(string filePath, string context, int x, int y)
|
|
|
{
|
|
|
PdfDocument pdf = new PdfDocument();
|
|
|
//读取文件
|
|
|
pdf.LoadFromFile(filePath);
|
|
|
PdfPageBase pb = pdf.Pages.Add();
|
|
|
pdf.Pages.Remove(pb);
|
|
|
-
|
|
|
- PdfCode39Barcode pdfCode = new PdfCode39Barcode(context);
|
|
|
+ //生成条形码
|
|
|
+ PdfCode39Barcode pdfCode = new PdfCode39Barcode(context); //构造条形码内容
|
|
|
pdfCode.BarcodeToTextGapHeight = 2f;
|
|
|
- pdfCode.TextDisplayLocation = TextLocation.Bottom;
|
|
|
- pdfCode.BarHeight = 40;
|
|
|
- pdfCode.NarrowBarWidth = 0.5f;
|
|
|
- pdfCode.TextColor = Color.Black;
|
|
|
+ pdfCode.TextDisplayLocation = TextLocation.Bottom; //条形码文字位置
|
|
|
+ pdfCode.BarHeight = 40; //高度
|
|
|
+ pdfCode.NarrowBarWidth = 0.5f; //宽度比例
|
|
|
+ pdfCode.TextColor = Color.Black; //文字颜色
|
|
|
|
|
|
- //绘制二维码图形到PDF
|
|
|
+ //绘制条形码图形到PDF
|
|
|
for (int i = 0; i < pdf.Pages.Count; i++)
|
|
|
{
|
|
|
var page = pdf.Pages[i];
|
|
|
- //位于页面底部中间
|
|
|
- //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(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
|
|
|
}
|
|
|
}
|