The .NET Developer Community

How do I Print on a preprinted form?

rated by 0 users
Answered (Verified) This post has 1 verified answer | 8 Replies | 3 Followers

Mr. Alexis
Not Ranked
Miami
Since 3/21/2003
Posts 8
Reputation 150
Mr. Alexis (Mr. Alexis) posted on 12/11/2010 7:56 PM

Hello programmers,

 

I am going to make a windows form using either C# or VB.Net 3.5 with Visual Studio Express 2008. This form is going to have about 80 to 90 textboxes for input and a Print Button. When I click the Print Button, the entered text must be sent the printer to fill up a pre-printed form.

 

I need to be able to adjust the output coordinates for each textbox so the information prints exactly in the box it is supposed to.

 

At the moment I am not planning to store that information in a database, but I definitely want do it later.

 

What function do you suggest I use for C# 3.5 and VB.Net 3.5?

How could I accomplish this?

 

Thanks In advance for your help.Yes

Answered (Verified) Verified Answer

XTab
Top 10 Contributor
Scotland
Since 2/21/2002
Posts 10,984
Reputation 118,563
ForumsAdministrator
vbCity Team
Answered (Verified) Ged Mead (XTab) replied on 12/12/2010 12:34 AM
Verified by Mr. Alexis (Mr. Alexis)

In my experience, it's usually very fiddly to do this, mainly because it isn't always easy to line up the printed page layout exactly with the screen layout and you find yourself doing a lot of printing/comparing/editing until you get it right.

It used to be that you had to manually set the x and y locations of each string to print, using the PrintDocument component - and of course you can still do that.  

' A procedure to assign textbox text to PrintDocument 
Private Sub PrintDocument1_PrintPage1(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 
e.Graphics.DrawString(Me.TextBox1.Text, New Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125) 
End Sub
 

But it's very laborious!  In the example above, the 150 value is the distance from the left of the printed page and the 125 is the distance from the top.  You can see that you can print all the text you want to by using this method.

MS then brought out the PrintForm power pack component, which makes life easier.  This tool will print whatever is on the screen directly to the printer.  That sounds brilliant, but of course, you probably don't want to print everything because presumably you'll have labels on the screen to help users, but you won't want them printing.  There are various ways round this, and one easy one is to set the visibility of items you don't want printed to hidden once the user hits the print key.

I'm not sure if the printform component is included in the Express edition of 2008, as I don't have it installed.  it's definitely in the full edition.  But if it isn't then you can download it from the MS download site.

One approach to consider, if you're allowed to do this, is to print out the complete form - by that I mean that you replicate the preprinted form on the screen and print the whole shebang out on to plain paper.  In other words you just create a replica of the preprinted format and print it all out.

I spotted a youtube video on using the PrintForm power pack that you might find useful.  The link to that is here.  There's also an MSDN item on this page.

Hope this helps.

 

 

  • | Post Points: 100

All Replies

XTab
Top 10 Contributor
Scotland
Since 2/21/2002
Posts 10,984
Reputation 118,563
ForumsAdministrator
vbCity Team
Answered (Verified) Ged Mead (XTab) replied on 12/12/2010 12:34 AM
Verified by Mr. Alexis (Mr. Alexis)

In my experience, it's usually very fiddly to do this, mainly because it isn't always easy to line up the printed page layout exactly with the screen layout and you find yourself doing a lot of printing/comparing/editing until you get it right.

It used to be that you had to manually set the x and y locations of each string to print, using the PrintDocument component - and of course you can still do that.  

' A procedure to assign textbox text to PrintDocument 
Private Sub PrintDocument1_PrintPage1(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 
e.Graphics.DrawString(Me.TextBox1.Text, New Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125) 
End Sub
 

But it's very laborious!  In the example above, the 150 value is the distance from the left of the printed page and the 125 is the distance from the top.  You can see that you can print all the text you want to by using this method.

MS then brought out the PrintForm power pack component, which makes life easier.  This tool will print whatever is on the screen directly to the printer.  That sounds brilliant, but of course, you probably don't want to print everything because presumably you'll have labels on the screen to help users, but you won't want them printing.  There are various ways round this, and one easy one is to set the visibility of items you don't want printed to hidden once the user hits the print key.

I'm not sure if the printform component is included in the Express edition of 2008, as I don't have it installed.  it's definitely in the full edition.  But if it isn't then you can download it from the MS download site.

One approach to consider, if you're allowed to do this, is to print out the complete form - by that I mean that you replicate the preprinted form on the screen and print the whole shebang out on to plain paper.  In other words you just create a replica of the preprinted format and print it all out.

I spotted a youtube video on using the PrintForm power pack that you might find useful.  The link to that is here.  There's also an MSDN item on this page.

Hope this helps.

 

 

  • | Post Points: 100
firoz.raj
Top 150 Contributor
KSA
Since 12/4/2008
Posts 640
Reputation 12,117

I am going to make a windows form using either C# or VB.Net 3.5 with Visual Studio Express 2008. This form is going to have about 80 to 90 textboxes for input and a Print Button. When I click the Print Button, the entered text must be sent the printer to fill up a pre-printed form.

Better zip the project and post it . so that all the member see the the project . please delete all the files from bin folder.and post it !!! .this way you will get very fast reply !!!.

  • | Post Points: 5
frog
Not Ranked
Sydney Australia
Since 2/12/2004
Posts 160
Reputation 2,440

In my experience printing a form from an image gives a somewhat blurred print. I now use Xtab's method of drawing to printdocument.

  • | Post Points: 5
Mr. Alexis
Not Ranked
Miami
Since 3/21/2003
Posts 8
Reputation 150

 

Thank you Ged Mead, you gave me a starting point.

I know I asked for both VB or C# but since I don’t have much space on my hard drive I didn’t install VB to try your suggestion, but I did look for information about the PrintDocument component in C# because that’s what I have installed at the moment.

 

And from what I have found about your suggestion, I made this and it is working beautifuly, of course, as you said it is very laborious and I am doing a lot of printing/comparing/editing until I get it right for each of the TextBoxes.

It is a dirty job but I am having fun doing it.

Thank you very much to all of you.

I still have eighty more to go.

using System; 
using System.IO; 
using System.Collections; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Drawing.Printing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace MDISample 
{ 
public partial class frmForm32Page1 : Form 

{ 
private Font printFont; 

public frmForm32Page1() 
{ 
InitializeComponent(); 
} 

private void btnPrint_Click(object sender, EventArgs e) 
{ 
try 
{ 
printFont = new Font("Times New Roman", 10); 
PrintDocument pdocument = new PrintDocument(); 
pdocument.PrintPage += new PrintPageEventHandler(this.printDocumentForm32Pag1_PrintPage); 
pdocument.Print(); 
} 
catch(Exception ex) 
{ 
MessageBox.Show(ex.Message); 
} 
} 

private void printDocumentForm32Pag1_PrintPage(object sender, PrintPageEventArgs e) 
{ 

//This will print 0000000000 starting at coordinates 0,0. Just to guide me. 
e.Graphics.DrawString("0000000000", printFont, Brushes.Black, 
0, 0, new StringFormat()); 

/*This will print starting at coordinates 38,193. 
38 is float x (from left to right) and 193 is float y (from top bottom) :) */ 

e.Graphics.DrawString(txtTextBox1.Text, printFont, Brushes.Black, 
38, 193, new StringFormat()); 

e.Graphics.DrawString(txtTextBox2.Text, printFont, Brushes.Black, 
650, 192, new StringFormat()); 
e.Graphics.DrawString(txtTextBox3.Text, printFont, Brushes.Black, 
650, 209, new StringFormat()); 
e.Graphics.DrawString(txtTextBox4.Text, printFont, Brushes.Black, 
38, 218, new StringFormat()); 
e.Graphics.DrawString(txtTextBox5.Text, printFont, Brushes.Black, 
38, 242, new StringFormat()); 
e.Graphics.DrawString(txtTextBox6.Text, printFont, Brushes.Black, 
410, 242, new StringFormat()); 
e.Graphics.DrawString(txtTextBox7.Text, printFont, Brushes.Black, 
630, 242, new StringFormat()); 
e.Graphics.DrawString(txtTextBox8.Text, printFont, Brushes.Black, 
700, 242, new StringFormat()); 
e.Graphics.DrawString(txtTextBox9.Text, printFont, Brushes.Black, 
760, 242, new StringFormat()); 

// And I still have eighty more to go 
// //New Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125) 
} 

} 
}

If you know what is the unit of measure of those numbers (float x and float y)

Please let me know, it would be very helpful.

 

  • | Post Points: 20
frog
Not Ranked
Sydney Australia
Since 2/12/2004
Posts 160
Reputation 2,440

 

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
{ 

foreach(Control c in this.Controls) 
{ 
if (c.Name.Contains("textBox")) 
{ 
e.Graphics.DrawString(c.Text, myfont, Brushes.Black, c.Left + 10, c.Top + 10); 
} 

} 
}
Try this - fiddle with 10 to suit positioning

 

 

 

 

 

 

  • | Post Points: 20
Mr. Alexis
Not Ranked
Miami
Since 3/21/2003
Posts 8
Reputation 150

Thank you Frog I’ll use the “foreach” idea when I have to adjust all the controls at the same time. Thank you one more time for your advice.

  • | Post Points: 5
Mr. Alexis
Not Ranked
Miami
Since 3/21/2003
Posts 8
Reputation 150

Hello programmers, I am having a little situation with this program:

It does not print anything after floatY bigger than 1059. The paper is 8.5 x 14. I mean, floatY = 1059 is good enough for Letter (8.5x11) size sheets of paper but not for longer ones.

 

Am I missing something to make it print after the float Y=1059 mark?

PD: I don’t know if I should open another post for this or continue here.

  • | Post Points: 5
Mr. Alexis
Not Ranked
Miami
Since 3/21/2003
Posts 8
Reputation 150

/*This code did not make any difference.*/

 

Margins margins = new Margins(0,0,0,10);

 

pdocument.DefaultPageSettings.Margins = margins;

 

 

Even though I wanted to do it through code, some how I remembered there was something called “The Control Panel” Ta Daaaa!!! I went there and changed the default paper size from Letter to Legal. If you know how to fix this through code, please let me know. I will highly appreciate it.

Alex.

  • | Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright 1998-2015 vbCity.com LLC