Sdílet prostřednictvím


Tisk textového souboru s více stránkami

Pro aplikace založené na Windows je běžné tisknout text. Třída Graphics poskytuje metody pro kreslení objektů (grafiky nebo textu) na 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 nejsou podporovány pro tisk. Vždy byste měli použít DrawString metody Graphics, jak je znázorněno v následujícím příkladu kódu, k vykreslování textu pro účely tisku.

Tisk textu

  1. V sadě Visual Studio dvakrát klikněte na formulář, který chcete tisknout, v podokně Průzkumník řešení. Tato akce otevře Vizuální Návrhář.

  2. Z panelu nástrojů poklikejte na komponentu PrintDocument a přidejte ji do formuláře. Tato akce vytvoří komponentu PrintDocument s názvem printDocument1.

  3. Buď do formuláře přidejte Button, 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 událost Click vygenerujte obslužnou rutinu události.

  5. Kód události Click 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. Vraťte se do Click kódu obslužné rutiny události a 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. Metoda Print je zvýrazněná v následujícím kódu:

    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ě události PrintPage použijte vlastnost Graphics třídy PrintPageEventArgs a obsah 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 se vyvolává, 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, jako je použit 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é