Sdílet prostřednictvím


Tisk textového souboru s více stránkami (model Windows Forms .NET)

Pro aplikace založené na Windows je běžné tisknout text. Třída Graphics poskytuje metody pro nakreslené objekty (grafiku nebo text) do zařízení, jako je obrazovka nebo tiskárna. Následující část podrobně popisuje proces tisku textového souboru. Tato metoda nepodporuje tisk souborů bez prostého textu, jako je třeba wordový dokument Office nebo soubor PDF .

Poznámka:

Metody DrawTextTextRenderer tisku nejsou podporovány. Vždy byste měli použít DrawString metody Graphics, jak je znázorněno v následujícím příkladu kódu, kreslit text pro účely tisku.

Tisk textu

  1. V sadě Visual Studio poklikejte na formulář, ze kterého chcete tisknout, v podokně Průzkumník řešení. Tím se otevře vizuální návrhář.

  2. Z panelu nástrojů poklikejte na komponentu PrintDocument a přidejte ji do formuláře. To by mělo vytvořit komponentu PrintDocument s názvem printDocument1.

  3. Buď přidejte Button do formuláře nějaký formulář, nebo použijte tlačítko, které už je ve formuláři.

  4. V návrháři vizuálu formuláře vyberte tlačítko. V podokně Vlastnosti vyberte tlačítko Filtru událostí a poklikáním na Click událost vygenerujte obslužnou rutinu události.

  5. Kód Click události by měl být viditelný. Mimo rozsah obslužné rutiny události přidejte privátní řetězcovou proměnnou do třídy s názvem stringToPrint.

    private string stringToPrint="";
    
    'Private PrintDocument1 As New PrintDocument()
    Private stringToPrint As String
    
  6. Zpět v Click kódu obslužné rutiny události nastavte DocumentName vlastnost na název dokumentu. Tyto informace se odesílají do tiskárny. Dále si přečtěte textový obsah dokumentu a uložte ho stringToPrint do řetězce. Nakonec zavolejte metodu Print pro vyvolání PrintPage události. Níže je zvýrazněná Print metoda.

    private void button1_Click(object sender, EventArgs e)
    {
        string docName = "testPage.txt";
        string docPath = @"C:\";
        string fullPath = System.IO.Path.Combine(docPath, docName);
    
        printDocument1.DocumentName = docName;
    
        stringToPrint = System.IO.File.ReadAllText(fullPath);
        
        printDocument1.Print();
    }
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim docName As String = "testPage.txt"
        Dim docPath As String = "C:\"
        Dim fullPath As String = System.IO.Path.Combine(docPath, docName)
    
        PrintDocument1.DocumentName = docName
    
        stringToPrint = System.IO.File.ReadAllText(fullPath)
        
        PrintDocument1.Print()
    
    End Sub
    
  7. Vraťte se do vizuálního návrháře formuláře a vyberte komponentu PrintDocument . V podokně Vlastnosti vyberte filtr událostí a poklikáním na PrintPage událost vygenerujte obslužnou rutinu události.

  8. V obslužné rutině PrintPage události použijte Graphics vlastnost PrintPageEventArgs třídy a obsahu dokumentu k výpočtu délky řádků a řádků na stránku. Po vykreslení každé stránky zkontrolujte, jestli se jedná o poslední stránku, a nastavte HasMorePages vlastnost PrintPageEventArgs odpovídajícím způsobem. Událost PrintPage je vyvolána, dokud HasMorePages není false.

    V následujícím příkladu kódu se obslužná rutina události používá k tisku obsahu souboru "testPage.txt" ve stejném písmu, jaké používá ve formuláři.

    private void PrintDocument1_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, 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);
    }
    
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object,
    ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 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, Me.Font, e.MarginBounds.Size,
        StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
        ' Draws the string within the bounds of the page
        e.Graphics.DrawString(stringToPrint, Me.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
    
    End Sub
    

Viz také