【问题标题】:Print the contents of a text file on the client's printer using asp.net使用 asp.net 在客户端打印机上打印文本文件的内容
【发布时间】:2013-05-06 19:30:58
【问题描述】:

我在服务器上上传了一个文本文件。

当用户单击打印按钮时,我希望使用他/她的打印机打印文件。

目前我正在使用下面的代码,但它是用于服务器端打印的。

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

【问题讨论】:

    标签: asp.net vb.net printing client-side


    【解决方案1】:

    使用 window 对象的 Javascript print function

    【讨论】:

    • 我刚才尝试了你的建议。但它打印整个页面。我想打印一个文本文件的内容。
    • 你需要有一个页面只显示你想打印的内容,然后在那个页面调用print函数。例如,像维基百科这样的网站,每个页面都有一个“打印机友好”版本,这正是您所需要的。
    • 是的,但它只打印半页。 2 分钟后查看问题中附加的图片。
    • 您确定不是打印机设置的问题吗?比如进纸尺寸和边距?
    • 这不是问题,因为我正在尝试使用 Microsoft XPS 文档查看器进行打印。
    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多