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.

Wed Apr 12, 2023 8:31 am

Facing an issue with form fields in case of newline characters


STACK TRACE
Object reference not set to an instance of an object.
at spr?.?(TextRange A_0)
at spr?.?(TextRange A_0)
at spr?.?(FieldMark A_0, Field A_1)
at spr?.?(FieldMark A_0)
at Spire.Doc.Fields.FieldMark.Accept(spr? visitor)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.Documents.Paragraph.Accept(spr? visitor)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.TableCell.Accept(spr? visitor)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.TableRow.Accept(spr? visitor)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.Table.Accept(spr? visitor)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.DocumentObject.?(spr? A_0)
at Spire.Doc.Body.Accept(spr? visitor)
at spr?.?(Document A_0)
at spr?.?()
at spr?.?()
at spr?.?()
at spr?.?()
at spr?.?(spr? A_0)
at spr?.?(Document A_0, Stream A_1)
at Spire.Doc.Document.?(Stream A_0)
at Spire.Doc.Document.SaveToFile(Stream stream, FileFormat fileFormat)
at Spire.Doc.Document.SaveToStream(Stream stream, FileFormat fileFormat)
at DocFormFieldTest.Program.Main(String[] args) in C:\Users\Ganesh.Walanj\Documents\Visual Studio 2022\Projects\SpireDocTestApp\DocFormFieldTest\Program.cs:line 38



I am facing an issue with uploading attachments can you guide me as I have VS solution setup to repro issue with Spire.Doc -Version 11.4.0

Ganesh3991
 
Posts: 6
Joined: Tue Jan 03, 2023 2:32 pm

Wed Apr 12, 2023 9:40 am

Hi,

Thank you for your inquiry.

I didn't find the attachment you mentioned, could you please upload it again? Does the attachment you want to upload already contain your complete code and input document? It will help us reproduce your problem more quickly and provide a solution to you.

Thank you in advance for your assistance.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Wed Apr 12, 2023 9:53 am

Yes it's complete code

Ganesh3991
 
Posts: 6
Joined: Tue Jan 03, 2023 2:32 pm

Thu Apr 13, 2023 9:30 am

Hi,

Thank you for sharing your project.

After investigation, I found that the text form field in Word itself does not support filling content containing newlines. You are using the text form field as a placeholder to replace your real data, right? If so, I recommend using bookmarks as placeholders, which supports the inclusion of newline content.

I made a demo for you. Attached is a template document that uses bookmarks as placeholders along with the complete implementation code and resulting documentation.

Please contact us if you have any questions.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Thu Apr 13, 2023 3:28 pm

Hi,

Thank you for your prompt response and for providing a solution to my issue. The solution you provided is working perfectly fine.

However, I have noticed that the formatting of the text in the form field is different than that of the original text.

Thank you for suggesting the use of bookmarks as placeholders. I have tried the attached demo this will be very helpful.

I appreciate your efforts and the level of support provided by the E-iceblue team. Thank you once again for your assistance in resolving this issue.

Sincerely,
Ganesh

Ganesh3991
 
Posts: 6
Joined: Tue Jan 03, 2023 2:32 pm

Fri Apr 14, 2023 2:49 am

Hi,

Thank you for your feedback, we are glad to have been able to assist you.

Our Spire.Doc supports preserving the original bookmark format when replacing bookmarks, and this can be achieved by setting the relevant parameters to true in the ReplaceBookmarkContent (TextBodyPart bodyPart, bool isKeepSourceFirstParaFormat, bool saveFormatting) method. For example: bookmarkNavigator.ReplaceBookmarkContent(part,true,true). I have attached a template document with bookmark content formatting as well as the generated result document for you.

If you have any further questions, please feel free to contact us. We would be happy to help you.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Fri Apr 14, 2023 1:38 pm

Hi,

I have tried this solution (i.e. bookmarkNavigator.ReplaceBookmarkContent(part,true,true)) but not working. I have added a new Word file template in project to repro the issue


Sincerely,
Ganesh

Ganesh3991
 
Posts: 6
Joined: Tue Jan 03, 2023 2:32 pm

Mon Apr 17, 2023 3:33 am

Hi,

Thank you for sharing your project.

After running your project, I noticed that your document used text form fields as placeholders to replace real data. I removed the text form fields and added bookmarks before running it again. The resulting document can be divided into paragraphs and retains its original text format. I have uploaded a demo containing the modified document, the resulting document, and four screenshots showing the steps taken to modify the document. Please feel free to try it with your original document.

If you have any further questions, please do not hesitate to contact us. We are happy to assist you.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Mon Apr 17, 2023 8:58 am

Hi,

I would like to request your assistance in raising a ticket in your system to support newline characters for text form fields. The current solution provided is causing changes in the paragraph alignment, and I have multiple Live templates that cannot be modified.




Sincerely,
Ganesh

Ganesh3991
 
Posts: 6
Joined: Tue Jan 03, 2023 2:32 pm

Tue Apr 18, 2023 10:51 am

Hi,

Thank you for your feedback.

I modified the code in the original UpdateFormFieldsValues_Solution1() method to test the template document (Employee.docx) that i provided, and the resulting document was able to recognize line breaks and maintain the original paragraph alignment of the Template document .You will need to modify your template,if you don't modify your template, line breaks will not be recognized.

Code: Select all
private static void UpdateFormFieldsValues_Solution1(Document template, List<FormFieldDto> data)
        {
            if (template.Sections != null)
            {
                foreach (var field in data)
                {
                    String[] files1 = new String[] { field.Name };
                    String[] files2 = new String[] { field.Value };                         
                    BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(template);
                    bookmarkNavigator.MoveToBookmark(files1[0]);
                    bookmarkNavigator.ReplaceBookmarkContent(files2[0], true);
                 
                }
            }
        }

I have uploaded a demo for you, which includes my input document and the resulting document generated from it. Please refer to the attachments.

If you have any further questions, please feel free to contact us. We would be happy to help you.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Wed Apr 19, 2023 8:22 am

Hi,

May I know if you have replaced the code in the UpdateFormFieldsValues_Solution1() method and whether the resulting output document meets your requirements?

If you have any further questions, please feel free to contact us. We are always here to help you.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Wed Apr 19, 2023 3:19 pm

Hi,

I have tried the demo solution, and the resulting document generated from the modified code was able to maintain the original paragraph alignment of the template document. However, I have multiple Live templates that cannot be modified manually, and I need a tool that can convert the form fields into bookmarks of the template document.

Thank you so much for your assistance

Sincerely,
Ganesh

Ganesh3991
 
Posts: 6
Joined: Tue Jan 03, 2023 2:32 pm

Thu Apr 20, 2023 10:17 am

Hi,

Thank you for your feedback.

I found that the text form fields in your original document were inserted into bookmarks. You can first delete the text form fields and leave only the bookmarks,then replacing the content of the bookmarks. I have put the complete code for implementing this requirement below:

Code: Select all
namespace DocFormFieldTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Document template = new Document();
         
            string templateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Employee.docx");

            template.LoadFromFile(templateFilePath);

            //remove a field from a Word document
            UpdateFormFieldsValues_Error(template, GetData());

            //replace bookmark content in a Word document
            UpdateFormFieldsValues_Solution1(template, GetData());

            string saveFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Results", "EmployeeOutPut.docx");

            if (File.Exists(saveFilePath))
            {
                File.Delete(saveFilePath);
            }

            string dir = Path.GetDirectoryName(saveFilePath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            try
            {
                using (var fs = File.Open(saveFilePath, FileMode.OpenOrCreate))
                {
                    template.SaveToStream(fs, FileFormat.Docx2019);
                }

                Process.Start(saveFilePath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            Console.ReadLine();
        }

        private static List<FormFieldDto> GetData()
        {
            return new List<FormFieldDto>
            {
                new FormFieldDto {  Name = "fdAdd1", Value = null },
               new FormFieldDto {  Name = "fdAdd2", Value = null },
                new FormFieldDto {  Name = "fdEMP_ID", Value = "NRS_UD301717" },
               new FormFieldDto {  Name = "fdRegisteredOwner", Value = null },
                new FormFieldDto {  Name = "fdOwnerAddress", Value = "Bestepe Mah.Nergiz Sk.No: 7\r\nA lc kapi No 24,\r\nYenimahalle,Ankara,Turkey" },
                new FormFieldDto {  Name = "fdEMP_NAME", Value = "., Win lwin " },
                new FormFieldDto {  Name = "fdDOB", Value = "04 Aug 1963" },
               new FormFieldDto {  Name = "fdBirthPlace", Value = null },
               new FormFieldDto {  Name = "fdPassportNumber", Value = null },
               new FormFieldDto {  Name = "fdPassportIssued", Value = null },
               new FormFieldDto {  Name = "fdPassportCountry", Value = null }
            };
        }

        private static void UpdateFormFieldsValues_Error(Document template, List<FormFieldDto> data)
        {
           
            if (template.Sections != null)
            {
                foreach (var field in data)
                {           
                        Field field1 = template.Fields[0];
                        //Get the paragraph of the field
                        Paragraph par = field1.OwnerParagraph;
                        //Get the index of the  field
                        int index = par.ChildObjects.IndexOf(field1);
                        //Remove if field via index
                        par.ChildObjects.RemoveAt(index);                                   
                }
            }
        }

        private static void UpdateFormFieldsValues_Solution1(Document template, List<FormFieldDto> data)
        {
            if (template.Sections != null)
            {
                foreach (var field in data)
                {                   
                    String[] files2 = new String[] { field.Name };
                    String[] files3 = new String[] { field.Value };                         
                    BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(template);
                    bookmarkNavigator.MoveToBookmark(files2[0]);//name
                    bookmarkNavigator.ReplaceBookmarkContent(files3[0], true);//value
                 
                }
            }
        }
    }

    struct FormFieldDto
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
}



If you have any questions, please feel free to contact me.

Sincerely,
Ella
E-iceblue support team
User avatar

Ella.Zhang
 
Posts: 42
Joined: Fri Apr 07, 2023 7:42 am

Fri May 26, 2023 3:28 am

Hello,

Hope you are doing well!
Has your issue been solved? Looking forward to your feedback.

Please feel free to contact us if you have any problems.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3044
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc

cron