2013-05-06 157 views
0

我正在服務器上傳文本文件。使用asp.net打印客戶端打印機上的文本文件的內容

當用戶點擊打印按鈕時,我希望使用他/她的打印機打印文件。

當前我正在使用下面的代碼,但它是用於服務器端打印。

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 

enter image description here

回答

0

使用JavaScript print functionwindow對象。

+0

我剛剛嘗試了你的suggession。但它打印整個頁面。我想打印文本文件的內容。 – Vishal 2013-05-06 19:41:58

+0

您需要有一個只顯示您想要打印的內容的頁面,然後在該頁面調用'print'功能。例如,像維基百科這樣的站點,每個頁面都有一個「打印友好」的版本,這正是你所需要的。 – Renan 2013-05-06 19:44:58

+0

雅但它僅打印半頁。 2分鐘後查看問題中附帶的圖片。 – Vishal 2013-05-06 19:49:48

相關問題