Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / January 2008

Tip: Looking for answers? Try searching our database.

Automatic Print to File VB.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kraeven - 17 Jan 2008 13:25 GMT
Hi guys,

I have been looking for a few days, but no one seems to know how to print to
a file with automatic file renaming.

I made an application in VB.NET 2005 that prints pdf visio and project files.
Select the files or folders, select a printer and the applications are
executed with the correct print command.

We have a Generic PostScript Printer, which prints to a ps file in a certain
folder.
This folder is watched by a service that converts this ps file to a pdf file.

But when printing to the Generic PostScript Printer, a dialog box pops up in
which you need to specify the path and filename. To avoid this box from
popping up and changing the filename from within the code, after digging
around I managed to accomplish this.

Imports System.Runtime.InteropServices
Imports System.ServiceProcess

Public Function PrintToFile(ByVal filename As String) As Boolean
       filename = Microsoft.VisualBasic.Right(filename, filename.Length -
InStrRev(filename, "\"))
       filename = Microsoft.VisualBasic.Left(filename, InStrRev(filename, ".
"))
       filename = PSin + "\" + filename + "ps" 'PSin is a variable (L:\in)
       PostScriptPort = filename
   End Function

Public Function SetFileToPrintPort(ByVal filename As String) As Boolean
       Dim regkey As Microsoft.Win32.RegistryKey
       regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\
Microsoft\Windows NT\CurrentVersion\Ports", True)
       regkey.SetValue(filename, "", Microsoft.Win32.RegistryValueKind.
String)
       regkey.Close()
       myController = New ServiceController("Spooler")
       Dim sStatus As String
       myController.Refresh()
       sStatus = myController.Status.ToString
       
       If myController.CanStop Then
           myController.Stop()
       Else
           MsgBox("Spooler service cannot be stopped")
       End If

       Try
           myController.Start()
       Catch exp As Exception
           MsgBox("Could not start spooler service")
       End Try
       myController.Refresh()

       myController.WaitForStatus(ServiceControllerStatus.Running)

       Shell("rundll32.exe printui.dll,PrintUIEntry /Xs /n " + Chr(34) +
"Generic PostScript Printer" + Chr(34) + " PortName " + Chr(34) + filename +
Chr(34), AppWinStyle.Hide, True)

   End Function

   Public Function RestoreGeneric(ByVal filename As String) As Boolean
       Dim rk As Microsoft.Win32.RegistryKey

       Shell("rundll32.exe printui.dll,PrintUIEntry /Xs /n " + Chr(34) +
"Generic PostScript Printer" + Chr(34) + " PortName " + Chr(34) + "FILE:" +
Chr(34), AppWinStyle.Hide, True)

       rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\
Microsoft\Windows NT\CurrentVersion\Ports", True)
       rk.DeleteValue(filename, True)
       rk.Close()
   End Function

Private Sub Startup()
Dim item As String
Dim GenPr As Boolean = False
For Each item In ListbVisio.SelectedItems
If CbPrinters.SelectedItem = "Generic PostScript Printer" Then
           GenPr = True
End If

  If GenPr Then
                       PrintToFile(item)
                       SetFileToPrintPort(PostScriptPort)
                   End If
                   
                   Shell(VisioAppPath + " /pt " + Chr(34) + item + Chr(34) +
"," + printer, AppWinStyle.Hide, True) 'MS Visio commandline Print commando  

                   If GenPr Then
                       RestoreGeneric(PostScriptPort)
                   End If
Next
End Sub
kraeven - 17 Jan 2008 14:04 GMT
Left out something

Dim myController As ServiceController (in the SetFileToPrintPort function)
srry ;-)

>Hi guys,
>
[quoted text clipped - 93 lines]
>Next
>End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.