Hi Steve,
Firstly, the System.Drawing.Graphics class encapsulates a GDI+ drawing
surface and provides methods for drawing objects to the display device.
Using the Graphics class to draw objects, we needn't care about the
underlying detailed GDI+ technology and no matter where we're going to
draw, we can call the drawing methods on the Graphics class all the same.
As for your question, you needn't wordwrap the string manually to fit for
a
particular paper width at all. Instead, you should draw the string in a
specified paper margin and the string will be wordwrapped within the paper
margin automatically.
For example, I'd like to draw some text to a paper of 100mm width and
300mm
height. The paper magin would be 1 inch.
The first step is to add a custom page size to the "Printing Preferences"
dialog of the printer.
To do this, go to Control Panel -> Printers. Right click the printer I
want
to use and choose "Printing Preferences" command. In the coming dialog,
switch to the "Paper/Quality" tab and click the "Custom" button to add a
custom page size called "test paper size".
The second step is to use the custom page size added above in the
application. The following is a sample. It requires that you add a
PrintDocument and a Button on the form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
}
void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Red,e.MarginBounds);
e.Graphics.DrawString("this is a very long text to print. this
is a very long text to print" +
" this is a very long text to print. this is a very long text
to print. this is a very long text to print" +
" this is a very long text to print. this is a very long text
to print. this is a very long text to print",
this.Font, Brushes.Black,e.MarginBounds);
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <
this.printDocument1.PrinterSettings.PaperSizes.Count; i++)
{
if
(this.printDocument1.PrinterSettings.PaperSizes
.PaperName == "test
paper
size")
{
this.printDocument1.DefaultPageSettings.PaperSize =
this.printDocument1.PrinterSettings.PaperSizes;
break;
}
}
this.printDocument1.DefaultPageSettings.Margins.Left = 50;
this.printDocument1.DefaultPageSettings.Margins.Top = 50;
this.printDocument1.DefaultPageSettings.Margins.Bottom = 50;
this.printDocument1.DefaultPageSettings.Margins.Right = 50;
this.printDocument1.Print();
}
}
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.