Print the contents of a text file on the client's printer using asp.net

天大地大妈咪最大 提交于 2019-12-13 04:29:22

问题


I am having a text file uploaded on the server.

When a User Clicks on print button I want the file to be printed using his/her printer.

Currently I am using the below code, but it is for the server side printing.

Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click

        Dim fileName As String = ""

        For x As Integer = 0 To FileImageList.Count - 1

            If FileImageList(x).parent.backcolor = Drawing.Color.Orange Then
                fileName = FileNameList(x).text
            End If

        Next

        ReadFile(fileName, Server.MapPath("~\Medical\Users\" & Session("DocumentUploader") & "\Uploaded\"))
        printDocument1.Print()

    End Sub

Private Sub printDocument1_PrintPage(ByVal sender As Object, _
        ByVal e As PrintPageEventArgs)

        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 0
        Dim font As System.Drawing.Font = New Font("Times New Roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)

        ' Sets the value of charactersOnPage to the number of characters  
        ' of stringToPrint that will fit within the bounds of the page.
        e.Graphics.MeasureString(stringToPrint, font, e.MarginBounds.Size, _
            StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

        ' Draws the string within the bounds of the page
        e.Graphics.DrawString(stringToPrint, font, Brushes.Black, _
            e.MarginBounds, StringFormat.GenericTypographic)

        ' Remove the portion of the string that has been printed.
        stringToPrint = stringToPrint.Substring(charactersOnPage)

        ' Check to see if more pages are to be printed.
        e.HasMorePages = stringToPrint.Length > 0

    End Sub


回答1:


Use the Javascript print function of the window object.



来源:https://stackoverflow.com/questions/16405837/print-the-contents-of-a-text-file-on-the-clients-printer-using-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!