0

我正在将 BarcodeDataMatrix 添加到现有的 pdf 文档中。由于该文档是自动处理的,因此每个模块的大小必须根据自动化的规范(即高于默认生成的)。

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

我遇到的问题是:每当我设置 moduleSize > 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 [单位] 的矩形。然而,它返回一个矩形,其条码大小为模块数。所以这意味着,对于 moduleSize > 1 的每个值,返回的矩形都太小了。Placebarcode返回后,对返回的矩形CreateFormX进行 aSetBBox()处理,在我看来,这对于每个 moduleSize > 1 来说都太小了。

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

我现在解决它的方法是PlaceBarcode直接调用并将条形码或多或少手动添加到页面中。

4

1 回答 1

0

此代码有效(而不是调用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 );

反而。

于 2019-02-28T12:48:32.327 回答