c# 在PDFsharp里绘制ZXing生成的条形码矢量图

PDFsharp支持 在pdf文档里生成矢量格式的二维码, 单支持的条码格式比较少,不支持常用的93码和二维码等。
ZXing 支持的条码多,但无法在PDFsharp里直接绘制矢量格式的图形。
这里一个扩展就可以 在PDFsharp里绘制ZXing生成的条形码矢量图了。

使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using ZXing;
using ZXing.Common;
using Extensions; //引入扩展
 
PdfDocument newDocument = new PdfDocument(); // 创建pdf文档
PdfPage page = newDocument.AddPage();   //添加页面
XGraphics gfx = XGraphics.FromPdfPage(page);
var barcodeWriter = new ZXing.BarcodeWriterGeneric
{
    Format = BarcodeFormat.CODE_93, //条码格式
    Options = new EncodingOptions { Height = 80, Width = 200 } //条码尺寸
};
var bitMatrix = barcodeWriter.Encode("24341873"); //条码内容
gfx.DrawBitMatrix(bitMatrix, new XPoint(100, 100));  //绘制条码
newDocument.Save("output.pdf");   //保存文件
newDocument.Close();

 

扩展: XGraphicsExtensions.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using PdfSharp.Drawing;
using ZXing.Common;
namespace Extensions
{
    public static class XGraphicsExtensions
    {
        public static void DrawBitMatrix(this XGraphics gfx, BitMatrix matrix, XPoint offset)
        {
            if (matrix == null)
                return;
            int width = matrix.Width;
            int height = matrix.Height;
            var processed = new BitMatrix(width, height);
            bool currentIsBlack = false;
            int startPosX = 0;
            int startPosY = 0;
            //test border
            //gfx.DrawRectangle(new XPen(XColor.FromArgb(0, 0, 0), 0.5), new XRect(offset.X, offset.Y, matrix.Width, matrix.Height));
            for (int x = 0; x < width; x++)
            {
                int endPosX;
                for (int y = 0; y < height; y++)
                {
                    if (processed[x, y])
                        continue;
 
                    processed[x, y] = true;
 
                    if (matrix[x, y])
                    {
                        if (!currentIsBlack)
                        {
                            startPosX = x;
                            startPosY = y;
                            currentIsBlack = true;
                        }
                    }
                    else
                    {
                        if (currentIsBlack)
                        {
                            FindMaximumRectangle(matrix, processed, startPosX, startPosY, y, out endPosX);
                            var rect = new XRect(startPosX + offset.X, startPosY + offset.Y, endPosX - startPosX + 1, y - startPosY);
                            gfx.DrawRectangle(XBrushes.Black, rect);
                            currentIsBlack = false;
                        }
                    }
                }
                if (currentIsBlack)
                {
                    FindMaximumRectangle(matrix, processed, startPosX, startPosY, height, out endPosX);
                    var rect = new XRect(startPosX + offset.X, startPosY + offset.Y, endPosX - startPosX + 1, height - startPosY);
                    gfx.DrawRectangle(XBrushes.Black, rect);
                    currentIsBlack = false;
                }
            }
        }
        private static void FindMaximumRectangle(BitMatrix matrix, BitMatrix processed, int startPosX, int startPosY, int endPosY, out int endPosX)
        {
            endPosX = startPosX;
 
            for (int x = startPosX + 1; x < matrix.Width; x++)
            {
                for (int y = startPosY; y < endPosY; y++)
                {
                    if (!matrix[x, y])
                    {
                        return;
                    }
                }
                endPosX = x;
                for (int y = startPosY; y < endPosY; y++)
                {
                    processed[x, y] = true;
                }
            }
        }
    }
}

  

 

posted @   bearxu  阅读(55)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示