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

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

8 Answers 121 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Pavel
Top achievements
Rank 1
Pavel asked on 28 Nov 2016, 09:15 PM

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?

8 Answers, 1 is accepted

Sort by
0
Pavel
Top achievements
Rank 1
answered on 28 Nov 2016, 09:30 PM

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.

0
Pavel
Top achievements
Rank 1
answered on 28 Nov 2016, 10:14 PM

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());
    }
}
0
Hristo
Telerik team
answered on 29 Nov 2016, 03:40 PM
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.
0
Pavel
Top achievements
Rank 1
answered on 29 Nov 2016, 05:15 PM

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.

0
Pavel
Top achievements
Rank 1
answered on 29 Nov 2016, 05:46 PM

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();
    }
}
0
Accepted
Hristo
Telerik team
answered on 30 Nov 2016, 11:25 AM
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.
0
Pavel
Top achievements
Rank 1
answered on 30 Nov 2016, 03:10 PM

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);
}
0
Hristo
Telerik team
answered on 01 Dec 2016, 02:01 PM
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.
Tags
RichTextEditor
Asked by
Pavel
Top achievements
Rank 1
Answers by
Pavel
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or