Printing header RTL

4 posts, 0 answers
  1. ShareDocs
    ShareDocs avatar
    91 posts
    Member since:
    Oct 2012

    Posted 07 Apr 2013 Link to this post

    Hi,
    is there a way to make the right header in the RadPrintDocument right aligned so if i have mixed language text (hebrew and english in my case) It will show it right...

    currently i use this function (partly taken from this site - c-sharp-split-and-revers-sentence-with-two-languages):
    public static string ProcessEnglishHebrewSentence(string sentence)
            {
                var ret = new List<string>();
                string[] words = sentence.Split(' ');
     
                var curHebrewList = new List<string>();
                var curEnglishList = new List<string>();
                bool curLangIsHebrew = false;
                string w;
                string reversed;
     
                for (int i = words.Length; i > 0; i--)
                {
                    w = words[i - 1];
                    if (System.Text.RegularExpressions.Regex.IsMatch(w, @"\p{IsHebrew}") && curLangIsHebrew) // we have a word in Hebrew and the last word was in Hebrew too
                    {
                        curHebrewList.Add(w);
                    }
                    else if (System.Text.RegularExpressions.Regex.IsMatch(w, @"\p{IsHebrew}") && !curLangIsHebrew) // we have a word in Hebrew and the last word was in English
                    {
                        if (curEnglishList.Any())
                        {
                            curEnglishList.Reverse();
                            ret.AddRange(curEnglishList);
                        } // reverse current list of English words and add to List
                        curEnglishList = new List<string>(); // create a new empty list for the next series of English words
                        curHebrewList.Add(w);
                        curLangIsHebrew = true; // set current language to Hebrew
                    }
                    else if (!System.Text.RegularExpressions.Regex.IsMatch(w, @"\p{IsHebrew}") && !curLangIsHebrew) // we have a word in English and the last word was in English
                    {
                        if (System.Text.RegularExpressions.Regex.IsMatch(w, @"[a-zA-Z]"))
                        {
                            reversed = new string(w.Reverse().ToArray());
                            reversed = reversed.Replace("(", "^");
                            reversed = reversed.Replace(")", "(");
                            reversed = reversed.Replace("^", ")");
                        }
                        else
                            reversed = w;
     
     
                        curEnglishList.Add(reversed); // reverse and add it to the current series of English words
                    }
                    else if (!System.Text.RegularExpressions.Regex.IsMatch(w, @"\p{IsHebrew}") && curLangIsHebrew) // we have a word in English and the last word was in Hebrew
                    {
                        if (curHebrewList.Any())
                        {
                            curHebrewList.Reverse();
                            ret.AddRange(curHebrewList); // add current list of Hebrew words to List of Lists
                        }
                        curHebrewList = new List<string>(); // create a new empty list for the next series of Hebrew words
     
                        if (System.Text.RegularExpressions.Regex.IsMatch(w, @"[a-zA-Z]"))
                        {
                            reversed = new string(w.Reverse().ToArray());
                            reversed = reversed.Replace("(", "^");
                            reversed = reversed.Replace(")", "(");
                            reversed = reversed.Replace("^", ")");
                        }
                        else
                            reversed = w;
     
                        curEnglishList.Add(reversed);
                        curLangIsHebrew = false; // set current language to English
                    }
                    else
                    {
                        throw new Exception("there should be no other case...");
                    }
                }
                if (curHebrewList.Any())
                {
                    curHebrewList.Reverse();
                    ret.AddRange(curHebrewList);
                }
                if (curEnglishList.Any())
                {
                    curEnglishList.Reverse();
                    ret.AddRange(curEnglishList);
                }
     
                return ret.Aggregate((a, b) => a + " " + b);
            }

    I would like to know if there is a more elegant sollution
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    717 posts

    Posted 10 Apr 2013 Link to this post

    Hello Lior,

    Thank you for writing.

    The headers of a RadPrintDocument are three strings that are drawn in the same rectangle with alignment Left, Center and Right. I would need to see how you expect the text to be drawn and how it is actually drawn so I can advise as to how to approach this.

    Looking forward to your reply.

    All the best,
    Ivan Petrov
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. ShareDocs
    ShareDocs avatar
    91 posts
    Member since:
    Oct 2012

    Posted 02 May 2013 Link to this post

    Hi,
    the string variable contain:
    לקרן: אי.בי.אי. (0B) (!)  אג"ח ללא מניות

    and I want it to look like
    אג"ח ללא מניות (B0) (!)  לקרן: אי.בי.אי.

    The problem is when i combine hebrew and english...
    when I copy the sentence to a text editor than the upper sentence is when i left align and the lower sentence is when i right aligned...
  4. Ivan Petrov
    Admin
    Ivan Petrov avatar
    717 posts

    Posted 07 May 2013 Link to this post

    Hi Lior,

    Thank you for writing back.

    You can print the header yourself using RadPrintDocument's PrintPage event. Here is how the document is set up:
    RadPrintDocument doc = new RadPrintDocument();
    doc.PrintPage += doc_PrintPage;
    doc.AssociatedObject = this.radGridView1;
    doc.RightHeader = "לקרן: אי.בי.אי. (0B) (!)  אגח ללא מניות";
    RadPrintPreviewDialog dialog = new RadPrintPreviewDialog(doc);

    And here is the code to print the right header in RTL:
    private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        RadPrintDocument doc = (RadPrintDocument)sender;
        SizeF size = e.Graphics.MeasureString(doc.RightHeader, doc.HeaderFont);
        PointF location = new PointF(e.MarginBounds.Right - size.Width, e.MarginBounds.Top + (doc.HeaderHeight - size.Height) / 2);
         
        RadGdiGraphics gr = new RadGdiGraphics(e.Graphics);
        TextParams tp = new TextParams();
        tp.alignment = ContentAlignment.MiddleRight;
        tp.rightToLeft = true;
        tp.text = doc.RightHeader;
        tp.font = doc.HeaderFont;
        tp.foreColor = Color.Black;
        tp.paintingRectangle = new RectangleF(location, size);
     
        gr.FillRectangle(tp.paintingRectangle, Color.White);
        gr.DrawString(tp, size);
    }

    The gr.FillRectangle is to hide the default header printing.

    I hope this will be useful. Should you have further questions, I would be glad to help.

    Greetings,
    Ivan Petrov
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Back to Top