【问题标题】:Printing does not start at the top edge of the page打印未从页面的顶部边缘开始
【发布时间】:2014-01-30 06:01:20
【问题描述】:

我正在尝试使用Graphicss.DrawString() 打印一些strings。我已将边距设置为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);
}

代码有什么问题?

【问题讨论】:

    标签: c# .net winforms printing


    【解决方案1】:

    您必须将 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

    但从精确边缘打印取决于可打印区域,该区域由打印设备的物理限制定义。检查HardMarginXHardMarginY 以获取打印机的物理来源。更多信息请参考this问题的答案。

    【讨论】:

    • 假设我想从页面的左上角打印,代码应该是什么?我问这个是因为,我在分配margins 后将PrintDocument.OriginMargins 设置为true,但打印的比以前更低。
    • @DhavalR ,请参阅我编辑的答案。您的打印机应该支持从左上角打印。为了更清楚,请参阅我链接的问题的答案。
    • 这意味着无法在最大矩形之外打印。那么我应该怎么做才能打印贴纸(用于邀请卡)。纸张上的贴纸非常靠近顶部边缘。我正在使用激光打印机。
    • @DhavalR ,除非您的打印设备支持,否则您无法在顶部边缘打印。您可以尝试使用OriginAtMargins true 设置负边距。但很可能打印件会被剪裁。
    猜你喜欢
    • 2021-01-16
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多