473,472 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

e.graphics.DrawString text cuts off from R.H.S of the page

79 New Member
Hi.

I am having a problem in printing text to printer through printPage event using e.grphics.drawString method. On printdocument.printPage event When it draws the string, text from the right hand side of the page gets out.. And printer finishes printing...
Expand|Select|Wrap|Line Numbers
  1. void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  2.         {
  3.             Font myFont = new Font("m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point);
  4.  
  5.             siteHTML = webBrowser1.Document.Body.InnerHtml + webBrowser1.Document.Body.OuterHtml;
  6.  
  7.             float lineHeight = myFont.GetHeight(e.Graphics) + 4;
  8.             float yLineTop = e.MarginBounds.Top;
  9.  
  10.                 if (yLineTop + lineHeight > e.MarginBounds.Bottom)
  11.                 {
  12.                     e.HasMorePages = true;
  13.                     return;
  14.                 }
  15.                 //e.Graphics.DrawString(siteHTML, myFont, Brushes.Black,
  16.                   //  new PointF(e.MarginBounds.Left, yLineTop));
  17.                 e.Graphics.DrawString(siteHTML, myFont, Brushes.Black, e.PageSettings.PrintableArea.X, e.PageSettings.PrintableArea.Y);
  18.                 yLineTop += lineHeight;
  19.  
  20.             e.HasMorePages = false;
  21.         }
  22.  
How will I deal with this problem?

Regards,
Syed Ahmed Hussain
Sep 7 '10 #1
12 8735
Aimee Bailey
197 Recognized Expert New Member
What you need to take into consider is that DrawString does not automatically wrap your text, nor is there a function to do this automatically, however the Graphics class does offer the method MeasureString, though seeing as I love working with the graphics class admittedly, here is a method that will help you:

Expand|Select|Wrap|Line Numbers
  1. private void DrawWrappedString(Graphics g, Font font, string str, int maxWidth, Brush brush, PointF position, int rightMargin = 30, int lineSpacing = 0)
  2.         {
  3.             SizeF s = g.MeasureString(str, font);
  4.             string lineBuffer = "";
  5.             float lineHeight = s.Height;
  6.             float y = position.Y;
  7.             PointF linePosition = new PointF(0, 0);
  8.             foreach (char c in str)
  9.             {
  10.                 s = g.MeasureString(lineBuffer, font);
  11.                 if (s.Width >= (maxWidth - position.X - rightMargin) && c == ' ')
  12.                 { 
  13.                     linePosition = new PointF(position.X, y);
  14.                     g.DrawString(lineBuffer.Trim(), font, brush, linePosition);
  15.                     y += lineHeight + lineSpacing;
  16.                     lineBuffer = "";
  17.                 }
  18.                 lineBuffer += c.ToString();
  19.             }
  20.             linePosition = new PointF(position.X, y);
  21.             g.DrawString(lineBuffer.Trim(), font, brush, linePosition);
  22.         }
  23.  
Sep 8 '10 #2
Ahmedhussain
79 New Member
Hi,

I like the work, but need to know width parameter will take width of what?

Thank you,
Sep 8 '10 #3
Aimee Bailey
197 Recognized Expert New Member
luckily the method you are in actually has this information :) just use:

Expand|Select|Wrap|Line Numbers
  1. int width = e.PageBounds.Width;
  2.  
Aimee.
Sep 8 '10 #4
Ahmedhussain
79 New Member
hey,
Thanks for the code, but it still cuts the text from the right hand side. is there any way to stop this?
Sep 9 '10 #5
Ahmedhussain
79 New Member
I used this, it was working fine except for the e.pagebounds.width. So I divided the width by 2, And now it prints on almost half of my page.

Regards,
Ahmed
Sep 9 '10 #6
Plater
7,872 Recognized Expert Expert
I was under the impression that if you used the overload of DrawString that took a rectangle, it would wrap the text to be contained inside the rectangle.
It mentions this in the StringFormatFlags enum where you can tell it NoWrap or DirectionRightToLeft and other options
Sep 9 '10 #7
Ahmedhussain
79 New Member
Hi,
Platter, I would appreciate if you give me some code... Cause I am not getting a proper understanding of e.graphics, to print on a piece of a paper. :'(
Sep 9 '10 #8
Plater
7,872 Recognized Expert Expert
Take a look at the DrawString function:
g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);

If you supply a bounding RectangleF, instead of just a starting point, the text will be wrapped.


Here's an example showing 2 different types. The first example word wraps, but could leave a partial line (the tops of the glyphs used in xenophobia)
while the 2nd type forces only whole lines to be drawn, it character wraps. I didn't play around enough to see if you could make both happen.

Expand|Select|Wrap|Line Numbers
  1. private static void DemoBitmap()
  2. {
  3.   Bitmap bm = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
  4.   Graphics g= Graphics.FromImage(bm);
  5.  
  6.   string StringToDraw = "Don had a lot of rancid cats. The cats liked his xenophobia very much.";
  7.   Font FontToUse = new Font(FontFamily.GenericSansSerif, 10);
  8.   Brush BrushToUse = new SolidBrush(Color.Black);
  9.   int margin = 10;//10pixels
  10.   RectangleF RectangleFToUse = new RectangleF(margin, margin, bm.Width - (2 * margin), bm.Height - (2 * margin));
  11.   StringFormat StringFormatToUse = new StringFormat();//can add StringFormatFlags here 
  12.  
  13.   g.Clear(Color.White);
  14.   g.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width-1, bm.Height-1);
  15.   g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
  16.   bm.Save(@"c:\boundedbmp1.bmp", ImageFormat.Bmp );//will have a partial line shown
  17.  
  18.   g.Clear(Color.White);
  19.   g.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width-1, bm.Height-1);
  20.   StringFormatToUse.FormatFlags = StringFormatToUse.FormatFlags | StringFormatFlags.LineLimit;
  21.   g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
  22.   bm.Save(@"c:\boundedbmp2.bmp", ImageFormat.Bmp);//will not have a partial line shown, wraps on character
  23. }
  24.  
Sep 9 '10 #9
Ahmedhussain
79 New Member
Platter, It create bitmaps great.. But still tell me how to print.


Expand|Select|Wrap|Line Numbers
  1. private static void DemoBitmap(String str, Graphics g1)
  2.         {
  3.               Bitmap bm = new Bitmap(750, 1000, PixelFormat.Format32bppArgb);
  4.               Graphics g = Graphics.FromImage(bm);
  5.               g1 = Graphics.FromImage(bm);
  6.               string StringToDraw = str;//"Donhad a lot of rancid cats. The cats liked his xenophobia very much.";
  7.               Font FontToUse = new Font(FontFamily.GenericSansSerif, 10);
  8.               Brush BrushToUse = new SolidBrush(Color.DarkBlue);
  9.               int margin = 10;//10pixels
  10.               RectangleF RectangleFToUse = new RectangleF(margin, margin, bm.Width - (2 * margin), bm.Height - (2 * margin));
  11.               StringFormat StringFormatToUse = new StringFormat();//can add StringFormatFlags here 
  12.  
  13.               g1.Clear(Color.White);
  14.               g1.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width - 1, bm.Height - 1);
  15.               g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
  16.               g1.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
  17.               //bm.Save(@"d:\boundedbmp1.bmp", ImageFormat.Bmp);//will have a partialline shown
  18.  
  19.               g1.Clear(Color.White);
  20.               g1.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width - 1, bm.Height - 1);
  21.               StringFormatToUse.FormatFlags = StringFormatToUse.FormatFlags | StringFormatFlags.LineLimit;
  22.               g1.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
  23.              // bm.Save(@"d:\boundedbmp2.bmp", ImageFormat.Bmp);//will not have a partial line shown, wraps on character
  24.         }
  25.  
but it doesnt print any thing .. I get only blank paper on my print event :(
Sep 9 '10 #10
Ahmedhussain
79 New Member
What I did to print on the page properly is this.

Expand|Select|Wrap|Line Numbers
  1. void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  2.         {
  3.             siteHTML = webBrowser1.Document.Body.OuterHtml;
  4.             float linesPerPage = 0;
  5.             float leftMargin = e.MarginBounds.Left;
  6.             float topMargin = e.MarginBounds.Top;
  7.             String line = null;
  8.  
  9.             byte[] byteArray = Encoding.ASCII.GetBytes(siteHTML);
  10.             MemoryStream stream = new MemoryStream(byteArray);
  11.             StreamReader reader = new StreamReader(stream);
  12.  
  13.             e.PageSettings.Color = true;
  14.             rptFont = new Font("Arial", 10);
  15.             linesPerPage = e.MarginBounds.Height / rptFont.GetHeight(e.Graphics);
  16.  
  17.             e.PageSettings.PaperSize = new PaperSize("A4", 8, 11);
  18.             float lineHeight = rptFont.GetHeight(e.Graphics);
  19.  
  20.             int count = 0;
  21.             while (((line = reader.ReadLine()) != null)&& count<linesPerPage)
  22.             {
  23.                 Rectangle rect = new Rectangle(20, 20 + Convert.ToInt32(lineHeight * count), 800, Convert.ToInt32(lineHeight));
  24.                 e.Graphics.DrawString(line, rptFont, Brushes.BlueViolet, rect, new StringFormat());
  25.                 count++;
  26.             }
  27.         }
  28.  
But now I have to look for a next page to print it properly. If I do something like,

Expand|Select|Wrap|Line Numbers
  1. if (count > linesPerPage)
  2.                     e.HasMorePages = true;
  3.                 else
  4.                     e.HasMorePages = false;
  5.  
The code will go boom. :@
How would it be possible to print another page???
Sep 9 '10 #11
Ahmedhussain
79 New Member
Well not the code, but the count for pages go unlumited :s
Sep 9 '10 #12
Plater
7,872 Recognized Expert Expert
The bitmap was just an example. Printing works with the same Graphics object. It's just a big empty bitmap that is printed
Sep 10 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DrDevious | last post by:
Maybe I am doing something wrong but has anyone else here noticed a difference in the positioning of text between the Graphics.DrawString method and the Win32 GDI DrawText function? My text is...
0
by: Alpha | last post by:
How can I pint in Landscape when I use graphics.drawstring? Currently, I have to change my printer to Landscape to make it do so. The output is preview in a Crystal Report form that is setup as...
2
by: DazedAndConfused | last post by:
Is there a way to find the coordinates of x after using Graphics.DrawString? i.e. e.Graphics.DrawString("My Report", reportFont, reportBrush, x, y) x = the coordinates position of "t" in "My...
3
by: Gürkan Demirci | last post by:
Hi, i am trying to write Jananese characters with DrawString(). It is not working. The characters a printed as boxes only. How can i change the culture of my windows forms application ? It...
3
by: Allen | last post by:
I've got a control that you can resize the contents of one of the text fields inside it. When the contents are resized to smaller than the text, I remove some of the end of the text and...
1
by: Dom | last post by:
In a control's OnPaint, I do the following: e.Graphics.DrawString (Text, MyFont, Brushes.Red, MyRect) The purpose of the "Brushes.Red" is to highlight the word, but I would like to highlight...
2
by: Tony Johansson | last post by:
Hello! If I use the DrawString below with object of StringFormat as the last object it works good. If I instead remove object StringFormat below as the last object of DrawString I get some rows...
6
vekipeki
by: vekipeki | last post by:
I am having a problem with basic drawing of unicode characters in Windows 2000 and XP. I have written a simplest possible C# WinForms program to test it (just create a new Windows Forms C#...
6
by: Dilip | last post by:
Howdy Folks I have a display where the Graphics.DrawString function is called to display something. Since the text seems to be larger than its bounding rectangle, the call basically splits the...
3
by: arunman | last post by:
Hi, Im writing a C# .Net application to print in zebra 2030 thermal receipt printer (Uses 80 mm wide paper). I'm using the windows Graphics object for printing. string text = "Line1\n ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.