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
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
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
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.
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