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

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

使用方法:

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

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 @ 2024-09-18 12:41  bearxu  阅读(168)  评论(0)    收藏  举报