By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

How to print label on network shared printer in ASP.Net

P: n/a
Hi,

I am development a project using C#.Net. Inside application, it needs to
print labels on different Zebra label printers on the network. I used a shell
script, but it only worked on the server machine locally. Anybody knows how
to do it?

Thanks,
Peng
Mar 9 '06 #1
Share this Question
Share on Google+
56 Replies


P: n/a
Hi
I've just done exactly this.....
I went down a few dead ends before getting there....

I initially formatted my Zebra label in ZPL in a txt file, and used a CMD
line TYPE label.txt > \\computername\printershare

The label printed fine. However, I then started to code this using interop
making calls to the Win32 API to direct print.

The prints did not happen, and I was baffled. I wasn't sure why it wasn't
printing, so I then had my code shell out to a CMD line, and run the same
TYPE redirect to the printer.
This didn't work!! It worked from my own command line, but not when the code
shelled it....

Its a permission problem. The ASPNET account doesn't have permissions to the
printer in a corporate network environment.
To get round this I added the following line to web.config
<Identity Impersonate = true />

This makes the requests on the server run in the security context of the web
requestor (ie. a valid user, and not the ASPNET account)

I expected this to work..... but it did not..... :-((

The problem was that I was still shelling out to execute the TYPE, and when
you do this, the account runs under ASPNET again!!!
I re-instated my direct print code in windows, and it worked straight away!!!

To summise:
- add <identity impersonate = true /> to your web.config file
- make an interop call back to the Win32 API to execute direct printing to a
printer share.

I have some sample code if you would like it.

Good luck.. HTH
Paul
MCSD, MCDBA
"peng" wrote:
Hi,

I am development a project using C#.Net. Inside application, it needs to
print labels on different Zebra label printers on the network. I used a shell
script, but it only worked on the server machine locally. Anybody knows how
to do it?

Thanks,
Peng

Mar 9 '06 #2

P: n/a

Paul provided me with the sample code and I applied it to my application. It
worked beautifully!! Thanks a lot, Paul!

Peng

"Paul Aspinall" wrote:
Hi
I've just done exactly this.....
I went down a few dead ends before getting there....

I initially formatted my Zebra label in ZPL in a txt file, and used a CMD
line TYPE label.txt > \\computername\printershare

The label printed fine. However, I then started to code this using interop
making calls to the Win32 API to direct print.

The prints did not happen, and I was baffled. I wasn't sure why it wasn't
printing, so I then had my code shell out to a CMD line, and run the same
TYPE redirect to the printer.
This didn't work!! It worked from my own command line, but not when the code
shelled it....

Its a permission problem. The ASPNET account doesn't have permissions to the
printer in a corporate network environment.
To get round this I added the following line to web.config
<Identity Impersonate = true />

This makes the requests on the server run in the security context of the web
requestor (ie. a valid user, and not the ASPNET account)

I expected this to work..... but it did not..... :-((

The problem was that I was still shelling out to execute the TYPE, and when
you do this, the account runs under ASPNET again!!!
I re-instated my direct print code in windows, and it worked straight away!!!

To summise:
- add <identity impersonate = true /> to your web.config file
- make an interop call back to the Win32 API to execute direct printing to a
printer share.

I have some sample code if you would like it.

Good luck.. HTH
Paul
MCSD, MCDBA
"peng" wrote:
Hi,

I am development a project using C#.Net. Inside application, it needs to
print labels on different Zebra label printers on the network. I used a shell
script, but it only worked on the server machine locally. Anybody knows how
to do it?

Thanks,
Peng

Mar 10 '06 #3

P: n/a
Hi Paul,

May I have your code please? I didn't understand what you mean with "make an
interop call back to the Win32 API to execute direct printing to a printer
share"

I'm experiencing the same problem when trying to print to a shared printer
from an ASP.net application. impersonate=true doesn't help alone... Thanks in
advance. I need your solution immediately...

Yours sincerely,

Kayıhan Onuk

"Paul Aspinall" wrote:
Hi
I've just done exactly this.....
I went down a few dead ends before getting there....

I initially formatted my Zebra label in ZPL in a txt file, and used a CMD
line TYPE label.txt > \\computername\printershare

The label printed fine. However, I then started to code this using interop
making calls to the Win32 API to direct print.

The prints did not happen, and I was baffled. I wasn't sure why it wasn't
printing, so I then had my code shell out to a CMD line, and run the same
TYPE redirect to the printer.
This didn't work!! It worked from my own command line, but not when the code
shelled it....

Its a permission problem. The ASPNET account doesn't have permissions to the
printer in a corporate network environment.
To get round this I added the following line to web.config
<Identity Impersonate = true />

This makes the requests on the server run in the security context of the web
requestor (ie. a valid user, and not the ASPNET account)

I expected this to work..... but it did not..... :-((

The problem was that I was still shelling out to execute the TYPE, and when
you do this, the account runs under ASPNET again!!!
I re-instated my direct print code in windows, and it worked straight away!!!

To summise:
- add <identity impersonate = true /> to your web.config file
- make an interop call back to the Win32 API to execute direct printing to a
printer share.

I have some sample code if you would like it.

Good luck.. HTH
Paul
MCSD, MCDBA
"peng" wrote:
Hi,

I am development a project using C#.Net. Inside application, it needs to
print labels on different Zebra label printers on the network. I used a shell
script, but it only worked on the server machine locally. Anybody knows how
to do it?

Thanks,
Peng

Apr 13 '06 #4

P: n/a

Paul, could you send me one copy of that sample code? I just began to
learn C#, but my boss want me to write zpl program......I searched for
long time, and still no idea with this issue......So , could you give
me a hand?

--
Kain
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

May 22 '06 #5

P: n/a

Hi Paul. Would you please send me a sample of your code? I would
really appreciate it.

--
ctwilliams
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

May 25 '06 #6

P: n/a

Paul,
help me please,send me your code,i'm stuck on how to print label to
zebra printer form asp.net!
thank you

ctwilliams wrote:
*Hi Paul. Would you please send me a sample of your code? I would
really appreciate it. *


--
caro
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Jun 4 '06 #7

P: n/a

Hi Paul,

Would you please send me a sample of your code? I would really
appreciate it.

Thanks,
Robert Z.
MCSD

--
rzey
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Jun 24 '06 #8

P: n/a

Hi Paul,

Could you also send me a sample of your code please? I would really
appreciate it.

--
khtee
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Jun 25 '06 #9

P: n/a

Paul,

Please send me the sample code as well.

Thanks.
Greg
*** Sent via Developersdex http://www.developersdex.com ***
Jul 10 '06 #10

P: n/a
Hello Peng,

Do you still have the sample code? Can you please send me the sample
code.

Thanks,
Hoang-

*** Sent via Developersdex http://www.developersdex.com ***
Jul 18 '06 #11

P: n/a
Hello Paul,

Can you please send me the sample code.

Thanks.
Hoang-

*** Sent via Developersdex http://www.developersdex.com ***
Jul 18 '06 #12

P: n/a

Hi Paul,

I have the same issue. Could you send me a sample of your code please? I
would really appreciate it.

Thanks in advance,
Wendy
*** Sent via Developersdex http://www.developersdex.com ***
Jul 31 '06 #13

P: n/a

Hi Paul. Would you please send me a sample of your code? I would really
appreciate it.

Egarcia

*** Sent via Developersdex http://www.developersdex.com ***
Aug 10 '06 #14

P: n/a
Hi Paul,
I am using .net to fire a print to zebra printer.The label is simple
one, no bar codes involved, so i just created string with new line
characters '\r\n' after every field. the output format is correct on
local network laserjet printer.But when i replace this printer path with
that os zebra printer, unable to get the output, rather a blank print
label.I am using the code recommended by msdn
http://support.microsoft.com/?kbid=322091.Can u please share your code
and advice me on this issue.
Thanks
Nitin

*** Sent via Developersdex http://www.developersdex.com ***
Aug 16 '06 #15

P: n/a


Hi Paul,
I am using .net to fire a print to zebra printer.The label is simple
one, no bar codes involved, so i just created string with new line
characters '\r\n' after every field. the output format is correct on
local network laserjet printer.But when i replace this printer path with
that os zebra printer, unable to get the output, rather a blank print
label.I am using the code recommended by msdn
http://support.microsoft.com/?kbid=322091.can u please send your code
and advice me on this issue.
Thanks
Nitin

*** Sent via Developersdex http://www.developersdex.com ***
Aug 16 '06 #16

P: n/a
Hi Paul,

I am also facing the same problem.... May I ask you to also send me you
your code sample.

Thanks a lot in advance

Christophe

*** Sent via Developersdex http://www.developersdex.com ***
Aug 17 '06 #17

P: n/a

Could you please send me your code, I would really appreciate.

Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Aug 23 '06 #18

P: n/a


Hi Paul!

My boss just told me to do a project to print label, but I am not
sure I can do it. Would you please send me a sample of your code? I
would really appreciate it.
Peter.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 26 '07 #19

P: n/a
Please send me the code you have for this sample. Thanks in advance.

*** Sent via Developersdex http://www.developersdex.com ***
Feb 10 '07 #20

P: n/a
Dear Paul

Could you please send me your code, I would really appreciate.
pa*********@hotmail.com

Thanks
From http://www.developmentnow.com/g/8_20...in-ASP-Net.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Feb 28 '07 #21

P: n/a


Hey,

I am trying to print to my Zebra printer. Can you please forward me the
code.

Thank you,
-Tom

*** Sent via Developersdex http://www.developersdex.com ***
Apr 13 '07 #22

P: n/a
can you please send me this if you have the code, thanks:

Rick

*** Sent via Developersdex http://www.developersdex.com ***
Apr 20 '07 #23

P: n/a
Hi Paul,

can u pls send barcode printg from asp.net saple.

Thanks
siva

*** Sent via Developersdex http://www.developersdex.com ***
Apr 23 '07 #24

P: n/a

Hi Peng,

can u send sample code to me.

Thanks and Regards,
Siva

*** Sent via Developersdex http://www.developersdex.com ***
Apr 23 '07 #25

P: n/a


Hi Please send the sample code

Regards
Adhi.D

*** Sent via Developersdex http://www.developersdex.com ***
Jun 12 '07 #26

P: n/a


Paul,

I see that his is an old thread. If possible could you forward your
print code?

Regards,
Tom

*** Sent via Developersdex http://www.developersdex.com ***
Jun 15 '07 #27

P: n/a
Could you please send me the code? I am having problems setting the
permissions so that I can print to my Zebra Printer from a asp website.
jo**@encoelectronics.com

*** Sent via Developersdex http://www.developersdex.com ***
Jun 26 '07 #28

P: n/a
Paul:

Could you please send me the code? We are working though some of the
same issues. ej*******@ch.blommer.com

*** Sent via Developersdex http://www.developersdex.com ***
Jun 26 '07 #29

P: n/a


Hi Paul,
Can you please send me the code?

*** Sent via Developersdex http://www.developersdex.com ***
Jul 4 '07 #30

P: n/a
Paul,

Could you please send the sample code to perform printing?
Regards,
Narahari

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '07 #31

P: n/a


Hi Paul,

Can you please send me the sample code.

Thanks,
Raj Gopal.

*** Sent via Developersdex http://www.developersdex.com ***
Aug 1 '07 #32

P: n/a


I've written an Inventory application and users would like labels
printed after they have added the items to the database. This is done
by filling out a form and then pressing submit. Upon submission I would
like to have a label printed.

I'm using a Zebra printer, connected to the users local machine, while
the application resides on a server and I use the barcodenet dll to
create the barcode to place on labels.

I have successfully been able to print labels as long as I'm in the
application on the server and the printer is located there, but this is
not the scenario that I need to implement. The printers will be located
far away from the server so I need to be able to send the print to the
local label printer.

Does anyone have any ideas how I can access the local printers from
within ASP.NET?

*** Sent via Developersdex http://www.developersdex.com ***
Sep 19 '07 #33

P: n/a
"Joanna Bar" <jo*****@hotmail.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
I've written an Inventory application and users would like labels
printed after they have added the items to the database. This is done
by filling out a form and then pressing submit. Upon submission I would
like to have a label printed.

I'm using a Zebra printer, connected to the users local machine, while
the application resides on a server and I use the barcodenet dll to
create the barcode to place on labels.

I have successfully been able to print labels as long as I'm in the
application on the server and the printer is located there, but this is
not the scenario that I need to implement. The printers will be located
far away from the server so I need to be able to send the print to the
local label printer.

Does anyone have any ideas how I can access the local printers from
within ASP.NET?

You can't natively... Modern browsers are designed specifically to prevent
any sort of direct interaction with the hardware or software in the machine
on which they are running, for fairly obvious security reasons...

Does each user absolutely have to have their own Zebra printer?

If not, I would advise you to set up a dedicated print machine with the
printer attached, and write a Windows service which polls your database
server for labels to print...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 19 '07 #34

P: n/a
Would you send me your sample code?

Thanks!

*** Sent via Developersdex http://www.developersdex.com ***
Sep 25 '07 #35

P: n/a


Hi Paul,
I'm doing the exact same thing. Was wondering if you would send me the
code also.
thanks

*** Sent via Developersdex http://www.developersdex.com ***
Oct 17 '07 #36

P: n/a
If anyone there is still listening, can you please send me the code to
be able to do this !! I am desperate.

Thanks in advance.

Jenny

*** Sent via Developersdex http://www.developersdex.com ***
Nov 14 '07 #37

P: n/a

Hi Paul,

Can you send me your sample.
Thanks in advance.

Jimmy

--
cheebeng73
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Dec 7 '07 #38

P: n/a
Hi Paul.
Guess what,
Would you please send me a sample of your code?
I would really appreciate it.

10x


*** Sent via Developersdex http://www.developersdex.com ***
Dec 17 '07 #39

P: n/a


Hi Paul

Would you please send me a sample of your code?
You are helping me out.

Thank you
*** Sent via Developersdex http://www.developersdex.com ***
Jan 8 '08 #40

P: n/a


Can you please send me the code as well?

*** Sent via Developersdex http://www.developersdex.com ***
Feb 8 '08 #41

P: n/a
Would you please send me your code as well?
*** Sent via Developersdex http://www.developersdex.com ***
Feb 8 '08 #42

P: n/a
Hi Paul,

It looks like you are getting blasted. Any chance you are still sending
the sample? I would appreciate it!
*** Sent via Developersdex http://www.developersdex.com ***
Mar 4 '08 #43

P: n/a

Hi Paul,
Can you please send me the sample code?

Thanks,
Anirudha
*** Sent via Developersdex http://www.developersdex.com ***
Apr 3 '08 #44

P: n/a


Paul;

I know everyone seems to be asking the same question. Me too. Can you
send me the code.
Thanks

David

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #45

P: n/a

Can you also forward this code example to me as well, it seems to be
very popular, and would save me countless hours of trial and error.
Thank you,
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #46

P: n/a
Here is some sample c# code to print directly to a ZPL or EPL printer

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace CourierLib
{
/// <summary>
/// Summary description for ZPLPrint.
/// </summary>
public class ZPLPrint
{
public readonly int GENERIC_WRITE = 1073741824;
public readonly int OPEN_EXISTING = 3;
public readonly int FILE_SHARE_WRITE = 2;
public string LPTPORT;
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr CreateFile(
string lpFileName,
EFileAccess dwDesiredAccess,
EFileShare dwShareMode,
IntPtr lpSecurityAttributes,
ECreationDisposition dwCreationDisposition,
EFileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[System.Runtime.InteropServices.DllImport("kernel32 ",
EntryPoint="CloseHan
dle")]
public static extern int CloseHandle(int hObject);
int retval;
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
private int nLength;
private int lpSecurityDescriptor;
private int bInheritHandle;
}
[Flags]
public enum EFileAccess : uint
{
GenericRead = 0x80000000,
GenericWrite = 0x40000000,
GenericExecute = 0x20000000,
GenericAll = 0x10000000,
}

[Flags]
public enum EFileShare : uint
{
None = 0x00000000,
Read = 0x00000001,
Write = 0x00000002,
Delete = 0x00000004,
}

public enum ECreationDisposition : uint
{
New = 1,
CreateAlways = 2,
OpenExisting = 3,
OpenAlways = 4,
TruncateExisting = 5,
}

[Flags]
public enum EFileAttributes : uint
{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline= 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
}

public ZPLPrint()
{
//
// TODO: Add constructor logic here
//
}
public void print(string strPrnPath, string sText)
{
FileStream outFile;
IntPtr hPortP;
int hPort;
LPTPORT = strPrnPath;
hPortP = CreateFile(LPTPORT, EFileAccess.GenericWrite,
EFileShare.Write,
IntPtr.Zero, ECreationDisposition.OpenExisting, EFileAttributes.Normal,
IntP
tr.Zero);
hPort = (int)hPortP;
outFile = new FileStream(hPortP, FileAccess.Write, false);
StreamWriter fileWriter = new StreamWriter(outFile);
StreamReader fileReader = new
StreamReader("H:\\VSNet\\CourierLib\\Parcel
ForceLabelDef.txt");
fileWriter.Write("^XA^FO50,50^A030,30^FD " + sText + "^FS");
fileWriter.Write(Formatting.Chr(34));
fileWriter.Write("^FO50,100^A030,30^FD " + sText + "^FS^XZ");
fileWriter.Write(Formatting.Chr(13));
fileWriter.Write(Formatting.Chr(10));
fileWriter.Flush();
fileWriter.Close();
outFile.Close();
retval = CloseHandle(hPort);

}
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #47

P: n/a
Hi Guyz,

after 2 days of finding a solution with out risking the vunerability of
the server. I found the ultimate solution, for me that is, What i did is
to create an aspx page where when the user will click on the button it
will download the EPL file. From there i created a .bat file that
commands it to print the opened file. I set the .bat file as the default
program to open any .epl file.

It works perfectly, whether your in an intranet or internet. I works
because the printer is now in the client side.

MarkV

*** Sent via Developersdex http://www.developersdex.com ***
Jul 30 '08 #48

P: n/a
"Mark Villahermosa" <ma**************@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
Hi [Guyz] Guys,

after 2 days of finding a solution with out risking the vunerability of
the server. I found the ultimate solution, for me that is, What i did is
to create an aspx page where when the user will click on the button it
will download the EPL file. From there i created a .bat file that
commands it to print the opened file. I set the .bat file as the default
program to open any .epl file.

It works perfectly, whether [your] you're in an intranet or internet.
[i] It works because the printer is now in the client side.
How did you persuade an ASP.NET app to modify the default program for .epl
files on a machine which is connected over the Internet...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '08 #49

P: n/a
the .bat file is on the client side. so its only on the client side pc
you have to configure the .epl file to be opened by the .bat file. you
dont have to configure anything on the server since its just like
downloading any file on the server. Its the way the file is handled on
the client side the you have to configure.

MarkV

*** Sent via Developersdex http://www.developersdex.com ***
Jul 31 '08 #50

56 Replies

This discussion thread is closed

Replies have been disabled for this discussion.