-1

我如何在 WPF 中使用以下代码?

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Name", new Font("tahoma", 10), Brushes.Black, 100, 100);
        e.Graphics.DrawString("Last", new Font("tahoma", 10), Brushes.Black, 100, 120);
    }
4

1 回答 1

0

由于要点,我还不能发表评论,但是您显示的是添加到事件处理程序的方法,这是我为将文本打印到文档中所做的示例:

向事件处理程序添加方法:

 printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage);

事件处理程序方法(您发布的案例):

 void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        int charactersOnPage = 0;
        int linesPerPage = 0;

        // Sets the value of charactersOnPage to the number of characters  
        // of stringToPrint that will fit within the bounds of the page.
        e.Graphics.MeasureString(stringToPrint, printFont,
            e.MarginBounds.Size, System.Drawing.StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);

        // Draws the string within the bounds of the page
        e.Graphics.DrawString(stringToPrint, printFont, System.Drawing.Brushes.Black,
            e.MarginBounds, System.Drawing.StringFormat.GenericTypographic);

        // Remove the portion of the string that has been printed.
        stringToPrint = stringToPrint.Substring(charactersOnPage);

        // Check to see if more pages are to be printed.
        e.HasMorePages = (stringToPrint.Length > 0);
    }

关闭打印:

 printDocument.Print();
于 2013-01-22T09:04:04.790 回答