[Closed] Printing a file to a network printer?
Artur, your the man, saved my a$$.
Would love to know how to get it to work without the custom DLL as well though.
No problem, it’s a pleasure!
I’ll work on a maxscript version when I get home Either way, if you want I can compile a custom dll with just that class, memory wise
Cheers.
I always prefer to use stright up Max script versions when ever possible as I need to ensure that it will work for clients. If I can’t fix it my self it can become a problem. For the issue that I had at hand this worked great and got me over the hump. I kept trying doing it the other way but not sure what it is I’m missing.
Hmm, that isn’t working to the network printer. Thanks, I’ll poke more at it.
Hey Paul,
As promised, here’s the maxscript equivalent:
(
local printFont = dotnetobject "System.Drawing.Font" "Arial" 10
local streamReader
function pd_PrintPage sender ev =
(
local count = 0
local linesperpage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
local yPos = 0
local leftMargin = ev.MarginBounds.Left
local topMargin = ev.MarginBounds.Top
local pline = ""
local brush = dotnetobject "System.Drawing.Solidbrush" (dotnetclass "System.Drawing.Color").black
local stringformat = dotnetobject "System.Drawing.StringFormat"
while count < linesperpage do
(
pline = streamReader.ReadLine()
if pline == undefined then exit
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString pline printFont brush leftMargin yPos stringformat
count += 1
)
if pline != undefined then ev.HasMorePages = true else ev.HasMorePages = false
)
fn PrintIt filetoprint printer =
(
if printer==unsupplied then print "teste"
try
(
streamReader = dotnetobject "System.IO.Streamreader" filetoprint
local pd = dotnetobject "System.Drawing.Printing.PrintDocument"
dotnet.addeventhandler pd "PrintPage" pd_PrintPage
if printer!="" then pd.PrinterSettings.PrinterName = printer
pd.Print()
streamReader.Close()
)
catch
(
print (getCurrentException())
)
)
-- PrintIt @"c: est.txt" "" -- use default printer
PrintIt @"c: est2.txt" @"\\sharename\printer"
)
The only thing I could do, dont know why, because I’ve done it before, was having the printer parameter being optional… I’ve tried the printer:unsupplied bla bla, but no avail, well, this works anyway I guess
Cheers.
(
local printFont = dotnetobject "System.Drawing.Font" "Arial" 10
local streamReader
function pd_PrintPage sender ev =
(
local count = 0
local linesperpage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
local yPos = 0
local leftMargin = ev.MarginBounds.Left
local topMargin = ev.MarginBounds.Top
local pline = ""
local brush = dotnetobject "System.Drawing.Solidbrush" (dotnetclass "System.Drawing.Color").black
local stringformat = dotnetobject "System.Drawing.StringFormat"
while count < linesperpage do
(
pline = streamReader.ReadLine()
if pline == undefined then exit
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString pline printFont brush leftMargin yPos stringformat
count += 1
)
if pline != undefined then ev.HasMorePages = true else ev.HasMorePages = false
)
fn PrintIt filetoprint printer:"" =
(
--if printer==unsupplied then print "teste"
try
(
streamReader = dotnetobject "System.IO.Streamreader" filetoprint
local pd = dotnetobject "System.Drawing.Printing.PrintDocument"
dotnet.addeventhandler pd "PrintPage" pd_PrintPage
if printer!="" then pd.PrinterSettings.PrinterName = printer
pd.Print()
streamReader.Close()
)
catch
(
print (getCurrentException())
)
)
-- PrintIt @"c: est.txt" -- use default printer
PrintIt "c: est2.txt" printer:"\\sharename\printer"
)
NICE JOB Kameleon!!! Thanks for the snippet!
Thanks, that will go in the archives. Dennis, not sure why I couldn’t get the network printing where I was in DOS but it works at my office.
Just testing your code Artu, looks like I was getting close but I didn’t have all the setup code and I wasn’t doing all the setup in the eventHandler. What I’m not clear on is what you are having to do in there and how it ties into the .print() command in the PrintIt function. Can you shed some light on it for us?
Hey Paul,
I didn’t know how to do this till yesterday also, but I’ll try to explain what I’ve understood and read on msdn.
It seems the PrintDocument class has two ways for printing, the .PrintDialog and the .Print method, whereas the last one is to print without any user interaction, which was what we’ve wanted.
By doing so, we have to manually setup the page in the PrintPage event, which is called for every page printing in the document, this is where you set the HasMorePages = true since we havent read the streamreader to the end (count < linesperpage).
Finally we have the DrawString which actually renders out the line read from the streamreader to the page being printed.
And I guess this is it, there is also some parameters in the DrawString such as the Brush to define the color of the text and the stringformat to define alignments, orientations and such.
Cheers!
Interesting, thanks, I think that I see what is going on. Thanks for working on this and posting the results.