MailMerge direct to radDocument

8 posts, 0 answers
  1. LEVEFAUDE
    LEVEFAUDE avatar
    39 posts
    Member since:
    Apr 2011

    Posted 30 Oct 2012 Link to this post

    Hello

    I want to merge a radDocument in the  radDocument itself, ( Me.radRichTextBox.Document = Me.radRichTextBox.MailMerge(True)
    It works fine, but doesn't insert CRLF in each page

    I try to put a Page break

                Me.radRichTextBox.Document.InsertPageBreak()
                Me.radRichTextBox.Document = Me.radRichTextBox.MailMerge(True)

    And a Linebreak at the end of the document

                Me.radRichTextBox.Document.CaretPosition.MoveToLastPositionInDocument()
                Me.radRichTextBox.Document.InsertLineBreak()

                Me.radRichTextBox.Document = Me.radRichTextBox.MailMerge(True)

    None of the methods described above, work

    Can u help us ?

    Thank a lot

    Anne
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 30 Oct 2012 Link to this post

    Hello Anne,

    I am not quite sure what is the desired behavior, but here are my suggestions:

    1. If you wish to perform a mail merge and replace the current(template) document of the RadRichTextBox, you should do it as follows:
      Me.radRichTextBox.Document = Me.radRichTextBox.MailMerge(True)
      This should add section breaks between the separate records. You can ensure that by turning on the formatting symbols(from the RadRichTextBoxRibbonUI or from code using the ShowFormattingSymbols property). You should also know that in order for your records to look placed on different pages, the LayoutMode of your editor should be set to Paged.
    2. If you wish to insert the merged records in the same document instance that holds your template, you can do it like this:
      RadDocument merged = this.radRichTextBox.MailMerge(true);
      RadDocument document = new RadDocument();
      document.InsertFragment(new DocumentFragment(this.radRichTextBox.Document));
      document.InsertPageBreak();
      merged.Selection.SelectAll();
      DocumentFragment fragment = merged.Selection.CopySelectedDocumentElements();
      document.InsertFragment(fragment);
      merged.MeasureAndArrangeInDefaultSize();
      document.InsertFragment(new DocumentFragment(merged));
      this.radRichTextBox.Document = document;
    Once again, the LayoutMode should be set to Paged.

    I hope this answers your question. If not, please get back to us with additional information or a sample project, demonstrating the issue.

    Kind regards,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. doneill
    doneill avatar
    3 posts
    Member since:
    Sep 2012

    Posted 20 Jan 2013 Link to this post

    I am doing multiple merges combining the output of each merge into a single document using the following code:

    RadDocument templateDocument = OpenRadDocument(templateFile);
    templateDocument.MailMergeDataSource.ItemsSource = mergeData;
     
    RadDocument mergedDocument = templateDocument.MailMerge(true);
     
    if (combinedMergeDocument == null)
    {
        combinedMergeDocument = new RadDocument();
        combinedMergeDocument.LayoutMode = DocumentLayoutMode.Paged;
    }
     
    combinedMergeDocument.CaretPosition.MoveToLastPositionInDocument();         
     
    if(!combinedMergeDocument.IsEmpty)
        combinedMergeDocument.InsertPageBreak();
     
    combinedMergeDocument.InsertFragment(new DocumentFragment(mergedDocument));
    combinedMergeDocument.MeasureAndArrangeInDefaultSize();


    When I do the merge, the resulting merged document contains a single page for each record which is perfect.  The problem is that once I insert the merged document into the combined document, the original paging is gone and the records are now split over pages. The merge document contains 10 pages but the combined document contains 6 pages because it has started it's own paging ignoring the paging from the source document. 

    If I don't set the LayoutMode of the combined document, I end up with one page.

    How can I maintain the original paging when inserting into the combined document ?  

    If required, I wouldn't even mind adding the pages to the combined document one by one but there doesn't seem to be document pages collection exposed that I could use to select page by page.

    Thanks
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 23 Jan 2013 Link to this post

    Hello,

    The issue you are facing seems related to the method you are using in order to create DocumentFragment. Basically, the two approaches shown in my previous post differ in the way the properties of the sections are copied when creating the document fragment instance. The first approach - using the selection, copies all properties of the sections and the page/section breaks are one of them, as opposed to the second approach (which is also the one used in your snippet).

    So, in order to make your project work as per your requirments I suggest you create the DocumentFragments using the other approach - through selection. 
    mergedDocument.MeasureAndArrangeInDefaultSize();
    mergedDocument.Selection.SelectAll();
    DocumentFragment fragment = mergedDocument.Selection.CopySelectedDocumentElements();
    combinedMergeDocument.InsertFragment(fragment);
    I am also attaching the project I used for testing for your reference.

    When it comes to your remark that if you do not set the LayoutMode of the document to Paged, the result is a single page, this is due to the fact that the default LayoutMode is Flow. If you want to ensure that the document would be in paged mode, you should keep the property as it is.

    As for a pages collection of some sort, RadDocument's element hierarchy does not contain the concept of pages. Pages are simply a representative manner and depend on the layout mode. However, in the way that the project attached is implemented, the separate mail-merge entries are actually Sections, so if you need to access them, you can do so through the appropriate API.

    I hope this information is helpful! Let us know if you are experiencing other problems.
     
    Regards,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Krithiga
    Krithiga avatar
    4 posts
    Member since:
    Aug 2014

    Posted 11 Sep 2014 in reply to Petya Link to this post

    Hi 

    I also want to merge a existind document with having tables inside , I tried to do a mail merge , but ends with an error.
    Unable to cast object of type Telerik.Window.Documents.Layout.TableLayOutBox to type Telerik.Windows.Documents.Layout.InlineLayoutBox . Please help me out to solve this issue , Here is my sample code also

     Table table = new Table();


                Span span = new Span("Test 123");
                Paragraph paragraph = new Paragraph();
                paragraph.Inlines.Add(span);

                TableCell cell = new TableCell();
                cell.Blocks.Add(paragraph);

                TableRow row = new TableRow();
                row.Cells.Add(cell);

                table.AddRow(row);

                

                Section section = new Section();
                section.Blocks.Add(table);
                
                RadDocument document = new RadDocument();
                document.Sections.Add(section);

                document.MeasureAndArrangeInDefaultSize();
                
                return new DocumentFragment(document);










  7. Petya
    Admin
    Petya avatar
    975 posts

    Posted 16 Sep 2014 Link to this post

    Hello Krithiga,

    Tables in RadDocument cannot be the first or last element in a section. This limitation of the document model is most likely the cause of the issue you are observing. Please make sure to add paragraphs before and after your table. If you are still experiencing problems, try to provide more details on your scenario.

    I hope this is useful.

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

    Posted 16 Sep 2014 in reply to Petya Link to this post

    Hello Petya

    Thank you so much , I solved this issue , that time i used  this.View.radRichTextBox.UpdateAllFields(FieldDisplayMode.Result) but now i changed that to this.View.radRichTextBox.UpdateAllFields(FieldDisplayMode.DisplayName) now it is working perfectly.

    But I am facing some other problem , I want to add custom field to the merged document , so if user drag and drop cutom fileds into it , But I dont want to save the field tags in the merged document , So Instead using InsertField, I want to place  the document Fragment from GetResultFragment directly to the document with the help of insert fragment . How can i achieve this please let me know any work around for this.
  9. Petya
    Admin
    Petya avatar
    975 posts

    Posted 19 Sep 2014 Link to this post

    Hello Krithiga,

    RadRichTextBox exposes an InsertFragment() method which allows you to insert a DocumentFragment instance at the caret position. That being said, if you can obtain or create the desired result fragment, you can insert it without adding a custom field to the document.

    Regards,
    Petya
    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