using SharpCompress.Common;
//using Spire.Barcode;
using Spire.Pdf;
using Spire.Pdf.Barcode;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
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
{
///
/// 二维码生成工具
///
public class QRcodeHelper
{
///
/// 生成二维码
///
/// 二维码内容
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;
}
///
/// 生成条形码
///
/// 条形码内容
public void GenerateBarcode(string context)
{
}
///
/// Pdf文档添加二维码
///
/// 文件路径
/// 二维码内容
/// x轴偏移量
/// y轴偏移量
public void PdfAddQrCode(string filePath, string context,int x,int y)
{
PdfDocument pdf = new PdfDocument();
//读取文件
pdf.LoadFromFile(filePath);
PdfPageBase pb = pdf.Pages.Add();
pdf.Pages.Remove(pb);
//生成二维码
Image image = GenerateQrCode(context);
//循环每一页,绘制二维码图形到PDF
PdfImage pdfImage = PdfImage.FromImage(image);
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
//自定义位置
page.Canvas.DrawImage(pdfImage, x, y);
}
//保存文档
pdf.SaveToFile(filePath);
}
///
/// Pdf文档添加条形码
///
/// 文件路径
/// 挑衅码内容
/// x轴偏移量
/// 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); //构造条形码内容
pdfCode.BarcodeToTextGapHeight = 2f;
pdfCode.TextDisplayLocation = TextLocation.Bottom; //条形码文字位置
pdfCode.BarHeight = 40; //高度
pdfCode.NarrowBarWidth = 0.5f; //宽度比例
pdfCode.TextColor = Color.Black; //文字颜色
//绘制条形码图形到PDF
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
//自定义位置
pdfCode.Draw(page, new Point(x, y));
}
//保存文档
pdf.SaveToFile(filePath);
}
}
}