Replace text in header/footer

7 posts, 0 answers
  1. Aaron
    Aaron avatar
    28 posts
    Member since:
    May 2012

    Posted 28 Aug 2013 Link to this post

    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?
  2. Deyan
    Admin
    Deyan avatar
    135 posts

    Posted 30 Aug 2013 Link to this post

    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 >>
  3. DevCraft banner
  4. Daniela
    Daniela avatar
    21 posts
    Member since:
    Jun 2012

    Posted 11 Sep 2013 Link to this post

    Hi!
    Thank you for your reply. I'm a colleague of Aaron and I implemented your proposal. It worked. Thank you!
  5. ayyavoo
    ayyavoo avatar
    1 posts
    Member since:
    Feb 2014

    Posted 18 Feb 2014 in reply to Aaron Link to this post

    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();
                }
  6. Yancho
    Admin
    Yancho avatar
    72 posts

    Posted 21 Feb 2014 Link to this post

    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
  7. Krithiga
    Krithiga avatar
    4 posts
    Member since:
    Aug 2014

    Posted 26 Sep 2014 in reply to Yancho Link to this post

    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.
  8. Boby
    Admin
    Boby avatar
    595 posts

    Posted 01 Oct 2014 Link to this post

    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.
     
Back to Top
DevCraft banner