Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Printing a text file in vb.net

Posted on 2015-01-21
13
Medium Priority
?
1,891 Views
Last Modified: 2015-01-22
I  have an application which is using bartender to print data labels and it works fine except that it won't print multi-line pallet labels and that is a requirement for the job,

From a legacy system I have a procedure which generates all of the code to do this as a text file. In the legacy system the text was written out to a serial port - which is file - but Bartender is using a windows printer.

All I need to be able to do is send my generated text to the windows printer.
Any ideas anyone?
0
Comment
Question by:grwallace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 36

Expert Comment

by:Kimputer
ID: 40562105
I'd say take a look at this:

https://msdn.microsoft.com/en-us/library/cwbe712d(v=vs.110).aspx

This uses a fixed txt file, but of course you can generate a temporary file and use that instead.
0
 

Author Comment

by:grwallace
ID: 40562307
Well it does send to the printer, but it seems to be sending more than the raw text file
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 40562310
EDIT: Sorry, seems I can't print either! I'll try to look up another solution.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:grwallace
ID: 40562333
It is a label printer, and it is in command mode
Sending it simple text "INPUT ON" through the serial port changes the mode, but doing it through the program changes the mode then gives a load of errors
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 40562473
So it's not a Windows printer?
0
 

Author Comment

by:grwallace
ID: 40562583
It has work through just a serial port with text commants or it can work as a windows printer through the USP or Ethernet ports. I am working with the Ethernet port
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 40562960
So to be clear, if you open Word (or wordpad if you don't have Office), and you type a few letters, then use Windows to print it out (selecting the network printer), you will get those few letters printed out?
0
 

Author Comment

by:grwallace
ID: 40563924
Bizarrely if I send it to the default printer I simply get a blank page!
It reads the file OK but just doesn't print anything
0
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 40563929
I know, this below code works better. But as I said, are you sure it's a Windows printer? See earlier post about Word printing. If it is, then below will work, otherwise it won't.

Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents printButton As System.Windows.Forms.Button
    Private printFont As Font
    Private streamToPrint As StreamReader


    ' The PrintPage event is raised for each page to be printed. 
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing

        ' 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()
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            count += 1
        End While

        ' If more lines exist, print another page. 
        If (line IsNot Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            streamToPrint = New StreamReader("D:\abc.txt")
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
                'pd.Print()
                MsgBox("a")
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Open in new window


Obviously, change D:\abc.txt to a real text file.
0
 

Author Comment

by:grwallace
ID: 40563966
Yes - that routine does exactly what is says on the tin - prints the text on the label - but it is not actually what I want to do, I am looking to send a raw text file to the printer to load a program into memory and then run it - which is slightly different! This routine prints the program,

I think what I need to do is find the port name and use a streamwriter to write the text directly to it in the same way as the serial port does.  
I have found another article that I am going to try now.
I am going to close the question and award the points as you did answer it correctly, it is just that I possible answered the wrong question!
Thanks for your help
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 40563985
Thanks for the points.

Can you try this command in a DOS box ?

type d:\abc.txt > COM1

Open in new window


(or change  COM1 to whatever the serial port should be, and of course, point to a real text file)
IF it works, the workaround for now (until better code is found), is to use that command (in the form of a Process).
0
 

Author Comment

by:grwallace
ID: 40564001
No problem with serial ports - I have been working with them for years
What I would need to do would be the same thing to the Ethernet port on the printer
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 40564006
Oh got it, might not be possible, because the printer's network driver is sitting inbetween you, the commands, the network cable and the printer. It probably requires the engineer who wrote the driver to get this solution!
Using serial port (and type d:\abc.txt > COM1) is talking to the serial port.
Using network, is talking to the printer's network drivers, there's no 1:1 replacement or workaround.

In the past I had some workarounds, but those only work for lpt ports. So try this:

share your network printer, even though you will use it yourself
then use this command
net use lpt1: \\ownpcname\printersharename
now use this command again:
type d:\abc.txt > lpt1
If this works, implement this command line using a Process
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question