Signature to Word

Hi,

do you have a working example of getting a signature (previously stored in a database as a Base64 string - canvas toDataURL) and inserting it in a Word document (preferably in a bookmark field)



3 Replies

MJ Mohanaselvam Jothi Syncfusion Team March 6, 2018 11:59 AM UTC

Hi Pratura,

Thank you for contacting Syncfusion support.

To achieve your requirement, we have prepared a sample application to get the signature image as Base64String and inserts into the Word document using DocIO.

In this sample we have done the following things:
1. Loads an template Word document using DocIO.
2. Access to the bookmark in the Word document using
BookmarksNavigator.
3. Gets the image data as Base64String.
4. Inserts
WPicture into the bookmark content.
5. Converts the Base64String to image and loads into
WPicture.
6. Saves the Word document.

Please find the sample form the below link and let us know if this helps you:
http://www.syncfusion.com/downloads/support/forum/136243/ze/SampleApplication-1537410409.zip

From the given details, we suspect that datatype - Base64String is used in your database to store image bytes of the signature. If so, then it is feasible to create image from  Base64String and insert into the Word document using DocIO.

Please refer the below UG documentation link to know more about inserting content into bookmark using DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks#inserting-content-into-a-bookmark

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 



PR Pratura March 7, 2018 02:56 PM UTC

Hi Mohanaselvam,

that works great.

But when implemented in my project i have the following problem:

In your example, you call the button submit on Index action.

I call it on a different action, and that seems to cause the Sample.docx not to be generated.

There are no errors, my controller action gets called and populates the base64 string with the correct value from the database (using dapper)

But nothing happens on return. 
There is no file generated.

The same code, when placed on Index action works correctly (without the parameter in Index() and with hard-coded id.

This is the JS:
 $("#GridMaster").ejGrid({
            recordClick: function (args) {
                if (args.cellIndex === 0) {
                    $.ajax({
                        contentType: 'application/json; charset=utf-8',
                        url: "FDI/ExportToWord/",
                        method: 'POST',
                        dataType: 'json',
                        data: JSON.stringify(args.data.FDIQuestionnaireID)
                    });
                }
            }
        });

And the controller part:
public ActionResult ExportToWord([FromBody]int id)
        {

            string rootPath = _hostingEnvironment.WebRootPath;
            FileStream fileStream = new FileStream(rootPath + "\\Template\\FDI_HR.docx", FileMode.Open);

            WordDocument document = new WordDocument(fileStream, FormatType.Docx);
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);

            bookmarkNavigator.MoveToBookmark("signature");

            using (var connection = new SqlConnection(Global.SQLConnection))
            {
                connection.Open();
                string base64 = connection.QueryFirstOrDefault<string>("Signature_Select", new { FDIQuestionnaireID = id }, commandType: CommandType.StoredProcedure);

                WPicture picture = bookmarkNavigator.InsertParagraphItem(ParagraphItemType.Picture) as WPicture;
                picture.LoadImage(Convert.FromBase64String(base64));

                MemoryStream memoryStream = new MemoryStream();
                document.Save(memoryStream, FormatType.Docx);
                document.Close();
                fileStream.Dispose();
                memoryStream.Position = 0;
                return File(memoryStream, "application/msword", "Sample.docx");
            }
        }

Can you help please



MJ Mohanaselvam Jothi Syncfusion Team March 8, 2018 06:24 PM UTC

Hi Pratura,

Thank you for your update.

We have prepared a sample application to download the Word document as per your requirement mentioned in your previous update.

Please find the sample from the below link and let us know if this helps you:
http://www.syncfusion.com/downloads/support/forum/136243/ze/SampleApplication-923521966.zip

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 


Loader.
Up arrow icon