I want to be able to create templates in the UI, and then export PDFs using mailmerge. Are there any good examples?

9 posts, 1 answers
  1. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 28 Nov 2016 Link to this post

    I want to give application admins to create new templates. Something like

    Hello <LastName>, <FirstName>. You owe us <AmountOwed>. Please mail a check to <Address>. 

    I spend some time research, and it appears that it's doable using RichTextEditor and MailMerge. However, I can't seem to find an ability for the user to actually create a template through a GUI. What am I missing?

  2. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 28 Nov 2016 Link to this post

    Looks like my google fu got stronger, and I found exactly what I needed http://docs.telerik.com/devtools/winforms/richtexteditor/features/mail-merge

    However, it's spitting out a single document. Is there a way to get it to generate multiple documents without just setting a source to a list with a single item? I feel like there's gotta be a flag that can do this. I'd also prefer to have more control over a process. I want to give the application a folder path, and then using the data source, create multiple documents with unique names.

  3. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 28 Nov 2016 Link to this post

    Well, I managed to figure it out, and it turns out to be quite simple. If there's a better way of doing this, I'd love to hear it, but my code is below.

    radRichTextEditor1.Document.MailMergeDataSource.MoveToFirst();
    using (var folderDialog = new FolderBrowserDialog())
    {
        folderDialog.ShowNewFolderButton = true;
        if (folderDialog.ShowDialog() == DialogResult.OK)
        {
            var provider = new DocxFormatProvider();
     
            do
            {
                var item = (Employee) radRichTextEditor1.Document.MailMergeDataSource.CurrentItem;
     
                var d = this.radRichTextEditor1.Document.MailMergeCurrentRecord();
                using (Stream output = File.Create(folderDialog.SelectedPath + "\\" + item.FirstName + ".docx"))
                {
                    provider.Export(d, output);
                }
     
            } while (radRichTextEditor1.Document.MailMergeDataSource.MoveToNext());
        }
    }
  4. Hristo
    Admin
    Hristo avatar
    1508 posts

    Posted 29 Nov 2016 Link to this post

    Hi Pavel,

    Thank you for writing.

    I confirm that the approach you are employing is valid. I can only add that if you need to update the control and its UI while performing the operation you can call the UpdateAllFields method of the editor with a FieldDisplayMode.Result flag or use preview the results as shown here: http://docs.telerik.com/devtools/winforms/richtexteditor/features/mail-merge#previewing-the-results-of-mail-merge.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
  5. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 29 Nov 2016 in reply to Hristo Link to this post

    Thank you. I do have an additonal question. Is there a way to override the "Save" button behavior?

    I want to be able to save the document to SQL database, and then be able to load it.

  6. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 29 Nov 2016 in reply to Pavel Link to this post

    I think I am close. I used the code found in the Remove Controls from Rich Text Editor Ribbon Bar thread and I can get get my custom even to fire. However, the save dialog still comes up. How do I suppress it?

    public partial class Letters : RadRibbonForm
    {
        public Letters()
        {
            InitializeComponent();
            var ribbonUI = new CustomRichTextEditorRibbonBar();
            ribbonUI.AssociatedRichTextEditor = radRichTextEditor1;
            this.Controls.Add(ribbonUI);
        }
    }
    public class CustomRichTextEditorRibbonBar : RichTextEditorRibbonBar
    {
        protected override void Initialize()
        {
            base.Initialize();
            this.buttonSave.Click += ButtonSave_Click1;
        }
     
        private void ButtonSave_Click1(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
  7. Answer
    Hristo
    Admin
    Hristo avatar
    1508 posts

    Posted 30 Nov 2016 Link to this post

    Hello Pavel,

    Thank you for writing.

    Using the suggested approach is valid, however, as you have already observed this will not prevent executing the built-in logic handling the save operation.

    The method responsible for performing the save command is also virtual and you can override it. This way you will not need to create a new event handler and the default behavior will be overridden as well: 
    public class CustomRichTextEditorRibbonBar : RichTextEditorRibbonBar
    {
        protected override void BackstageButtonSave_Click(object sender, EventArgs e)
        {
            if (this.IsDesignMode || this.AssociatedRichTextEditor == null)
            {
                return;
            }
     
            //Your custom logic...
        }
    }

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
  8. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 30 Nov 2016 in reply to Hristo Link to this post

    Thank you, that did it. Well, almost, I needed to override the small save icon in the top right corner, so I just had to use a different method

    protected override void ButtonSave_Click(object sender, EventArgs e)
    {
        SaveClicked?.Invoke(this, EventArgs.Empty);
    }
  9. Hristo
    Admin
    Hristo avatar
    1508 posts

    Posted 01 Dec 2016 Link to this post

    Hello Pavel,

    Thank you for writing back.

    I am glad that you have managed to achieve the desired result.

    Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
Back to Top