Как я могу увидеть текст, представленный в файле с помощью предварительного просмотра - PullRequest
0 голосов
/ 04 июля 2011

Я разрабатываю приложение для print preview файлов, которые пользователь любит печатать, я пишу следующий код, но я не могу увидеть текст или контент на странице предварительного просмотра, почему это происходит, может ли кто-нибудь мне помочь

Мой код выглядит следующим образом

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
    private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
    private PrintDocument printDocument1 = new PrintDocument();

    // Declare a string to hold the entire document contents.
    private string documentContents;

    // Declare a variable to hold the portion of the document that
    // is not printed.
    private string stringToPrint;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void ReadDocument()
    {

        string docName = "ACH.txt";
        string docPath = @"c:\";
        printDocument1.DocumentName = docName;
        using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
        using (StreamReader reader = new StreamReader(stream))
        {
            documentContents = reader.ReadToEnd();
        }
        stringToPrint = documentContents;
    }

    private void printToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ReadDocument();
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }
    void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        int charactersOnPage = 5;
        int linesPerPage = 10;

        // 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);

        // If there are no more pages, reset the string to be printed.
        if (!e.HasMorePages)
            stringToPrint = documentContents;
    }
}

}

1 Ответ

1 голос
/ 04 июля 2011

Вы не подключили свои события печати. Добавьте это к вашему конструктору формы:

public Form1()
{
  InitializeComponent();
  printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...