PDA

View Full Version : Printing a file to a network printer?


PEN
10-13-2009, 05:02 PM
Any one have any pointers on this?

DosCommand?

Kameleon
10-13-2009, 05:18 PM
Have you seen this?

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

Then in the submit button you can have something like this:

printdocument.PrinterSettings.PrinterName = "\\Sharename\printer"

Dunno, never did that before. Cheers.

PEN
10-13-2009, 05:21 PM
I'll have a poke. Would be nice if it was simpler.

PEN
10-13-2009, 05:39 PM
Any one please, The dotNet solution will work if I can sort out how to send it a file to print. Right now I can print blank pages only.

Kameleon
10-13-2009, 05:42 PM
Are you using the example above? Are you creating the streamreader? Adding printPage event handler?

PEN
10-13-2009, 05:46 PM
I don't want a UI at all. Just want to be able to print a pile of documents.

PEN
10-13-2009, 05:54 PM
not sure how to create the streamreader

PEN
10-13-2009, 06:10 PM
streamReader is created but I don't know what to do with it.

Kameleon
10-13-2009, 06:14 PM
Just a sec, I've created a custom class, I'm just writing an example in Max :P

Edit:

Done.

(
dotnet.loadAssembly @"C:\KClasses.dll"

local NPrinter= dotnetobject "KClasses.dnNetworkPrintFile"
NPrinter.printit @"c:\test.txt" @"\\printer\sharename"
)


The printer name is optional, so if you omit it it will print on the default printer of the system.

Cheers.

MarcoBrunetta
10-13-2009, 06:26 PM
I don't think you can just send any document directly. You'll have to be able to read the document first. So if it's text, you can use Graphics.DrawString in the PrintPageEventArgs, and if it's an image Graphics.DrawImage.

Also, I found a small overview in: http://msdn.microsoft.com/en-us/magazine/cc188767.aspx

PEN
10-13-2009, 06:32 PM
Artur, your the man, saved my a$$.

Would love to know how to get it to work without the custom DLL as well though.

Kameleon
10-13-2009, 06:37 PM
No problem, it's a pleasure!

I'll work on a maxscript version when I get home :D Either way, if you want I can compile a custom dll with just that class, memory wise :D

Cheers.

PEN
10-13-2009, 08:02 PM
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.

denisT
10-13-2009, 09:15 PM
Any one have any pointers on this?

DosCommand?

DOS:

copy "filename.ext" "\\computername\printername (file:///computernameprintername)"

if your file is binary
copy /b "filename.ext" "\\computername\printername (file://computername/printername)"

from max (sample):
HiddenDOSCommand "copy \"c:\\tmp\\log.txt\" \"\\\\project\\printer\ (http://forums.cgsociety.org/)""

it works for me...

PEN
10-13-2009, 09:21 PM
Hmm, that isn't working to the network printer. Thanks, I'll poke more at it.

denisT
10-13-2009, 09:22 PM
Hmm, that isn't working to the network printer. Thanks, I'll poke more at it.

i have a network printer...

denisT
10-13-2009, 09:24 PM
you have to use printer's path instead of computer_name

Kameleon
10-13-2009, 11:04 PM
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:\test.txt" "" -- use default printer
PrintIt @"c:\test2.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.

denisT
10-14-2009, 12:33 AM
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:\test.txt" -- use default printer
PrintIt "c:\test2.txt" printer:"\\sharename\printer"
)



NICE JOB Kameleon!!! Thanks for the snippet!

PEN
10-14-2009, 02:21 PM
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.

PEN
10-14-2009, 02:32 PM
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?

Kameleon
10-14-2009, 03:08 PM
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!

PEN
10-14-2009, 03:30 PM
Interesting, thanks, I think that I see what is going on. Thanks for working on this and posting the results.

CGTalk Moderation
10-14-2009, 03:30 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.