2

Web アプリケーションから領収書を印刷する必要があるという要件があります。データを文字列で取得し、一時テキスト ファイルを生成し、それをプリンターに送信してファイルを削除する方法を見つけました。印刷部分以外はすべて良好です。何も生成されません。白いドキュメントです。一時ファイルを印刷用に送信する前に確認したところ、そこにデータがありました。ファイルを間違った方法で送信したか、ファイルを追加するのを忘れていた可能性がありますPrintPageEventHandler(この最後のファイルについてよくわからないか、単にわからない)。

これは私がやったことです:

private void printTextfile(String strFileName)
{
    //there is this standar that makes programmers declare variables at the 
    //beginning of the method and this "obj" and "str" things...
    PrintDocument objPrintDocument =null;
    String strPrinterName = string.Empty;
    //getting the printer name from web.config
    strPrinterName= ConfigurationManager.AppSettings["NOMBRE_IMPRESORA"];
    objPrintDocument = new PrintDocument();
    objPrintDocument.PrinterSettings.PrinterName = strPrinterName;
    //the temp text file created and with data
    objPrintDocument.DocumentName = strFileName;
    //I guess don't need this because I've setted up the file name (it is reachable)
    //objPrintDocument.PrintPage += new PrintPageEventHandler(this.printTextFileHandler);
    //send the file to the printer (this works)
    objPrintDocument.Print();
    //ok, now I've check my physic file and it has nothing in it!
}

別の方法があれば教えてください。印刷されたデータを確認するだけです。注: 私は白い前景色などを使用していません。プリンターは 1 MB のテキストを受け取り、1 ページを何も含まずに印刷することができます。

4

2 に答える 2

3

データをプリンターに出力する印刷ページ イベント ハンドラーを追加する必要があります。がデータを含むファイル名の場合
strFileName

  • 上記のコードでファイルを開き、ストリームリーダーをグローバル インスタンス変数に保存します。
  • ページ イベント ハンドラーを追加する
  • 出口でストリームを閉じる

MSDN のこの例はぴったりだと思います

DocumentNameプロパティに関する MSDN のドキュメントを見ると、このステートメントが見つかります。

The DocumentName property does not specify the file to print.  
Rather, you specify the output to print by handling the PrintPage event.  
For an example, see the PrintDocument class overview.
于 2012-07-23T19:43:08.443 に答える
1

これを試して:

 private void button1_Click(object sender, EventArgs e)
        {
            System.IO.StreamReader filetoprint;
              filetoprint = new System.IO.StreamReader(@"D:\\m.txt");
              printDocument1.Print();
              filetoprint.Close();
        }
于 2012-11-29T16:58:36.543 に答える