更改Bar Codedatamatrx中模块的大小,代码图像



我正在为现有的pdf文档添加一个bar编码。由于该文档是自动处理的,因此每个模块的大小必须根据自动化的特征(该)(高于默认生成的默认值)。

这可以通过使用barcode.CreateFormX((Canvas)null, moduleSize, pdfDocument)来完成,其中模块化是一个影响代码中每个点大小的数字。

我遇到的问题是:每当我设置模块化> 1时,代码是裁剪的,即零件,右侧是丢失的。

当我研究源时,我发现了这个:

        public virtual Rectangle PlaceBarcode(PdfCanvas canvas, Color foreground, float moduleSide) {
        if (image == null) {
            return null;
        }
        if (foreground != null) {
            canvas.SetFillColor(foreground);
        }
        int w = width + 2 * ws;
        int h = height + 2 * ws;
        int stride = (w + 7) / 8;
        for (int k = 0; k < h; ++k) {
            int p = k * stride;
            for (int j = 0; j < w; ++j) {
                int b = image[p + j / 8] & 0xff;
                b <<= j % 8;
                if ((b & 0x80) != 0) {
                    canvas.Rectangle(j * moduleSide, (h - k - 1) * moduleSide, moduleSide, moduleSide);
                }
            }
        }
        canvas.Fill();
        return GetBarcodeSize();
    }

        public virtual PdfFormXObject CreateFormXObject(Color foreground, float moduleSide, PdfDocument document) {
        PdfFormXObject xObject = new PdfFormXObject((Rectangle)null);
        Rectangle rect = PlaceBarcode(new PdfCanvas(xObject, document), foreground, moduleSide);
        xObject.SetBBox(new PdfArray(rect));
        return xObject;
    }

因此,CreateFormX调用PlaceBarcode在条形码中的每条"线"上都划出了Modulsize [单位]的矩形。但是,它返回带有条形码数量的矩形。因此,这意味着,对于模块化> 1的每个值,返回的rect太小。在Placebarcode返回之后,CreateFormX使用返回的RECT进行SetBBox(),我认为每个模块> 1个太小。

现在的问题:我的分析是错误的,如果是的,我该如何解决我的问题?

我暂时解决的方法是直接调用PlaceBarcode,并在页面上或多或少地添加条形码。

此代码有效(而不是调用CreateFormXObject

        PdfFormXObject bcdObject = new PdfFormXObject((Rectangle)null);
        barcode.PlaceBarcode( new PdfCanvas( bcdObject, pdfDocument ), iText.Kernel.Colors.ColorConstants.BLACK, moduleSize);
        Rectangle r = new Rectangle( barcode.GetHeight() * moduleSize, barcode.GetWidth() * moduleSize );
        bcdObject.SetBBox(new PdfArray(r));

如果我没记错的话,错误是在PlaceBarcode()的最后一行中:

 return GetBarcodeSize();

应该阅读

 return GetBarcodeSize(, modulSide, ModulSide );

而不是。

最新更新