我正在尝试使用string
打印一些Graphicss.DrawString()
。我已将边距设置为printdocument
,但不是从页面的原点开始。我已将margins
设置为(0,0,0,0)
,但不知何故,它会在页面顶部边缘下方半厘米处打印。另一件事是它可以从左边缘打印。
以下是我的代码。
private void button1_Click(object sender, EventArgs e)
{
////PaperSize pkCustomSize1 = new PaperSize("First custom size", 1020, 3517);
////printDocument1.DefaultPageSettings.PaperSize = pkCustomSize1;
printPreviewDialog1.Document = printDocument1;
printDocument1.PrinterSettings.PrinterName = this.comboBox1.Text;
Margins margins = new Margins(0, 0, 0, 0);
printDocument1.PrinterSettings.DefaultPageSettings.Margins = margins;
printPreviewDialog1.Show();
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int resX = GetPrinterResolutionX(comboBox1);
int resY = PrnOpra.GetPrinterResolutionY(comboBox1);
Graphics g = e.Graphics;
float scale = resX / ScrnRes;
Bitmap bm = new Bitmap(367, 1205);
g.DrawRectangle(new Pen(Color.Black, 0.5F), panel9.Location.X / 2, panel9.Location.Y / 2, panel9.Width, panel9.Height);
g.DrawImage(bm, 0, 0);
}
代码有什么问题?
答案 0 :(得分:5)
您必须将PrintDocument.OriginAtMargins
属性设置为true才能考虑边距。
来自MSDN,
When OriginAtMargins is true, the Graphics object location takes into account the PageSettings.Margins property value and the printable area of the page
但是从精确边缘打印取决于可打印区域,该区域由打印设备的物理限制定义。检查HardMarginX
和HardMarginY
以获取打印机的物理来源。有关更多信息,请参阅this问题的答案。