简体   繁体   中英

Printing from Windows Control embedded in ASP.NET page

I am working on an application for which requires the following functionality: The user clicks a button on the web page and this causes a document to be printed without any further user intervention.

The approach I am using to deliver this functionality is to embed a Windows Control (using a Windows Control Library) in the web-page. (A tutorial on doing this is available here.) I worked through a similar tutorial and the Windows Control was properly created and displayed.

The next step was to try actually printing. I first tested the printing code is a stand-alone application to make sure that it functions properly. It does. I have provided a simplified version of that code (see below) for those who are interested.

The specific manner in which the code gets activated is this: This line of code, .Attributes.Add("OnClick", "myPrintFunction();") appears in Page_Load to associate a JavaScript function with the button. This JavaScript function is quite simple:

    function myPrintFunction() {
        var winCtrl = document.getElementById("MyWinControl");
        winCtrl.doMyPrintFunction();
    }

The problem is this: The code throws the following error:

mscorlib: Request for the permission of type 'System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.

What permissions do I need to change to make this work? One web-page I found seemed to suggest that setting Trust=Full might help, but it was not clear a) if this would help, or b) how to do this.

Thanks for your help!

Printing code:

Public Class MyWinControl
    Public Sub doMyPrintFunction()
        Dim prtMyDoc As PrintDocument = printerSetup()
        If (Not prtMyDoc Is Nothing) Then prtMyDoc.Print()
    End Sub

    Private Function printerSetup() As PrintDocument
        Dim prtMyDoc As New PrintDocument
        AddHandler prtMyDoc.PrintPage, AddressOf prtMyDoc_PrintPage
        prtMyDoc.PrinterSettings.PrinterName = "<my default printer>"
        Return prtMyDoc
    End Function

    Private Sub prtMyDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        ' use graphics native call to build up page to be printed
        ' it is accessed via e.Graphics
    End Sub
End Class

You are barking up the wrong tree here. This is never going to work across more than a small subset of your users, unless every single one of them has the same setup. Use javascript's print support to print web pages and allow the user some control over it. Anything more and you are just not getting the whole "web-based application" idea.

If you need some rigid formatting or want to print something like a Word doc or PDF, you need to just push the user the file and allow them to handle/print it on their own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM