我有以下代碼并想更改以在 A4 格式頁面上的多列中列印 QRCode:
private int curRow = 0;
private int curCopy = 0;
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
curRow = 0;
curCopy = 0;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var curY = e.MarginBounds.Y;
using (var fontNormal = new Font("Arial", 12))
using (var sf = new StringFormat())
{
sf.Alignment = sf.LineAlignment = StringAlignment.Center;
int itemHeight = (int)fontNormal.GetHeight(e.Graphics) 10;
for (int row = curRow; row < dt.Rows.Count; row )
{
DataRow dr = dt.Rows[row];
if (!string.IsNullOrEmpty(dr.Field<string>(1)) &&
int.TryParse(dr.Field<string>(4)?.ToString(), out int copies))
{
for (int i = curCopy; i < copies; i )
{
var imgRect = new Rectangle(e.MarginBounds.X, curY, 200, 200);
var labelRect = new Rectangle(
imgRect.X,
imgRect.Bottom,
imgRect.Width,
itemHeight);
if (curY imgRect.Height labelRect.Height >= e.MarginBounds.Bottom)
{
curCopy = i;
e.HasMorePages = true;
return;
}
e.Graphics.DrawImage(GenerateQRCODE(dr[1].ToString()), imgRect);
e.Graphics.DrawString(dr[1].ToString(),
fontNormal, Brushes.Black,
labelRect, sf);
curY = labelRect.Bottom 30;
}
}
curRow = row 1;
curCopy = 0;
}
}
refreshprintbtn.Enabled = true;
}
現在列印預覽如下:link to screenthot
我生成了 50 多個 QRCode,所以如果 A4 頁面充滿了 QRCode,我想開始一個新頁面。相同的 QRCode 可以出現多次,并且基于 dr[4] 值,而 dr[1] 是代碼的值。
uj5u.com熱心網友回復:
計算curY以行列印的值的方式與計算以x列列印的值的方式相同。如果值加上影像寬度大于該值,則將影像的寬度加上一些空間偏移x并中斷行。xe.MarginBounds.Right
private int curRow = 0;
private int curCopy = 0;
private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
{
curRow = curCopy = 0;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var curX = e.MarginBounds.X;
var curY = e.MarginBounds.Y;
using (var fontNormal = new Font("Arial", 12))
using (var sf = new StringFormat())
{
sf.Alignment = sf.LineAlignment = StringAlignment.Center;
int itemHeight = (int)fontNormal.GetHeight(e.Graphics) 10;
for (int row = curRow; row < dt.Rows.Count; row )
{
DataRow dr = dt.Rows[row];
if (!string.IsNullOrEmpty(dr.Field<string>(1)) &&
int.TryParse(dr.Field<string>(4)?.ToString(), out int copies))
{
for (int i = curCopy; i < copies; i )
{
var imgRect = new Rectangle(curX, curY, 200, 200);
var labelRect = new Rectangle(
imgRect.X,
imgRect.Bottom,
imgRect.Width,
itemHeight);
// You should do this to dispose of the
// image after printing it.
using (var qrImage = GenerateQRCODE(dr[1].ToString()))
e.Graphics.DrawImage(qrImage, imgRect);
e.Graphics.DrawString(dr[1].ToString(),
fontNormal, Brushes.Black,
labelRect, sf);
curX = imgRect.Right 30;
if (curX imgRect.Width > e.MarginBounds.Right)
{
curX = e.MarginBounds.X;
curY = labelRect.Bottom 30;
}
if (curY imgRect.Height labelRect.Height >= e.MarginBounds.Bottom)
{
curCopy = i 1;
e.HasMorePages = true;
return;
}
}
}
curRow = row 1;
curCopy = 0;
}
}
refreshprintbtn.Enabled = true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/495282.html
