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

Replace text in header/footer

6 Answers 82 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 28 Aug 2013, 08:39 AM
In our application we give the functionality to export some content to a docx file. Therefore we created a docx file to use as a template which is imported as described below to create a RadDocument:

IDocumentFormatProvider provider = new DocxFormatProvider();
StreamResourceInfo info = Application.GetResourceStream(pathToFile, UriKind.RelativeOrAbsolute));
using (Stream stream = info.Stream)
 {
    import word template
    document = provider.Import(stream);
 }

This works fine. Additionally the template contains a footer with some predefined text which we want to be replaced on the export process. According to this thread we implemented a method to find and replace content of a RadDocument:

DocumentTextSearch documentTextSearch = new DocumentTextSearch(document);
List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
string searchString = Regex.Escape(REPLACEMENT_STRING_DATE);
foreach (var textRange in documentTextSearch.FindAll(searchString))
{
    TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
     rangesTrackingDocumentChanges.Add(newRange);
}
  
foreach (var textRange in rangesTrackingDocumentChanges)
{
    SpanLayoutBox currentSpanBox = document.CaretPosition.GetCurrentSpanBox();
    Span currentSpan;
     if (currentSpanBox == null)
     {
            currentSpan = new Span();
     }
     else
     {
           currentSpan = currentSpanBox.AssociatedSpan;
     }
     StyleDefinition style = currentSpan.ExtractStyleFromProperties();
 
     document.CaretPosition.MoveToPosition(textRange.StartPosition);
     document.DeleteRange(textRange.StartPosition, textRange.EndPosition);
  
     document.Insert(DateTime.Now.Date.ToShortDateString(), style);
  
     textRange.StartPosition.Dispose();
     textRange.EndPosition.Dispose();
}

This works fine for content defined in the main text section.
But it does not work if the text to be replaced is part of the header or footer. It seems the DocumentTextSearch does not find strings inside headers or footers.

Is there some other way to replace text in headers or footers?

6 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 30 Aug 2013, 03:29 PM
Hello Aaron,

Thank you for contacting us about this issue!

Headers and Footers (as well as Footnotes, Endnotes and Comments) are actually represented by separate RadRichTextBox's and respectively have their own RadDocument. In order to find/replace in headers and footers you need to pass the header/footer RadDocument body in your FindAndReplace method. The following code snippet shows how exactly you can achieve that:

private void ReplaceInHeaderAndFooter(string toSearch, string toReplaceWith)
{
    RadDocument headerDocument = this.radRichTextBox.Document.Sections.First.Headers.Default.Body;
    this.ReplaceAllMatches(headerDocument, toSearch, toReplaceWith);
 
    RadDocument footerDocument = this.radRichTextBox.Document.Sections.Last.Footers.Default.Body;
    this.ReplaceAllMatches(footerDocument, toSearch, toReplaceWith);
}

I hope this helps! If you have any other questions please do not hesitate to contact us again!

Regards,
Deyan
the Telerik team
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Daniela
Top achievements
Rank 1
answered on 11 Sep 2013, 08:48 AM
Hi!
Thank you for your reply. I'm a colleague of Aaron and I implemented your proposal. It worked. Thank you!
0
ayyavoo
Top achievements
Rank 1
answered on 18 Feb 2014, 12:24 PM
Hi  I am new to telerix controls, I have tried to find and replace functinality in richtextbox by using the below code. i have tried to replace " with ' and it replaced only the begining words only. how to implement it for all the chars in the controls.Thanks in advance.

 DocumentTextSearch search = new DocumentTextSearch(this.Document.Document);
            List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
            foreach (var textRange in search.FindAll(toSearch))
            {
                TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
                rangesTrackingDocumentChanges.Add(newRange);
            }
            foreach (var textRange in rangesTrackingDocumentChanges)
            {
                this.Document.Document.Selection.AddSelectionStart(textRange.StartPosition);
                this.Document.Document.Selection.AddSelectionEnd(textRange.EndPosition);
                this.Document.Insert(toReplaceWith);
                textRange.StartPosition.Dispose();
                textRange.EndPosition.Dispose();
            }
0
Missing User
answered on 21 Feb 2014, 11:34 AM
Hi Ayyavoo,

Thank you for your interest in Telerik RadRichTextBox!

Please take a look at the code-snippet below! It illustrates how to search and replace a specific string in the Header of the document. All occurrences of the given string are replaced, it doesn’t matter whether they are in the begging or in the end of the word. Whole words are also replaced.
RadDocumentEditor headerDocument = new RadDocumentEditor(this.editor.Document.Sections.First.Headers.Default.Body);
 
DocumentTextSearch search = new DocumentTextSearch(headerDocument.Document);
List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
foreach (var textRange in search.FindAll("with"))
{
    TextRange newRange = new TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
    rangesTrackingDocumentChanges.Add(newRange);
}
foreach (var textRange in rangesTrackingDocumentChanges)
{
    headerDocument.Document.CaretPosition.MoveToPosition(textRange.StartPosition);
 
    headerDocument.Document.Selection.AddSelectionStart(textRange.StartPosition);
    headerDocument.Document.Selection.AddSelectionEnd(textRange.EndPosition);
    headerDocument.Delete(false);
    headerDocument.InsertInline(new Span("ok"));
 
    textRange.StartPosition.Dispose();
    textRange.EndPosition.Dispose();
}
 
Header header = new Header() { Body = headerDocument.Document };
this.editor.UpdateHeader(this.editor.Document.Sections.First, HeaderFooterType.Default, header);

Also, please take a notice that part of the API of RadDocument is marked as obsolete.

I hope this helps! Let me know if you have other comments or questions.

Regards,
Yancho
Telerik
0
Krithiga
Top achievements
Rank 1
answered on 26 Sep 2014, 09:17 AM
Hi 

I am trying to remove the header and footer only from the last page of the rich text box document , Can you please guid me how i can achieve this , If i override the UILayersBuilder , then it removes the header and footer from the whole document bu i want to remove only from the last page of my document.
0
Boby
Telerik team
answered on 01 Oct 2014, 07:20 AM
Hi Krithiga,
Every Section in the document has up to three different pairs of headers/footers - one for the first page, one for even and one for odd pages. You can read more on how to change them in Headers and Footers help article. Removing the UI layer, on the other side, will stop all headers/footer from visualization.

That said, it's not possible to directly remove/stop from visualizing only the last header/footer without changes to the document including splitting it to new sections.

Regards,
Boby
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
RichTextBox
Asked by
Aaron
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Daniela
Top achievements
Rank 1
ayyavoo
Top achievements
Rank 1
Missing User
Krithiga
Top achievements
Rank 1
Boby
Telerik team
Share this question
or