我正在将BarcodeDataMatrix添加到现有的pdf文档中。由于此文档是自动处理的,因此每个模块的大小必须符合自动化的规格(高于默认生成的)。
这可以通过使用barcode.CreateFormX((Canvas)null, moduleSize, pdfDocument)
来完成,其中moduleSize是一个影响代码中每个点大小的数字。
我遇到的问题是:每当我设置moduleSize> 1时,代码被裁剪,即顶部和右边的部分都缺失。
当我查看源代码时,我找到了this:
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 [units]的矩形。然而,它返回一个矩形,其中包含模块数量的条形码。所以这意味着,对于moduleSize> 1的每个值,返回的rect太小了。在Placebarcode
回归之后,CreateFormX
用返回的rect做了一个SetBBox()
,在我看来,每个moduleSize> 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 );
代替。