Using a Web Service to Print Files

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Using a Web Service to Print Files

Post by KarlR69 »

I have been experimenting with the SDK for PDF-XChange Viewer in a Windows Forms app and it does what I want it to do. Namely, I select a PDF file and it prints it with some options I select. This was a stepping stone towards my end goal: creating a web service that does this. The web service will take a file name as a parameter and then automatically print the selected document.

In my VB.NET project for the web service, I added references to the AxInterop.PDFXCviewAxLib.dll and Interop.PDFXCviewAxLib.dll. I then created a new instance of the control like this:

Code: Select all

'Create an instance of the control
            Dim AxCoPDFXCview1 As New AxPDFXCviewAxLib.AxCoPDFXCview()
When the code ran, this was the error message that came up when the above line was executed:

ActiveX control cannot be instantiated because the current thread is not in a single-threaded apartment.

Because of this, I modified my code to try to handle the threading issue like this:

Code: Select all

                Dim t As New Thread(New ParameterizedThreadStart(AddressOf RunThis))
                t.SetApartmentState(ApartmentState.STA)
                t.IsBackground = True
                t.Start()
                t.Join()
This worked, as the code no longer errored out when the control was created. However, as I tried to open the document, I ran into another error. This is my code for opening a file:

Code: Select all

                AxCoPDFXCview1.OpenDocument("C:\Temp\Examples\EBYD.pdf", "", iActiveDocID, 0)
The error I got on this line was:

Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.

I am starting to think that this is not the best way to handle this situation. Surely someone else has developed a web service that works with PDF-XChange Viewer. I have experimented with opening the file from a file stream with similar results. What would be an alternate way to do this? Is there a preferred way to load & print files that doesn't require rendering the document on a GUI? I thought I shouldn't be using the ActiveX control, but I wasn't sure what the alternative would be.

I would greatly appreciate any suggestions.

Thank you,
Karl
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Using a Web Service to Print Files

Post by Vasyl - PDF-XChange »

Hi, Karl

Code: Select all

This worked, as the code no longer errored out when the control was created. However, as I tried to open the document, I ran into another error. This is my code for opening a file:
    AxCoPDFXCview1.OpenDocument("C:\Temp\Examples\EBYD.pdf", "", iActiveDocID, 0)
The error I got on this line was:
    Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.
Please try to call all methods of AxCoPDFXCview1 object in the same thread where this object has been created, it may solve your problem.
STA objects must be used only in the same thread where it was created.

HTH.
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Vasyl,

Thanks for your reply. I create the object in the same function where I call the methods (see below code). Is this what you mean? Sorry, my understanding of multiple-thread coding is pretty limited.

Thanks,
Karl

Code: Select all

    Public Function RunThis() As Boolean

        Try
            'Create an instance of the control
            Dim AxCoPDFXCview1 As New AxPDFXCviewAxLib.AxCoPDFXCview()

            Dim iActiveDocID As Integer 'Contains the ID of the currently opened file.

            Try
                'Load the file into the control to display it.
                AxCoPDFXCview1.OpenDocument("C:\Temp\ObjectBank\Examples\EBYD.pdf", "", iActiveDocID, 0)

            Catch ex As Exception
                'ShowErrorMessage(System.Runtime.InteropServices.Marshal.GetHRForException(ex))
                System.Diagnostics.Trace.WriteLine(ex.Message)
            End Try

            If (iActiveDocID < 0) Then
                System.Diagnostics.Trace.WriteLine("There is no opened document.")
                Exit Function
            End If

        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message)
        End Try

    End Function
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Sorry, one additional thought...

What is the PDFXCviewAxLib reference used for? I noticed it has different properties & methods (from the AxPDFXCviewAxLib one) when I define it:

Code: Select all

Dim P As PDFXCviewAxLib.CoPDFXCview()
Would using this somehow be an alternative? Sorry, just grasping at straws here... :?:
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Using a Web Service to Print Files

Post by Vasyl - PDF-XChange »

May be we have other problem here. I'm not sure that is correct in our case:

Code: Select all

'Create an instance of the control
Dim AxCoPDFXCview1 As New AxPDFXCviewAxLib.AxCoPDFXCview()
Because our ActiveX object is the ActiveX Control and it requires the parent form. Otherwise it cannot be used.. I looked in one of our VB.NET examples (OpenDocument) and see:

Code: Select all

Partial Class Form1
 Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.AxCoPDFXCview1 = New AxPDFXCviewAxLib.AxCoPDFXCview
        ...
        CType(Me.AxCoPDFXCview1, System.ComponentModel.ISupportInitialize).BeginInit()
        ....
        Me.AxCoPDFXCview1.Enabled = True
        Me.AxCoPDFXCview1.Location = New System.Drawing.Point(370, 12)
        ...
        Me.Controls.Add(Me.AxCoPDFXCview1)
        CType(Me.AxCoPDFXCview1, System.ComponentModel.ISupportInitialize).EndInit()
        ...
Can you create temporary simple and hidden form that embeds our control in standard way and then use it, inside this form?
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Vasyl,

Unfortunately, when you use a web service, you typically do not have any forms within the project. Visual Studio will not even let you add a Windows Form; you can only add a Web Form (or .aspx file). I added this and tried to use that to create the ActiveX control and got the same error message.

In order to demonstrate what I am dealing with, I have created a sample web service project and attached it here. This solution was developed in Visual Studio 2010, so please let me know if you need a different version. I have hardcoded the PDF file path as "C:\Temp\TestFile.pdf", and so you will need to either create that path, or change my code. I have also attached the PDF for your use.

When you run the project, you will be shown a web page with the available web methods for the web service. Click on the method named "PrintPDF", which will take you to the method. Click on the "Invoke" button here to call the function. If you have a breakpoint set, you can step through the code and see that it always errors on the ".OpenDocument" method. I have included examples where I call it from the main ASMX code and from a web form I added to the project.

Hopefully you are able to run this project and see my issue. I really appreciate you taking the time to look into this. Your product seems to work very well other than in a web service, and I look forward to purchasing it if we can get this to work.

Thanks again,
Karl
You do not have the required permissions to view the files attached to this post.
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Using a Web Service to Print Files

Post by Vasyl - PDF-XChange »

Hi, Karl.

Thanks for your example project. I played some time with it but not succeeded. I couldn't create on server side the webform object with embedded our PDF-Viewer ActiveX Control. Sorry, I'm not a .NET developer. For now I can suggest one easy solution as workaround, it will be not ideal but will work. On the server side when you received the request from client such as 'Open and Print This Document' you may:

1. Create temporary simple html-page with source:

Code: Select all

<html>
<head>
	 <script>
	     function loadPage()
	     {
            var docID = 0;
            try
            {
                document.all.PDFView.OpenDocument("c:\\Temp\\TestFile.pdf", 0, 0, 2);
                docID = document.all.PDFView.Property("Documents.Active", 0);
            }
            catch (err)
            {
                docID = 0;
                alert("DEBUG: Open Error!");
            }
            if (docID != 0)
            {
                try
                {
	                document.all.PDFView.PrintDocument(docID, 10);
                }
                catch (err)
                {
	                alert("DEBUG: Print Error!");
                }
            }
	    }
    </script>
</head>
<body onload="loadPage()">
    <object id="PDFView" classid="CLSID:FE36F0F3-F082-41B7-9EED-772505A7C054" width="100%" height="100%" />
</body>
</html>
- when the page is created then pass to OpenDocument("") the valid file name. The our ActiveX(CLSID:FE36F0F3-F082-41B7-9EED-772505A7C054) must be installed on the server side.

2. Now, in the "RunThis()" function we can open silently this temporary html by:

Code: Select all

Dim WebBrowser As New System.Windows.Forms.WebBrowser
WebBrowser.Navigate("c:\Temp\WebServicePdfDemo\StubPageWithEmbeddedPDFViewer.html")
System.Windows.Forms.Application.DoEvents()
I tried it and it worked with such hardcoded names.

HTH
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Vasyl,

Using your example, I was able to get my web service to print on my local machine. This is very encouraging! I will work on trying this out on my actual server early next week and let you know how I make out.

Thank you very much for your example & continued support!

-Karl
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Using a Web Service to Print Files

Post by Stefan - PDF-XChange »

:)
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

I finally had a chance to test this on my test web server, and I still have some issues unfortunately. I have installed the ActiveX control on my server.

First, I should mention what is working properly. If I bring up the "StubPage" by a direct URL, it works. That is, if I use the URL below, the PDF is rendered on it and comes out on the printer I specified.

Code: Select all

http://servername/obprinterservicebeta/StubPageWithEmbeddedPDFViewer.html
However, when I try to call this from my web service code, it just give a blank page that sits there.

This is the current code in the stub page:

Code: Select all

<html>
<head>
    <script>
        function loadPage()
        {
            var docID = 0;
            try
            {
                document.all.PDFView.OpenDocument("c:\\Temp\\TestFile.pdf", 0, 0, 2);
                docID = document.all.PDFView.Property("Documents.Active", 0);
            }
            catch (err)
            {
                docID = 0;
                alert("DEBUG: Open Error!");
            }
            if (docID != 0)
            {
                try
                {
                    document.all.PDFView.SetProperty("Print.PrinterName", "USHIP5BW", 0);
                    document.all.PDFView.PrintDocument(docID, 10);
                }
                catch (err)
                {
                   alert("DEBUG: Print Error!");
                }
            }
       }
    </script>
</head>
<body onload="loadPage()">
    <object id="PDFView" classid="CLSID:FE36F0F3-F082-41B7-9EED-772505A7C054" width="100%" height="100%" />
</body>
</html>

Since this works on my laptop, I thought it might be a security issue. The web app/service runs under a service account. I have given this account full control permissions of the folder where the service & stub page reside. I have also added this service account to the IIS_WPG group on the server, as I have known this to cause issues on other sites.

What is the best way to proceed? I could send you my project, but I am not sure if you could work with that. Based on the way we have this set up, can you think of some other area (IIS settings, security, etc.) where I need to set something up?

I really feel that we are very close to having this work 100% & appreciate your continued support.

Thanks,
Karl
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Using a Web Service to Print Files

Post by Vasyl - PDF-XChange »

Hi, Karl.

Code: Select all

Dim WebBrowser As New System.Windows.Forms.WebBrowser
WebBrowser.Navigate("c:\Temp\WebServicePdfDemo\StubPageWithEmbeddedPDFViewer.html")
- this code creates a WebBrowser control that load your stub web-page with embedded PDFViewer control. The 'WebBrowser' here is the InternetExplorer definitely. So, you may check the security options of IE to be sure that the activating of ActiveX controls from web-page, inside the IE, isn't forbidden on the server-machine. Also the executing of JavaScrips inside the page must be allowed.. And note: the path "http://servername/stub.html" and "c:\<path>\stub.html" are different even if both are related to the same file on the server-machine. In both cases the IE have two different sets of security options: Internet and Local Intranet.

Look for details:
http://windows.microsoft.com/en-in/wind ... y-settings
and related:
http://windows.microsoft.com/en-IN/wind ... -questions

Or you may try to play with some properties of WebBrowser Control like this:

Code: Select all

WebBrowser.ScriptErrorsSuppressed = true;
WebBrowser.IsScriptEnabled = true;
HTH
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Vasyl,

Thanks for your reply & information. I will experiment with the security settings you detailed here and post back with my results.

Thanks,
Karl
User avatar
Will - Tracker Supp
Site Admin
Posts: 6815
Joined: Mon Oct 15, 2012 9:21 pm

Re: Using a Web Service to Print Files

Post by Will - Tracker Supp »

:)
If posting files to this forum, you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded.
Thank you.

Best regards

Will Travaglini
Tracker Support (Europe)
Tracker Software Products Ltd.
http://www.tracker-software.com
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Unfortunately, I am still having issues when I publish this to a server. :(

I have opened up every IE security setting I could find, including every ActiveX & JavaScript setting. This caused IE to display a large banner at the top saying "you are running with insecure settings", but I left it like that for my testing. The same situation existed, where if I manually browsed to the stub page, it was rendered & printed correctly. However, if I execute it via the web service, nothing happens and no error message is displayed.

I also tried calling the stub page via a "http://" path and the local "C:" path. Neither method worked.

I realize that it is very hard for you to debug this, as you cannot see what I am seeing. I am unfortunately reaching the end of my rope here. If I can't get this working, I am going to have to try something drastic to switch to another method, which I don't want to do. I would appreciate any suggestions of other things I could try with my current method or any "out of the box" suggestions for different things to try.

Thanks again,
Karl
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Using a Web Service to Print Files

Post by Vasyl - PDF-XChange »

Hi, Karl.

Did you find any solution or workaround for problem with IE-security restrictions? Or problem is still?
Just for info...

Best
Regard.
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
KarlR69
User
Posts: 14
Joined: Tue Sep 24, 2013 2:27 pm

Re: Using a Web Service to Print Files

Post by KarlR69 »

Hi Vasyl,

Unfortunately, I never found a workaround for this situation. I ended up doing something drastic as I suggested earlier. I created a small Windows Forms appliciation using the Viewer ActiveX control. The program sits in the user's system tray with a timer running. It periodically checks to see if there are new requests for prints, and then it prints it from the program.

While this works, it is not ideal. Each computer has to install not only my program, but also the Client Distribution file from you guys. If we had ever gotten this working via the web service, there wouldn't have been anything to install. Also, the web service would have worked on-demand as it was called, and I would not have to rely on the somewhat clunky method of a timer. :?

I will continue to move forward with my current solution, but if anyone has any additional thoughts on how this might be resolved, I am all ears.

Thanks for getting back to me,
Karl

P.S. As a reference, this is where I found the Client Distribution file & it's install instructions: https://forum.pdf-xchange.com/ ... nce#p19450
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Using a Web Service to Print Files

Post by Vasyl - PDF-XChange »

Hi Karl.

Please check this topic:

http://social.msdn.microsoft.com/Forums ... eb-service

Here is example of launching an external app from web-service.
If I understand it correctly and if is possible for you - you may create WebService that by user's print-request will launch on the server side the simple WinForm App with embedded Viewer ActiveX and then print by this app the document from this request.

HTH
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.