1

我正在玩 PrintDocument 和 PrintPreviewDialog 类来学习它们,但我似乎无法让文档在发送到打印机时打印任何内容,而且在 PrintPreviewDialog 中也没有任何内容,甚至没有白色的空白页,它是全是灰色的。我尝试过更改边距、绘制位置、绘制巨型矩形等,但没有任何显示。我对 GDI+ 不是很熟悉,所以我确信我遗漏了一些东西,但我找不到什么。

这是我正在使用的相关代码:

//Print
private void menuPrint_click(Object source, EventArgs e)
{
    printDocumentPage = 0;

    printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    printDocument1.BeginPrint += new System.Drawing.Printing.PrintEventHandler(printDocument1_BeginPrint);

    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.ShowDialog();
}

private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    // Save our print action so we know if we are printing 
    // a preview or a real document.
    printAction = e.PrintAction;

    // Set some preferences, our method should print a box with any 
    // combination of these properties being true/false.
    printDocument1.OriginAtMargins = false;

    if (printAction != PrintAction.PrintToPreview)
    {
        PrintDialog printDlg = new PrintDialog();
        printDocument1.DocumentName = "Print Document Simple Text";
        printDlg.Document = printDocument1;

        // Show printer dialog
        if (printDlg.ShowDialog() == DialogResult.OK)
        {
            printDocument1.PrinterSettings = printDlg.PrinterSettings;
        }
        else
        {
            e.Cancel = true;
        }
    }
}

void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Graphics g = e.Graphics;

    RectangleF marginBounds = e.MarginBounds;

    RectangleF printableArea = e.PageSettings.PrintableArea;

    //if (printAction == PrintAction.PrintToPreview)
        //g.TranslateTransform(printableArea.X, printableArea.Y);

    int availableWidth = (int)Math.Floor(printDocument1.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width));
    int availableHeight = (int)Math.Floor(printDocument1.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height));

    //g.DrawRectangle(Pens.Red, 0, 0, availableWidth - 1, availableHeight - 1);

    // Doesnt work either
    //g.DrawRectangle(Pens.Red, 0, 0, 100, 100);

    System.Drawing.Font myFont = new System.Drawing.Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Pixel);

    float lineHeight = myFont.GetHeight(g);
    float yLineTop = e.MarginBounds.Top;

    if (printDocumentPage >= m_report.PageCount)
    {
        e.HasMorePages = false;
        return;
    }

    otPage page = m_report.pageAt((int)printDocumentPage);

    for (int i = 0; i < page.LineCount ; i++)
    {
        otLine line = page.lineAt(i);

        g.DrawString(line.getPrintLine(true), myFont, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);

        yLineTop += lineHeight;
    }

    printDocumentPage++;

    if (printDocumentPage < m_report.PageCount)
    {
        e.HasMorePages = true;
        return;
    }

    e.HasMorePages = false;
}

PrintPreviewDialog 出现,两个事件处理程序都运行,它正确循环并绘制每一行,但对话框上没有绘制任何内容,当发送到打印机时,它打印空页。PrintPreviewDialog 都是灰色的,没有白页或任何东西,并且在所有级别上缩放什么都不做。

编辑: 摆脱我当前的页面/行处理,只是尝试绘制单个字符串、任何字符串或矩形,什么都不起作用。

这是打印预览屏幕的屏幕截图。更改缩放/页面也无济于事:在此处输入图像描述

编辑2:

我从http://www.c-sharpcorner.com/uploadfile/mahesh/printpreviewcontrol-in-c-sharp/下载了这个非常简单的示例项目,我认为它可以按预期工作。开箱即用,这也是同样的事情。仅显示灰色屏幕(以及表单上的按钮)。printpreviewcontrol 中根本没有打印任何内容。

可能是什么问题呢?

编辑3:

我在另一台开发机器上测试了它,它工作正常。为什么它不能在我的电脑上运行?另一台计算机是 64 位的,而这是 32 位的,但使用相同的设置构建。会不会是我的 .NET 框架搞砸了?

编辑4:

很抱歉所有的编辑。我发现如果我切换我的默认打印机,它可以完美地工作。与 e.MarginBounds 有关。

4

0 回答 0