This is a migrated thread and some comments may be shown as answers.

How to have different headers for sections in a radRichTextBox

7 Answers 201 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Media
Top achievements
Rank 1
Media asked on 06 Oct 2016, 05:25 PM

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);

7 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 11 Oct 2016, 03:40 PM
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.
0
Media
Top achievements
Rank 1
answered on 11 Oct 2016, 07:03 PM
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 :)
0
Martin Ivanov
Telerik team
answered on 14 Oct 2016, 12:50 PM
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.
0
Media
Top achievements
Rank 1
answered on 15 Oct 2016, 07:13 PM

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.

0
Media
Top achievements
Rank 1
answered on 15 Oct 2016, 07:16 PM
Also the attachment can not be downloaded
0
Tanya
Telerik team
answered on 19 Oct 2016, 12:39 PM
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!
0
Media
Top achievements
Rank 1
answered on 20 Oct 2016, 10:11 AM
It did the job, thanks thanks thanks ...
Tags
RichTextBox
Asked by
Media
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Media
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or