Printing out your W2 Form using C# and .NET

2024-01-17 12:18

本文主要是介绍Printing out your W2 Form using C# and .NET,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2001年12月25日 09:28:00
Printing out your W2 Form using C# and .NET
Submitted ByUser LevelDate of Submission
Mike GoldIntermediate08/07/2001

Source Code: W2FormMG.zip

Fig 1.0 - PrintPreview of the W2 Form

If you are a C# programmer, before you run out and buy that latest version of your favorite tax program, you may want to consider this useful article on form creation. This article covers a fairly practical aspect of using a computer - dealing with forms. The concepts in this article can be used to create any Form Application so that you can design forms that you can Fill Out, Open, Save, Print and Print Preview. Below is the simple UML design of our W2 Form Filler Application:

Fig 1.1 - UML Doc/View Design of W2 Form App reverse engineered using WithClass 2000

The first step to creating the form is to scan in the Form and convert it to a .gif file. You can also get most forms these days electronically from the .gov sites that supply them. Once you've got the form in a .gif (or jpeg or whatever), you can apply it to your form as an image background. Simply set the forms BackgroundImage property to the gif or jpeg file that you scanned in. Now you are ready to set up the forms edit fields. This form only uses TextBoxes and CheckBoxes. The TextBoxes overlay the whitespaces for all the places you would normally fill in the form and have names that are appropriate for the particular field on the form. The property window for a TextBox is shown below:

Fig 1.2 - Property window of a TextBox in the Form

The BorderStyle for all textboxes are set to none and the background color(BackColor) is set to the color of the form.

With Checkboxes you need to do a bit of extra work. Since you can't eliminate the border of a checkbox, we needed to go into the jpeg file of the form and remove the checkboxes, so we could place the real checkboxes in the form. You can us MSPaint and the little eraser utility to do this easily enough. The properties of the checkboxes are shown below:

Fig 1.3 - Property window of a Checkbox in the Form

You need to choose the FlatStyle to be Flat for the Checkbox, so the checkbox looks like a checkbox on a typical government form rather than that fun 3d look.

Once we've put all our window controls on the form, we need to set the tab order. This is done by clicking on the form and going into the View menu, then choosing Tab Order.

Fig 1.4 - Choosing Tab Order from the View Menu

Once you've chosen the Tab Order menu item, you'll notice your form light up with a bunch of boxed numbers. Click in each box in the order you want users to traverse your form.

Now we can get down to some good old-fashioned C# coding (well not that old-fashioned yet ;-). This application handles many aspects of C# .NET coding (serialization, printing, print preview). We are only going to talk about printing in detail in this article, because, well, it's the most interesting. Printing requires that you have a PrintDocument object added to the form. We've also added a PrintDialog object and a PrintPreview Dialog Object. It's much less expensive and time consuming to test printing in the print preview window so you should try to get this working first. The PrintPreview code is shown below:

privatevoid PreviewMenu_Click(object sender, System.EventArgs e)
{
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = this.printDocument1 ; // Attach PrintDocument to PrintPreview Dialog
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D ;
printPreviewDialog1.SetBounds(20, 20, this.Width, this.Height); // enlarge dialog to show the form
printPreviewDialog1.ShowDialog();
}

Listing 1 - Code for Print Preview

Both printing and print preview use the same event to print the form. They both use the PrintPage event from the print document. All printing code is performed in this routine. The printing is done into a Graphics object which is passed into the print page event arguement:

privatevoid printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
DrawAll(e.Graphics); // Pass the Graphics Object (or better known as the Device Context) to the Draw routine
}

Listing 2 - Code For Printing a Page

The DrawAll routine is in two parts. The first part prints the image scaled to fit on the printed page. The second part cycles through all the controls and prints the text or check mark contained within each control. The program uses the position of the textboxes and checkboxes, along with the scaling factors, to position the filled-in information on the form:

privatevoid DrawAll(Graphics g)
{

// Create the source rectangle from the BackgroundImage Bitmap Dimensions
RectangleF srcRect = new Rectangle(0, 0, this.BackgroundImage.Width, BackgroundImage.Height);

// Create the destination rectangle from the printer settings holding printer page dimensions
int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
RectangleF destRect = new Rectangle(0, 0, nWidth, nHeight/2);

// Draw the image scaled to fit on a printed page
g.DrawImage(this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel);

// Determine the scaling factors of each dimension based on the bitmap and the printed page dimensions
// These factors will be used to scale the positioning of the contro contents on the printed form
float scalex = destRect.Width/srcRect.Width;
float scaley = destRect.Height/srcRect.Height;

Pen aPen = new Pen(Brushes.Black, 1);

// Cycle through each control. Determine if it's a checkbox or a textbox and draw the information inside
// in the correct position on the form

for (int i = 0; i > this.Controls.Count; i++)
{
// Check if its a TextBox type by comparing to the type of one of the textboxes
if (Controls[i].GetType() == this.Wages.GetType())
{
// Unbox the Textbox
TextBox theText = (TextBox)Controls[i];

// Draw the textbox string at the position of the textbox on the form, scaled to the print page
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left*scalex, theText.Bounds.Top * scaley,
new
StringFormat());
}

if (Controls[i].GetType() == this.RetirementPlanCheck.GetType())
{
// Unbox the Checkbox
CheckBox theCheck = (CheckBox)Controls[i];


// Draw the checkbox rectangle on the form scaled to the print page
Rectangle aRect = theCheck.Bounds;
g.DrawRectangle(aPen, aRect.Left*scalex, aRect.Top*scaley, aRect.Width*scalex, aRect.Height*scaley);

// If the checkbox is checked, Draw the x inside the checkbox on the form scaled to the print page
if (theCheck.Checked)
{
g.DrawString("x", theCheck.Font, Brushes.Black,
theCheck.Left*scalex + 1, theCheck.Top*scaley + 1, new StringFormat());
}
}

}

}

Listing 3 - Drawing routine for drawing the form to the printer or the print preview

The actual printing onto a printer begins in the routine below. This routine brings up the print dialog and if the user accepts, it prints the form onto the printer using the PrintPage event previously discussed:

privatevoid menuItem2_Click(object sender, System.EventArgs e)
{
// Attach the PrintDialog to the PrintDocument Object
printDialog1.Document = this.printDocument1;

// Show the Print Dialog before printing

if (printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print(); // Print the Form
}

}

Listing 4 - Printing Menu Event for printing to the printer

Serialization

You may want to browse through the rest of the code to see how serialization is done. This project uses a Document/View archictecture. The W2Document Class handles persistence for the form(reading/writing) and the W2Document is made serializable by the [Serializable()] attribute inside the class. The read and write routines use the BinaryFormatter Class in combination with the File Class to serialize and deserialize the information extracted from the form.

Improvements

I think that the project could be made much more useful if the code was ported to the Web Form. Then someone could create an application with C# running code-behind that outputted the Web Form information into a database rather than a file. Then maybe government, hospitals, insurance companies, law firms and all other businesses dealing with "form-bureaucracy" could get more easily organized ;-) through .NET.


About the Author:Mike Gold is President of Microgold Software Inc. and Creator of WithClass 2000 a UML Design Tool for C#. In the last few years Mike has consulted for companies such as Merrill Lynch and Chase Manhattan Bank in New York. He is been active in developing Visual C++ Applications for 10 years and looks forward to the possibilities in C#. You can reach him at techsupport@microgold.com.



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=3543


这篇关于Printing out your W2 Form using C# and .NET的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/616007

相关文章

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.