dcsimg
Results 1 to 7 of 7

Thread: Print Preview using a Text Document

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    5

    Question Print Preview using a Text Document

    So I have been trying to find a method where I can do a PrintPreview on a specific Text Document before Printing using PrintPreviewControl but all the methods I found were using TableAdpters and DataGridView which I should be using but am not. If anyone knows a way that I can do this please?

    On my form I have a PrintDocument and PrintPreviewControl
    My code is very limited for this as I extremely confused and stuck.

    Here is my code:
    Code:
          
    Private Sub FrmStockPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    PrintStockDetails.DefaultPageSettings.Landscape = True
    PrintPreviewControl1.Document = PrintStock()
    
    End Sub
    
     Sub PrintStock()
    
    
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,720

    Re: Print Preview using a Text Document

    Table adapters and grids are irrelevant to printing. They are just a way to get the data to print. Once you have the data, printing it is done the same way regardless of where it came from, i.e. you call Print oh a PrintDocument, handle it's PrintPage event and then draw whatever you want printed using GDI+. You need to do just that to print text from a text for our anything else.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    5

    Re: Print Preview using a Text Document

    Oh ok so I have to actually design the Preview? So do you know a way how I can collect the data from the text document to do this please?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,720

    Re: Print Preview using a Text Document

    It's a text file. How do you usually read data from a text file? If you don't know, look it up. That's a very common task and thus easy to find information about. In fact, there's likely information out there specifically about printing text files. Depending on the size, i.e. if it might go over multiple pages, you will likely have to do some maths to determine where to put the page breaks.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    5

    Re: Print Preview using a Text Document

    Oh ok, Thanks. Yes i've been able to find information about printing text files just not Print Previewing.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,720

    Re: Print Preview using a Text Document

    It's the same thing. The PrintDocument draws using a Graphics object in its PrintPage event handler. That Graphics object is just created in a different way for a real print and a preview.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Print Preview using a Text Document

    Its your lucky day here is an example.

    Just call the preview dialog. The printout drawing is produced on the dialog preview screen. Click ok button on dialog and the same code is used to print the drawing on the printer paper.

    There are many ways to do it. This example shows basic printing in columns by using a monotype font. Also printing with word wrap. And finally printing multiple pages.

    The example makes the controls just cut and paste into an empty form.

    Name:  PrintTextBasicExample.png
Views: 228
Size:  25.1 KB

    Code:
    Imports System.Drawing.Printing
    
    Public Class PrintTextBasicExample
        Private WithEvents Button1 As New Button With {.Parent = Me, .Text = "Print...", .Location = New Point(100, 100)}
        Private WithEvents PrintDocument1 As New PrintDocument
        Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
        Private MasterData, PrintString As String
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Basic Text Printing"
    
            'make the column text data
            MasterData = "Item    Description     Quantity      Unit Price      Amount" & vbLf &
                         "_________________________________________________________________" & vbLf & vbLf
    
            For i As Integer = 1000 To 1040
                MasterData += i.ToString &
                         "    24 in RCP         2200          30              66000" & vbLf & vbLf
            Next
    
    
            'make the Word "Wrap text data
            MasterData += vbLf & vbLf
    
            For i As Integer = 1000 To 1040
                MasterData += "This is word wrap data for the printing example and contains mostly sentences."
                If i = 1010 Or i = 1020 Then MasterData += vbLf & vbLf
            Next
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                PrintString = MasterData
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.ShowDialog()
    
            Catch ex As Exception
                MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation)
            End Try
        End Sub
    
        Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            'draw the margins
            Dim rect As Rectangle = e.MarginBounds
            rect.Inflate(50, 50)
    
            e.Graphics.DrawRectangle(Pens.Black, rect)
    
            Dim charactersOnPage, linesPerPage As Integer
    
            Using f As Font = New Font("Consolas", 12)
                ' Sets the value of charactersOnPage to the number of characters  
                ' of stringToPrint that will fit within the bounds of the page.
                e.Graphics.MeasureString(PrintString, f, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
                ' Draws the string within the bounds of the page
                e.Graphics.DrawString(PrintString, f, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic)
    
            End Using
    
            ' Remove the portion of the string that has been printed.
            PrintString = PrintString.Substring(charactersOnPage)
    
            ' Check to see if more pages are to be printed.
            e.HasMorePages = PrintString.Length > 0
    
            'reload string for actual printing after preview
            If Not e.HasMorePages Then PrintString = MasterData
        End Sub
    End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width