1

複数ページの印刷に関する MSDN の「ハウツー」ドキュメントに従っています:方法: Windows フォームで複数ページのテキスト ファイルを印刷する

そのページの例をプロジェクトに変えました (Visual Studio 2010 および 2012 を試しました)。少量のページを印刷する場合は期待どおりに動作することがわかりましたが、大量 (9 ページ程度) を印刷するとレンダリングが開始されます。最初のページは空白 (1 ページ目と 2 ページ目は空白、次の 15 ページは正しいなど)。

誰でもこの動作を確認できますか? 何がこれを引き起こしているのかわかりません。例外はスローされません。

編集: 2 つの反対票を受け取りましたが、理由がわかりません。もっと明確にしようと思います。問題が含まれていると思われるコードのセクションは次のとおりです。

private void printDocument1_PrintPage(object sender, 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, this.Font,
        e.MarginBounds.Size, StringFormat.GenericTypographic,
        out charactersOnPage, out linesPerPage);

    // Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
        e.MarginBounds, 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);
}

データ型がオーバーフローしているとは思いません。いろいろなプリンターで試してみましたが、結果は同じでした。反対票を投じた場合は、コメントを残して、質問に問題がある理由をお知らせください。注: .NET Framework 4 と 4.5 を試しましたが、同じ結果が得られました。

4

1 に答える 1

1

MSDN の例が正常に機能していないようです。これは、MarginBounds Rectangle が float ベースではなく整数ベースであることが原因である可能性があります。Y 位置を float として追跡し、この値を MeasureString および DrawString メソッド内で使用すると、問題が解決します。これは、 MSDN の別の印刷例を調べることでわかりました。

関連するコードは次のとおりです。

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    float linesPerPage = 0;
    float yPos = 0;
    int count = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null;

    // Calculate the number of lines per page.
    linesPerPage = ev.MarginBounds.Height /
       printFont.GetHeight(ev.Graphics);

    // Print each line of the file.
    while (count < linesPerPage &&
       ((line = streamToPrint.ReadLine()) != null))
    {
        yPos = topMargin + (count *
           printFont.GetHeight(ev.Graphics));
        ev.Graphics.DrawString(line, printFont, Brushes.Black,
           leftMargin, yPos, new StringFormat());
        count++;
    }

    // If more lines exist, print another page.
    if (line != null)
        ev.HasMorePages = true;
    else
        ev.HasMorePages = false;
}

これは、前の例のようなワード ラッピングを考慮していませんが、比較的簡単に実装できます。

于 2013-02-10T09:44:15.983 に答える