Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Sep 13, 2016 10:21 am

Hello iceblue team,

first of all thank you for the quick fix of my issue there: http://www.e-iceblue.com/forum/update-if-field-t6302.html

But now I have another question.
We use Spire.Doc for mailmerging in ASP.NET MVC project. In our templates there're many <ASK> statements. So during mailmerging we need some user interaction to handle <ASK> statements (please check an attachment). But because the mailmerging is executed on the server - we can't use any user interaction during this process.

So could you please give us any suggestions how can we handle this case using Spire.Doc?

Is it possible for example such workflow:
1. Get all ASK statements from the Template to some List<T>
2. Show all ASK statements in some popups (like it shows in Word, by using JavaScript) for user
3. Collect the answers from User
4. Put User answers to mailmerging process for <ASK> statements.
5. Execute MailMerge

If it make sense - could you please show some code examples for p. 1 and p. 4?

Or maybe there're other ways to handle <ASK> statements for ASP.NET MVC?

Thank you in advance.

_andrews_
 
Posts: 10
Joined: Sun Apr 03, 2016 11:20 am

Wed Sep 14, 2016 3:48 am

Dear _andrews_ ,

Gald to hear that issue has been resolved, and thanks for your inquiry again.
For your requirements,at present it only can collects the <ASK> field, here is sample code for your reference.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\original document\8548S2.docx");

            List<Field> ASKField = new List<Field>();
            //traverse section           
            foreach (Section sec in doc.Sections)
            {
                //traverse section's body and find the ASK field
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph par = obj as Paragraph;
                        foreach (DocumentObject o in par.ChildObjects)
                        {
                            if (o.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = o as Field;
                                if (field.Type == FieldType.FieldAsk)
                                {
                                    ASKField.Add(field);
                                }
                            }
                        }
                    }
                }
            }

Sorry that Spire.Doc doesn't support interaction you mentioned. Sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Sep 14, 2016 8:21 am

Hi Betsy,

thank you for your support.
You example for collect ASK fields is very useful.

But are there any chance to call something like this?:

Code: Select all
List<Field> ASKFields = new List<Field>();
List<string> answers = new List<string>();

// fill lists
// ...
// ...

document.MailMerge.ExecuteForAskFields(ASKFields, answers);
document.MailMerge.Execute(filedNames, filedValues);

_andrews_
 
Posts: 10
Joined: Sun Apr 03, 2016 11:20 am

Wed Sep 14, 2016 8:43 am

Dear _andrews_ ,

Thanks for your reply.
We will investigate it further, and will let you know if there is any progress.
Sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Sep 14, 2016 2:10 pm

So, I've replaced ASK via scenario I wrote you in 1st mail.

The last things what I need - I need get somehow from each Field in this list
Code: Select all
List<Field> ASKFields = new List<Field>();


ASK_Message and ASK_DefaultValue (please, check an attachment).

Do you provide such info in you Spire.Doc classes?

_andrews_
 
Posts: 10
Joined: Sun Apr 03, 2016 11:20 am

Thu Sep 15, 2016 7:06 am

Hi,

Thanks for your information.
You need to use Regex to find the result you want from FieldCode. Here is sample code for your reference:
Code: Select all
            string FieldCode = ASKFields[2].Code;
            Regex regex = new Regex("\"[^\"]*\"");
            string ASK_Message = regex.Match(FieldCode).Value.Replace("\"", "");
            Regex regex2 = new Regex("\\\\d*([^*]*?)$");
            string defaultVaule = regex2.Match(FieldCode).Value.Replace("\\d", "");

Hope this can help you.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Sep 20, 2016 6:07 am

Dear _andrews_,

After investigation, we found there is a way to do something like merging the ASK field.
First add a bookmark before the field end and then add the string answer into it. Finally, open the result document, update the REF field. It will display the result you want.
Code: Select all
            for(int i = 0; i < ASKFields.Count; i ++)
            {
                for (int j = 0; j < ASKFields[i].OwnerParagraph.ChildObjects.Count; j++)
                {
                    DocumentObject obj = ASKFields[i].OwnerParagraph.ChildObjects[j];
                     if ( obj is FieldMark && (obj as FieldMark).Type == FieldMarkType.FieldEnd)
                    {
                         //get the index of field end
                        FieldMark mark = obj as FieldMark;
                        int index = mark.OwnerParagraph.ChildObjects.IndexOf(mark);
                         //get the field code
                        string FieldI = ASKFields[i].Code;
                         //split the field code for getting the bookmark name
                        string[] content = FieldI.Split(' ');
                        if (content.Length >= 3)
                        {
                            //create the bookmark by ASK field
                            BookmarkStart bookmarkStart = new BookmarkStart(doc, content[2].Trim());
                            BookmarkEnd bookmarkEnd = new BookmarkEnd(doc, content[2].Trim());

                            TextRange tr = new TextRange(doc);
                            tr.Text = "hello";// the answer from users
                            //add the bookmark and insert the text into it
                            mark.OwnerParagraph.ChildObjects.Insert(index, bookmarkEnd);
                            mark.OwnerParagraph.ChildObjects.Insert(index, tr);
                            mark.OwnerParagraph.ChildObjects.Insert(index, bookmarkStart);
                            break;
                        }
                    }
                }
            }
            doc.SaveToFile("8548.docx",FileFormat.Docx);

Hope this can help you. If it doesn't meet your requirement, please provide us your sample document to help us do the investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Sun Sep 25, 2016 1:48 pm

Thank you! It's very useful!

_andrews_
 
Posts: 10
Joined: Sun Apr 03, 2016 11:20 am

Mon Sep 26, 2016 1:14 am

Dear _andrews_,

Thanks for your feedback.
Please feel free to contact us if there is any question or needs. We are here for help.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jan 25, 2017 6:15 am

Dear _andrews_,

Thanks for waiting.
We just release Spire.Office Platinum (Hot Fix) Version:2.15.4, in which includes an event can handle the ASK field. Welcome to test it.
Here is sample code for your reference.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile("test.doc");
            doc.UpdateFields += new UpdateFieldsHandler(doc_UpdateFields);
            doc.IsUpdateFields = true;
            doc.SaveToFile("output.docx", FileFormat.Docx);
        }
        private static void doc_UpdateFields(object sender, IFieldsEventArgs args)
        {
            if (args is AskFieldEventArgs)
            {
                AskFieldEventArgs askArgs = args as AskFieldEventArgs;
                Console.WriteLine(askArgs.PromptText);
                string s = Console.ReadLine();
                askArgs.ResponseText = s;
            }
        }


If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc