How to have different headers for sections in a radRichTextBox

8 posts, 0 answers
  1. Media
    Media avatar
    10 posts
    Member since:
    Sep 2016

    Posted 06 Oct Link to this post

    Hi

    I have a richtextbox and I want each section has its own header. In the following code I have set the header but all the sections have the same header.

    I have three question

    1) How can I have different header for each section.

    2) How can I have access to the selected section?

    3) How can I access to other sections. Because there is no index for collection sections in the Document.

    I will appreciate if someone answers. Thanks in advance.

                dd.Section section = new dd.Section();
                dd.Paragraph paragraph = new dd.Paragraph();
                dd.Span span = new dd.Span(this.txtHeader.Text);
                
                paragraph.Inlines.Add(span);
                section.Blocks.Add(paragraph);
                dd.RadDocument document = new dd.RadDocument();
                document.Sections.Add(section);

                dd.Header header = new dd.Header();
                header.Body = document;
                this.radRichTextBox.UpdateHeader(this.radRichTextBox.Document.Sections.First, dd.HeaderFooterType.Default, header);

  2. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 11 Oct Link to this post

    Hello Media,

    By default, the sections are inheriting its header and footer from the previous section. You can change this behavior by setting the IsHeaderLinkedToPrevious and IsFooterLinkedToPrevious properties of the Section.

    The section that the caret is currently at could be obtained through the methods of the CaretPosition:
    Section currentSection = this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection;

    To obtain all the sections in a document, you can use the EnumerateChildrenOfType() method to get a collection of them:
    IEnumerable<Section> sections = this.radRichTextBox.Document.EnumerateChildrenOfType<Section>();

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Media
    Media avatar
    10 posts
    Member since:
    Sep 2016

    Posted 11 Oct in reply to Tanya Link to this post

    Thanks. Actually about my first question. Look, according to your documents each section can have its own header. Now in my application I have two sections and I want each have different header. What should I do? I used your method but they are still the same. What should I do? I really demand this functionality but I can not achieve that. Thanks :)
  5. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 14 Oct Link to this post

    Hello Media,

    In order to add a header to a section you can use couple approaches:
    • Use the Headers property of the Section class.
      section1.Headers.First = CreateHeader("Section 1"); // where CreateHeader() pseudo method generates a Header object with a single inline text element showing the header
      section2.Headers.First = CreateHeader("Section 2");
    • Use the UpdateHeader() method of RadRichTextBox
      this.radRichTextBox.ChangeSectionHeaderLinkToPrevious(section1, HeaderFooterType.First, false);
      this.radRichTextBox.ChangeSectionHeaderLinkToPrevious(section2, HeaderFooterType.First, false);
      this.radRichTextBox.UpdateHeader(section1, HeaderFooterType.First, CreateHeader("Section 1"));
      this.radRichTextBox.UpdateHeader(section2, HeaderFooterType.First, CreateHeader("Section 2"));

    When you use the UpdateHeader() method there is a small catch - you will need to manually tell the section not to link its header to the previous header. As demonstrated in the previous code snippet you can use the ChangeSectionHeaderLinkToPrevious() method to do this. Otherwise, the headers might not be displayed as you expect. Please try the method on your side and let me know if it helps. You can also take a look at the attached project.

    Regards,
    Martin
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Media
    Media avatar
    10 posts
    Member since:
    Sep 2016

    Posted 15 Oct in reply to Martin Link to this post

    Hi again and thanks for your reply. It is exactly one month that I am trying to make this program but still not working. Look, I have the following code:

    dd.Section section = new dd.Section();
    dd.Paragraph paragraph = new dd.Paragraph();
    dd.Span span = new dd.Span(this.txtHeader.Text);

    paragraph.Inlines.Add(span);
    section.Blocks.Add(paragraph);
    dd.RadDocument document = new dd.RadDocument();
    document.Sections.Add(section);

    dd.Header header = new dd.Header();
    header.Body = document;
    this.radRichTextBox.ChangeSectionHeaderLinkToPrevious                           (this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection, dd.HeaderFooterType.First, false);
    this.radRichTextBox.UpdateHeader                (this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection, dd.HeaderFooterType.First, header);

    I want to make the program that whenever the user's cursor is in one section, he has the ability to change that section's header which the text of that is inside a textbox. I did your recommended way. I really do not know why it is not working.

    Thanks a lot.

  7. Media
    Media avatar
    10 posts
    Member since:
    Sep 2016

    Posted 15 Oct in reply to Media Link to this post

    Also the attachment can not be downloaded
  8. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 19 Oct Link to this post

    Hello Media,

    We experienced a technical issue with the attachments in the forums. It is now fixed and you should be able to download the archive.

    I checked your code and noticed that you are setting only the first-page header of the section. To be respected this header and added to the document, you should first set the HasDifferentFirstPageHeaderFooter property to true:
    Header header = new Header();
    header.Body = document;

    this.radRichTextBox.ChangeSectionHeaderLinkToPrevious(this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection, HeaderFooterType.First, false);
    this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection.HasDifferentFirstPageHeaderFooter = true;
    this.radRichTextBox.UpdateHeader(this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection, HeaderFooterType.First, header);

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  9. Media
    Media avatar
    10 posts
    Member since:
    Sep 2016

    Posted 20 Oct in reply to Tanya Link to this post

    It did the job, thanks thanks thanks ...
Back to Top
UI for WPF is Visual Studio 2017 Ready