Printing a pdf in an asp.net app

  • Thread starter Thread starter Brian Hanson
  • Start date Start date
B

Brian Hanson

Hi,

I am trying to programmatically print a pdf file via an asp.net
application.
I have seen other postings that mention the following code used or
something similar to it in a vb.net app.

Process.Start("C:\Program Files\Adobe\Acrobat
5.0\Reader\AcroRd32.exe", "/p
/h ""C:\Program Files\Adobe\Acrobat 5.0\Help\ENU\ACROBAT.PDF""")

My question is if I can do this same thing in a web app. I will be
able to give it printer names and exact paths if necessary, but I
haven't got the above code or other example process code to work. Can
I only print a file behind the scenes from within a vb app?

Thanks
 
Hi,

Thanks for your post. Now I'd like to share the following information with
you:

Based on my experience, we are able to execute a program in ASP .NET by
using Diagnostics.Process.Start. In addition, I suggest that you can also
automate Acrobat, open the pdf file and then print it in your ASP .NET Web
application. You may have to install the full Acrobat produce in order to
automate the Acrobat. Please refer to the following code snippet:

//-------------code snippet--------------
Dim acrApp As Object
Dim avDoc As Object
Dim pdDoc As Object
Dim nbrPages As Int16

acrApp = CreateObject("AcroExch.App")
avDoc = CreateObject("AcroExch.AVDoc")
avDoc.Open("c:\t.pdf", "")
pdDoc = avDoc.GetPDDoc()
nbrPages = pdDoc.GetNumPages()

'avDoc.PrintPages 0, 1, 2, False, True
avDoc.PrintPages(0, nbrPages - 1, 2, False, True)
pdDoc = Nothing
avDoc = Nothing
//------------------end of---------------------

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi Brian,

Thanks for your response.
registry.
Would you please tell me the detailed error message? Do you have full
Acrobat product installed?
I will need more time to investigate this issue.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi Brian,

After further research, I noticed that the acrobat app can be started from
ASP .NET web page, however, it does not print the file. I will update you
with further result.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi,

Thanks for you efforts.

Are you saying that I cannot use the process.start method to print a
file?

Process.Start("C:\Program Files\Adobe\Acrobat
5.0\Reader\AcroRd32.exe", "/p
/h ""C:\Program Files\Adobe\Acrobat 5.0\Help\ENU\ACROBAT.PDF""")


Although I do have the full Acrobat installed, AcroRd32.exe in the
above example is Reader. I am currently seeing AcroRd32.exe in a task
manager when I run this, but nothing further happens. All indications
from other postings point to security for the aspnet account, but I am
still not having success.

Thanks
 
Hello Brian,

Now I'd like to share the following information with you:

1. After furthe investigation, I found that we are able to print a file by
using process.start. Generally speaking, printers are not installed for
either the system account or the ASPNET account. When we spawn a process
from ASP .NET, it will be created under the processIdentity of the
aspnet_wp.exe process (defaults to SYSTEM), unless explicitly specified.
That's the reason why the acrobat process is started, however, nothing
printed.

2. To workaround this problem, we should let ASP .NET worker process
(aspnet_wp.exe) run under a specific user account (say Administrator)
instead of the default SYSTEM identity. You can do so by changing
<processModel> configuration element in Machine.Config file. For example:

<system.web>
<processModel enable="true"
userName="Administrator"
password="AdminPassword"/>
</system.web>

Please refer to the following MSDN article for detailed information:

Configuring ASP.NET Process Identity
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconfiguringaspnetprocessidentity.asp

I look forward to your result.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi Tim,

Thanks for all of your help. Unfortunately, I've tried both of your
suggestions with no luck.

I'm no longer even to the point where It runs in the background, I get
an message that says System.ComponentModel.Win32Exception: Access is
denied. After spending two hours with my network admin giving away
rights to the farm, I'm no further along.

I gave SYSTEM full rights to my printer and changed my machine.config
file and I'm no further along than I was three days ago. Can you
please send my your test code using process.start that you used to
print a pdf, that would help my in my process of elimination.

Thanks Again
 
Hi Tim,

Thanks for all of your help. Unfortunately, I've tried both of your
suggestions with no luck.

I'm no longer even to the point where It runs in the background, I get
an message that says System.ComponentModel.Win32Exception: Access is
denied. After spending two hours with my network admin giving away
rights to the farm, I'm no further along.

I gave SYSTEM full rights to my printer and changed my machine.config
file and I'm no further along than I was three days ago. Can you
please send my your test code using process.start that you used to
print a pdf, that would help my in my process of elimination.

Thanks Again
 
Hello Brian,

Thanks for posting in the group.

After reviewing the whole post thread, I think we need to clarify some
points here:

1) Do you really want to print the pdf file in server side? In ASP.NET, the
codes are running on server side. That is to say, if we use Process.Start
to print the file, the file is printed in your web server side, not web
client side. Please make sure that it is what you need.

2) If we confirm that pdf is to be printed in server side, then we come to
consider this:
The printer you configured at the server is for the interactive user. That
means, if you log on the server using the account bdhanson and setup the
printer, the printer is only available for bdhansonand others cannot see
the printer. By default, your ASP.NET worker process is using the ASPNET
account so it cannot even see the printer.

To let your asp.net app see the printer, you need use impersonation. Please
check the following article and I hope it helps.
Q306158 INFO: Implementing Impersonation in an ASP.NET Application
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306158

You may also need to load the profile of the interactive user here to make
it work.

Generally speaking, we use "Crystal Report" to print doc from asp.net,
which should be easiler. You could search in google for many samples in
this area.

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: (e-mail address removed) (Brian Hanson)
!Newsgroups: microsoft.public.dotnet.general
!Subject: Re: Printing a pdf in an asp.net app
!Date: 12 Sep 2003 12:41:29 -0700
!Organization: http://groups.google.com/
!Lines: 60
!Message-ID: <[email protected]>
!References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
!NNTP-Posting-Host: 131.184.58.5
!Content-Type: text/plain; charset=ISO-8859-1
!Content-Transfer-Encoding: 8bit
!X-Trace: posting.google.com 1063395691 29930 127.0.0.1 (12 Sep 2003
19:41:31 GMT)
!X-Complaints-To: (e-mail address removed)
!NNTP-Posting-Date: 12 Sep 2003 19:41:31 GMT
!Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-05!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:108330
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Hi Tim,
!
!Thanks for all of your help. Unfortunately, I've tried both of your
!suggestions with no luck.
!
!I'm no longer even to the point where It runs in the background, I get
!an message that says System.ComponentModel.Win32Exception: Access is
!denied. After spending two hours with my network admin giving away
!rights to the farm, I'm no further along.
!
!I gave SYSTEM full rights to my printer and changed my machine.config
!file and I'm no further along than I was three days ago. Can you
!please send my your test code using process.start that you used to
!print a pdf, that would help my in my process of elimination.
!
!Thanks Again
!
!
[email protected] (Tian Min Huang) wrote in message
!> Hello Brian,
!>
!> Now I'd like to share the following information with you:
!>
!> 1. After furthe investigation, I found that we are able to print a file
by
!> using process.start. Generally speaking, printers are not installed for
!> either the system account or the ASPNET account. When we spawn a process
!> from ASP .NET, it will be created under the processIdentity of the
!> aspnet_wp.exe process (defaults to SYSTEM), unless explicitly specified.
!> That's the reason why the acrobat process is started, however, nothing
!> printed.
!>
!> 2. To workaround this problem, we should let ASP .NET worker process
!> (aspnet_wp.exe) run under a specific user account (say Administrator)
!> instead of the default SYSTEM identity. You can do so by changing
!> <processModel> configuration element in Machine.Config file. For example:
!>
!> <system.web>
!> <processModel enable="true"
!> userName="Administrator"
!> password="AdminPassword"/>
!> </system.web>
!>
!> Please refer to the following MSDN article for detailed information:
!>
!> Configuring ASP.NET Process Identity
!>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
!> l/cpconconfiguringaspnetprocessidentity.asp
!>
!> I look forward to your result.
!>
!> Have a nice day!
!>
!> Regards,
!>
!> HuangTM
!> Microsoft Online Partner Support
!> MCSE/MCSD
!>
!> Get Secure! ¨C www.microsoft.com/security
!> This posting is provided ¡°as is¡± with no warranties and confers no
rights.
!
 
Hi Yanhong,

Here's the latest.

1) Do I really want to print on the server side? Yes.

2) The printer is set up locally for aspnet and the rest of the
village and impersonation is being used (6 threads ago)... and still
no luck.

I found a third party piece of software to do the job because
something (else) is keeping me from doing the process.start thing.

Thanks
 
Hello Brian,

Thanks very much for the quick response.

There is some difference when we spawn a process from asp.net web
application since it is spawn from a service. To verify it, we could simply
create a notepad process. From task manager, we could see that notepad.exe
is in process list. However, it is not shown in the desktop, because the
exe exists on another desktop.

Any printing operation depends on registry entries located in
HKEY_CURRENT_USER. This registry hive is dynamic. Depending on which user
context the process is running under, different information will be loaded
into this hive. ASP pages run under IIS, which is running as the SYSTEM
account. When we print something in the ASP code, by default, it will also
run as the SYSTEM account. By default, the SYSTEM account does not have any
printers set up in the registry.

There are several ways to resolve this issue:

A. set up printers for the SYSTEM account to resolve this problem. To set
up printers for the SYSTEM account, perform the following:

This method requires you to modify the registry using the Registry Editor.

WARNING: Using Registry Editor incorrectly can cause serious, system-wide
problems that may require you to reinstall Windows to correct them.
Microsoft cannot guarantee that any problems resulting from the use of
Registry Editor can be solved. Use this tool at your own risk.

1. Ensure that the user you are currently logged into on the server has the
desired printers installed.
2. Launch the Registry Editor (Regedit.exe).
3. Select the following key:

HKEY_CURRENT_USER
\Software\Microsoft\Windows NT\Current Version\Devices

4. From the Registry menu, click Export Registry File.
5. In the File Name text box, type c:\Devices.reg.
6. Select the following key:

HKEY_CURRENT_USER
\Software\Microsoft\Windows NT\Current Version\PrinterPorts

7. From the Registry menu, click Export Registry File.
8. In the File Name text box, type c:\PrinterPorts.reg.
9. Select the following key:

HKEY_CURRENT_USER
\Software\Microsoft\Windows NT\Current Version\Windows

10. From the Registry menu, click Export Registry File.
11. In the File Name text box, type c:\Windows.reg.
12. From the Start button, select Run. Open Devices.reg in Notepad by
typing Notepad Devices.reg in Run dialog box.
13. Replace the text HKEY_CURRENT_USER with HKEY_USERS\.DEFAULT
14. Save the file. Then import it into the registry by double-clicking the
file in Windows Explorer.
15. Repeat steps 13 through 15 for PrinterPorts.reg and Windows.reg.

B. Run the ASP code in the interactive user who currently logged onto the
web server.
1. Open IIS manager and set the Application Protection of this web project
to High, which causes IIS to create a COM+/MTS package in COM+/MTS.
2. Open MTS or COM+ manager and change the Identity of the package created
above to Interactive User.

Note: This only works when one user with sufficient permission and printer
information in its registry logged on to the web server.

Please post here if you have any more concerns on it.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: (e-mail address removed) (Brian Hanson)
!Newsgroups: microsoft.public.dotnet.general
!Subject: Re: Printing a pdf in an asp.net app
!Date: 15 Sep 2003 12:47:08 -0700
!Organization: http://groups.google.com/
!Lines: 54
!Message-ID: <[email protected]>
!References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
!NNTP-Posting-Host: 131.184.58.5
!Content-Type: text/plain; charset=ISO-8859-1
!Content-Transfer-Encoding: 8bit
!X-Trace: posting.google.com 1063655230 587 127.0.0.1 (15 Sep 2003 19:47:10
GMT)
!X-Complaints-To: (e-mail address removed)
!NNTP-Posting-Date: 15 Sep 2003 19:47:10 GMT
!Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!news-out.cwix.com!newsfeed.cwix.com!prodigy.com!pd2nf1so.cg.shawcab
le.net!residential.shaw.ca!sn-xit-03!sn-xit-01!sn-xit-06!sn-xit-09!supernews
..com!postnews1.google.com!not-for-mail
!Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.general:108286
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Hi Yanhong,
!
!Here's the latest.
!
!1) Do I really want to print on the server side? Yes.
!
!2) The printer is set up locally for aspnet and the rest of the
!village and impersonation is being used (6 threads ago)... and still
!no luck.
!
!I found a third party piece of software to do the job because
!something (else) is keeping me from doing the process.start thing.
!
!Thanks
!
!
!
!> Hello Brian,
!>
!> Thanks for posting in the group.
!>
!> After reviewing the whole post thread, I think we need to clarify some
!> points here:
!>
!> 1) Do you really want to print the pdf file in server side? In ASP.NET,
the
!> codes are running on server side. That is to say, if we use
Process.Start
!> to print the file, the file is printed in your web server side, not web
!> client side. Please make sure that it is what you need.
!>
!> 2) If we confirm that pdf is to be printed in server side, then we come
to
!> consider this:
!> The printer you configured at the server is for the interactive user.
That
!> means, if you log on the server using the account bdhanson and setup the
!> printer, the printer is only available for bdhansonand others cannot see
!> the printer. By default, your ASP.NET worker process is using the ASPNET
!> account so it cannot even see the printer.
!>
!> To let your asp.net app see the printer, you need use impersonation.
Please
!> check the following article and I hope it helps.
!> Q306158 INFO: Implementing Impersonation in an ASP.NET Application
!> http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306158
!>
!> You may also need to load the profile of the interactive user here to
make
!> it work.
!>
!> Generally speaking, we use "Crystal Report" to print doc from asp.net,
!> which should be easiler. You could search in google for many samples in
!> this area.
!>
!> Hope that helps.
!>
!> Best regards,
!> Yanhong Huang
!> Microsoft Online Partner Support
!
 
Back
Top