打印不是从页面的顶部开始 [英] Printing does not start at the top edge of the page

查看:43
本文介绍了打印不是从页面的顶部开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Graphicss.DrawString()打印一些 string .我已经为 printdocument 设置了页边距,但是它不是从页面的原点开始的.我已经将 margins 设置为(0,0,0,0),但是不知何故,它在页面顶部边缘下方打印了半厘米.另一件事是它可以从左边缘打印.

下面是我的代码.

  private void button1_Click(对象发送者,EventArgs e){/////PaperSize pkCustomSize1 = new PaperSize("First custom size",1020,3517);////printDocument1.DefaultPageSettings.PaperSize = pkCustomSize1;printPreviewDialog1.Document = printDocument1;printDocument1.PrinterSettings.PrinterName = this.comboBox1.Text;保证金保证金=新保证金(0,0,0,0);printDocument1.PrinterSettings.DefaultPageSettings.Margins =边距;printPreviewDialog1.Show();printDocument1.Print();}私有void printDocument1_PrintPage(对象发送者,System.Drawing.Printing.PrintPageEventArgs e){int resX = GetPrinterResolutionX(comboBox1);int resY = PrnOpra.GetPrinterResolutionY(comboBox1);图形g = e.Graphics;浮动比例= resX/ScrnRes;位图bm =新位图(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);} 

代码出了什么问题?

解决方案

您必须将 PrintDocument.OriginAtMargins 属性设置为true才能考虑边距.

来自 MSDN

当OriginAtMargins为true时,Graphics对象的位置将考虑PageSettings.Margins属性值和页面的可打印区域

但是从确切的边缘进行打印取决于可打印区域,该区域由打印设备的物理限制来定义.检查 HardMarginX HardMarginY 以获取打印机的物理来源.有关更多信息,请参见问题的答案.>

I am trying to print some strings using Graphicss.DrawString(). I have set margins to the printdocument but does not start from the origin of the page. I have set margins to (0,0,0,0) but somehow it prints half centimeter below the top edge of the page. Another thing is that it can print from left edge.

Below is my code.

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);
}

What's wrong with code?

解决方案

You have to set the PrintDocument.OriginAtMargins property to true to consider your margins.

From MSDN,

When OriginAtMargins is true, the Graphics object location takes into account the PageSettings.Margins property value and the printable area of the page

But printing from the exact edge depends on the printable area which is defined by the physical limitations of the printing device. Check the HardMarginX and HardMarginY to get the physical origin of the printer. For more information refer the answer of this question.

这篇关于打印不是从页面的顶部开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆