【发布时间】:2014-08-18 13:20:56
【问题描述】:
我制作了一张底部有一个列表视图以列出所有行项目的发票。这可能会超过一页标记,因此我重载了 DocumentPaginator 类以允许我打印多页。但是,有时它会在列表视图的一行中间剪切页面。我找到了一篇使用 wpf 控件的位图的文章,然后检查一行像素的颜色以确定是否有空格或数据(代码如下)。但是,当我将控件制作为位图时,间距和行高与我将 wpf 控件打印为 xps 或打印到打印机时不同。关于如何智能地分页或让位图与 xps 匹配的任何其他想法?
private void GetGoodCut()
{
int goodCut = _lastCut;
int lastNumber = 0;
int numberCount = 1;
// At most, it will take 32 pixel lines to find a white space
for (int i = 0; i < 32; i++)
{
int number = rowPixelWhiteCount(_bitmap, goodCut);
goodCut--;
if (number == lastNumber)
{
numberCount++;
// White space count between LV lines is 12
if (numberCount == 12)
{
lastNumber = i - 6;
break;
}
}
else
{
// If we started inside a white space, can break if starting before/at middle of the white space
if (numberCount > 5)
{
lastNumber = i - 6;
break;
}
numberCount = 1;
}
lastNumber = number;
}
_lastCut -= lastNumber;
}
private int rowPixelWhiteCount(System.Drawing.Bitmap bmp, int row)
{
int colorCount = 0;
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
int stride = bmpData.Stride;
IntPtr firstPixelInImage = bmpData.Scan0;
unsafe
{
byte* p = (byte*)(void*)firstPixelInImage;
p += stride * row; // find starting pixel of the specified row
for (int column = 0; column < bmp.Width; column++)
{
// Printing in black/white, look for non-black pixels
byte blue = p[0];
byte red = p[1];
byte green = p[3];
if (blue > 0 && red > 0 && green > 0)
colorCount++;
// go to next pixel
p += 3;
}
}
bmp.UnlockBits(bmpData);
count.Add(colorCount);
return colorCount;
}
【问题讨论】:
标签: c# wpf printing page-break multipage