Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have developed an application to create, edit and print labels. In this application it is possible to insert rectangles in which there are several lines of text and the user can set the font, the color,.., and the orientation among 0°, 90°, 270° and 180° degrees. So I wrote a method to draw the text inside the bounding rectangle, according to the orientation, using GDI+ and DrawString method.
C#
switch (RotationAngle)
            {
                case 90:
                    g.RotateTransform(90);
                    objRec = DrawRectangle.GetNormalizedRectangle(objRec.Top, 
                          -objRec.Left, objRec.Bottom, -objRec.Right);
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    g.RotateTransform(-90);
                    break;
                case 180:
                    g.RotateTransform(180);
                    objRec = DrawRectangle.GetNormalizedRectangle(-objRec.Left, 
                          -objRec.Top, -objRec.Right, -objRec.Bottom); 
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    g.RotateTransform(-180);
                    break;
                case 270:
                    g.RotateTransform(270);
                    objRec = DrawRectangle.GetNormalizedRectangle(-objRec.Top, 
                        +objRec.Left, -objRec.Bottom, +objRec.Right);
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    g.RotateTransform(-270);
                    break;
                case 0:
                    g.DrawString(lines[i], textFont, brush, objRec, sf);
                    break;
            }


where "objRect" is the original rectangle set by the user. The method works for all the orientation, but not for 180°! In this case the text is drawn as at 0 degrees. I really don't know what to do and I'm going crazy in finding the correct way to solve the problem. What is the correct way to draw text in different rotation? Any help would be pleasant. Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 21-Oct-13 10:55am    
Weird. You are using the best method, only the code is no good. You should not use the case, use the RotationAngle and -RotationAngle instead. 1) Rewrite the code, 2) use "Improve question" and change it in the question, 3) for experiment, try not 180, but, say 181, 4) run it all under the debugger.

Did you try to create new Matrix variable and assign Transform property to it, instead of rotating back and forth?

Don't even play with the idea of leaving the code as is. Refactor it properly first, and then, it you do, we can discuss the problem if it is still there...

—SA

1 solution

A quick check with your code in the paint event of a panel gives we the 180 working fine - but I can't use your DrawRectangle.GetNormalizedRectangle method because I don't have it, so I used TranslateTransform instead:
C#
g.TranslateTransform(myDrawingPanel.Width, myDrawingPanel.Height);
g.RotateTransform(180);
//objRec = DrawRectangle.GetNormalizedRectangle(-objRec.Left,
//      -objRec.Top, -objRec.Right, -objRec.Bottom);
g.DrawRectangle(Pens.Red, objRect);
g.DrawString(lines[i], textFont, brush, objRec, sf);
g.RotateTransform(-180);

That worked fine - the text was upside down and right to left, as I would expect.
I would suggest that you want to look at your method - it may be that it is causing the problem. It could also be worth looking at your StringFormat settings - again I don't have those.


"The graphics used in this method is the Graphics object of a PrintPageEventArgs; it seems that this method doesn't work for 180° just if I select a pdf virtual printer. In all other cases it works fine and produce the result expected. Only for pdf printers and orientation 180° degrees, the instruction g.RotateTransform(-180) seems to be ignored at all! I know that it seems weird, but I can't find a solution!"

I just tried it with mine:
C#
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    pd.Print();
    }

void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
    Graphics g = e.Graphics;
    RectangleF objRec = new RectangleF(50, 50, 100, 75);
    g.TranslateTransform(myDrawingPanel.Width, myDrawingPanel.Height);
    g.RotateTransform(180);
    g.DrawRectangle(Pens.Red, new Rectangle(50, 50, 100, 75));
    g.DrawString("Hello", font, Brushes.Blue, new RectangleF(50, 50, 100, 75), new StringFormat());
    g.RotateTransform(-180);
    }
Using CutePDF as the printer driver and it worked fine - I get the text in blue, upside down, inside a red box.

Which PDF system are you using?
 
Share this answer
 
v2
Comments
Sara Noemi 22-Oct-13 5:46am    
The graphics used in this method is the Graphics object of a PrintPageEventArgs; it seems that this method doesn't work for 180° just if I select a pdf virtual printer. In all other cases it works fine and produce the result expected. Only for pdf printers and orientation 180° degrees, the instruction g.RotateTransform(-180) seems to be ignored at all! I know that it seems weird, but I can't find a solution!
OriginalGriff 22-Oct-13 6:05am    
Answer undated
Sara Noemi 22-Oct-13 6:22am    
I'm using PDF24 or BioPDF, for both it doesn't work. While for Microsoft XPS or One Note it works fine.
OriginalGriff 22-Oct-13 6:31am    
Sounds like a bug in the pair of 'em - have you tried installing CutePDF and seeing if it works? (XPS worked fine as well - it was the first thing I tried since I hadn't installed a PDF writer since my last re-format)
Sara Noemi 22-Oct-13 6:50am    
First of all thanks a lot for your reply! What do you mean with "a bug in the pair of 'em"? I've tried with CutePDF and it works perfectly as it is with the XPS! But really I can't find an explanation for this different behaviour according to the different PDF printer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900