Click here to Skip to main content
Click here to Skip to main content

universal printing without office

By , 14 May 2008
 

Introduction

Printing solution that is reliable, easy to mantain, no extra libary's needed. Just download the free microsoft word viewer. Make some XML templates and let the code do the job for you.

Background

I search a long time to get a good printing solution that can fill templates an has no need for drivers or references.

First of all we need a word 2003 (or 2007) xml file that whe use as template source. Because this file is not zipped like docx or open source alternatif odt, whe can read it as a text file and replace without any problem. In the XML template we put the 'TEXTFIELDS' for replacement. In this example i use the markers #...# as replacement field.

First whe read the source, do the replacements and the write the temp printfile. So we always keep the original template.

        Dim BRONDOCUMENT As String = "test.xml"  ' Path to then source file
        TEMPDOCUMENT = "TEMP.xml" 'this will be the output file with the changes
        Dim fileContents As String
        fileContents = My.Computer.FileSystem.ReadAllText(BRONDOCUMENT)
        ' WORD xml is not zipped so whe can copy an replace everthing without any problem
        ' I used a external textfile with the data to change. In this example whe do it inhere.
        fileContents = fileContents.Replace("#NAAM#", "DE CAT BEN")
        fileContents = fileContents.Replace("#ADRESSE#", "A STREET IN BELGIUM")
        fileContents = fileContents.Replace("#URL#", "<a href="%22http://www.hetnetop.be/%22">WWW.HETNETOP.BE</a>")
        fileContents = fileContents.Replace("#CITY#", "KORTRIJK")
        'etc....
        My.Computer.FileSystem.WriteAllText(TEMPDOCUMENT, fileContents, False)
        

Then all whe have to do is to make some buttons, PRINT, VIEW/EDIT and CANCEL.

        'PRINT 
        'be sure that xml is assosiated with MS word or wordview
        Process.Start(TEMPDOCUMENT) 'you can at some minimized window things here
        'STARTS PRINTING THE WORD VIEW WITH SENDKEYS 
        My.Computer.Keyboard.SendKeys("^p", True)
        My.Computer.Keyboard.SendKeys("~", True)
        'CLOSE THE WORDVIEUW PROGRAM
        For Each proc As Process In Process.GetProcesses()
            If InStr(proc.MainWindowTitle, "TEMP") > 0 Then proc.CloseMainWindow()
        Next

The VIEW/EDIT button is just the cal 'PROCESS.START(TEMPDOCUMENT)' so whe get the wordviewer in all his glory.

And guess, CANCEL just ends the program... Really simple ! but very fast and reliable in practice !

With this concept you can make a universal windows printer engine, with input the source template XML file and one with fill DATA, so you can call this from other programs, without all the problems that printing can give you...

PS: the test file TEST.XML is in the bind\debug dir.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

delcatto
Software Developer delcatto
Belgium Belgium
Member
Ben DE CAT, works in Belgium or online as freelance. Native language is Dutch so the english is a bit wacky ?!
Makes software for the Belgium gouverment since 1998. Started with Cobol, delphi an visual basic 5.00 and now uses VB2008.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralshellexecutememberMember 63246915 May '08 - 5:12 
it's better to use shellexecute rather senkeys
 
Dim p As New System.Diagnostics.Process()
 
p.StartInfo.FileName = "<xml file>"
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
p.StartInfo.Arguments = arguments
p.StartInfo.Verb = "Print"
p.StartInfo.CreateNoWindow = False
p.StartInfo.UseShellExecute = True
 
p.EnableRaisingEvents = False
p.Start()

GeneralRe: shellexecutememberdelcatto15 May '08 - 6:33 
I tried that, but that will not work with the wordviewer, it dont accept any command line or verb's.
So its impossible to do something with the windowstyle or print command.
The sendkeys does however the job well... But you have the annoying windowscreen popup for a moment.
Still its a improvment to close the application, the right way ' p.CloseMainWindow()'

Dim p As New System.Diagnostics.Process()
p.StartInfo.FileName = TEMPDOCUMENT
' THIS COMMANDS WILL NOT WORK WITH WORDVIEUW 2003
' p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
' p.StartInfo.Verb = "print"
' p.StartInfo.CreateNoWindow = False
'************************************************************
p.StartInfo.UseShellExecute = True
p.EnableRaisingEvents = False
p.Start()
System.Threading.Thread.Sleep(100) 'give it some time
My.Computer.Keyboard.SendKeys("^p", True)
My.Computer.Keyboard.SendKeys("~", True)
System.Threading.Thread.Sleep(300) 'give it some time
p.CloseMainWindow()

GeneralTemplate as Embedded Resource [modified]memberIlíon14 May '08 - 17:34 
If you make the template document an embedded resource of your program or project (in contrast to a separate file on disk) there is no chance of it getting lost or overwritten. Of course, this does somewhat limit flexibility, as the module would have to be recompiled and redistributed to reflect any changes to the template document.
 

Also, am I not understanding something? (Or, perhaps I'm missing something) ... When I run the demo program, I get an error: "Could not find a part of the path 'F:\rtfword\DOC\101 eerste herinnering.xml'"
 
modified on Thursday, May 15, 2008 12:01 AM

AnswerRe: Template as Embedded Resourcememberdelcatto15 May '08 - 2:10 
Indeed you can do this, for my part its easyier to distribute changes with a external file.
I included a test.xml in the bin/debug. That will solve the problem.
JokeThis is really KISSmemberdelcatto14 May '08 - 12:47 
so stupid simple, Laugh | :laugh: e that it realy works !

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 14 May 2008
Article Copyright 2008 by delcatto
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid