wordwrap for printing

S

Steve

Hi All

I have a windows forms 2005 application which has to print to a receipt
printer using OPOS drivers

The text is inserted in a windows textbox and saved to SQL server express
2005 in a nvarchar column within my application

I need to create a string from the database column to pass to the OPOS
drivers with the text wordwrapped to suit a particular string.length, so it
fits on the 80mm paper roll correctly

Most examples from the web do not work correctly

The string contains vbcr and spaces etc

Does anybody have any good routines which allows me to wordwrap the string
before passing it to the printer?

Regards
Steve
 
L

Linda Liu[MSFT]

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.
 
S

Steve

Hi Linda

The Printers I use (POS docket Printers) do not use Windows drivers, hence
there is no Printer in Windows Printers section

The Printers come with OPOS drivers and I have to format the string before
sending it to the OPOS drivers

They do not have the features you have mentioned, unfortunately, so I have
to be able to wordwrap the string in code by adding VBCRLF etc to the
relevant location in the string, without splitting words

Regards
Steve

Linda Liu said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top