Categories
.NET C#.NET Code Snippets Programming VB.NET/VB

Printing Text Document in .NET 2.0

Some time reporting tools are too heavy to use with a small application. Something similar happens with me, I need to print a Single page text information for this I cannot use any Heavy reporting software although this information is fetched from XML/Database. So I opt to use in built .NET feature and having following for you to use now.

 First Add a PrintDialog box on your Windows Application that gives printing capability.

Now Include a Object with its events in your code

Private WithEvents doctoprint As New Printing.PrintDocument

Once we have these basic Set up now, we can simply copy paste given two function on page to Print document define in FILE_PATH constant

Private Sub PrintDocument()
PrintText.AllowSomePages = False
PrintText.AllowSelection = False
 PrintText.ShowHelp = False
 PrintText.Document = doctoprint

 Dim result As DialogResult = PrintText.ShowDialog

 If result = Windows.Forms.DialogResult.OK Then
 Try
 streamToPrint = New StreamReader(FILE_PATH)
 doctoprint.DocumentName = "Title to Show on 'Now printing dialog'"
 doctoprint.Print()
 streamToPrint.Close()
 Catch ex As Exception
 MsgBox("Unable to print")
 End Try
 End If
 End Sub

 Private Sub doctoprint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles doctoprint.PrintPage

'change this line tochange default font for printing.
 Dim printFont As New System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular)

 Dim linesPerPage As Single = 0
 Dim yPos As Single = 0
 Dim count As Integer = 0
 Dim leftMargin As Single = e.MarginBounds.Left
 Dim topMargin As Single = e.MarginBounds.Top
 Dim line As String = Nothing

 ' Calculate the number of lines per page.
 linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

 ' Print each line of the file.
 While count < linesPerPage
 line = streamToPrint.ReadLine()
 If line Is Nothing Then
 Exit While
 End If
 yPos = topMargin + count * printFont.GetHeight(e.Graphics)
 e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
 count += 1
 End While

 ' If more lines exist, print another page.
 If Not (line Is Nothing) Then
 e.HasMorePages = True
 Else
 e.HasMorePages = False
 End If
 End Sub

Now, simply call your PrintDocument() function to start printing of FILE_PATH using your Print Button or menu option.

Categories
.NET C#.NET Programming Server Configuration VB.NET/VB

Creating Schedular application in Windows

This can be done in many ways. Often I see my collegues doing a ASP script that they schedule to run with Browser. As this seems quite obvious thing for them. An ASP scripts can run better in browser when called through IIS, and they can then schedule to run at given time and calling ASP page of their.

But this is a wrong way of doing it. Why? first, it will open your Web browser at given time, if you are on desktop machine you will see that browser popup and then navigate to your give url. Second, mostly commonly people put website url to their script such as http://www.example.com/work.asp instead of http://localhost/work.asp, this will cause your bandwidth consumption for no reason. Takes more memory then needed. and finally, it will time out if you have long execution to do.

So, what you need to do. My best bet is create your own Windows Service and install it, if your shared hosting doesn’t provide that feature, then make EXEcutable file and let your hoster schedule it for you. This file once schedule will do your desire work.

If you are using ASP.NET then it has some inbuilt support for threading, that you can start and then put in memory to run forever. However, I never work on this part but see it in action for software like DOTNETNUKE etc.

Suggest anyother method if you can please.

Categories
.NET C#.NET Programming

Sending inline Image using C#.NET

Recently i have to send an inline image in my outgoing automated email. I don’t know that it was that easy..

Read this article for sending inline image using C#.NET

Sumit Gupta

Categories
.NET ASP.NET C#.NET VB.NET/VB

Two Forms in ASP.Net

Hi,

As any other ASP.Net developer, I was not feeling comfortable with only one form concept that ASP.Net has (not sure about v2.0 as never work on it). Also, when you have to put something like google Search or other such service on your Site and place for such “explicit” forms are in middle of your Only “Runat Server form” how will you put the other forms …

Well what I did is simply use IFrame, capabilities of web browsers. I simply make a HTML page of Google search, and using IFrame call that page. now since it load explicit HTML page, you don’t have to worry about nested form, but simply about how it looks.

though It has a disadvantage that , on older browser your site might not be viewable, but who cares about old stuffs … every body you should get new browser after all browser are free 🙂

Sumit Gupta