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.

Sat Nov 30, 2019 10:45 pm

Ok, so I'm creating a web application that can take a word template and dynamically merge items based on user input.

I would like to verify that there are a few basic fields on the template before I save it to my file structure.

what I would like to do is take the IFormFile that is the file I have uploaded, and load that into a document so that spire can get the mail merge field and I can do some quick validation.

Here is a generic version of what I'm looking for

Here is my Model
Code: Select all
    public class AddLetterViewModel
    {

        [Required]
        [Display(Name = "Letter Title")]
        public string LetterTitle { get; set; }

        [Required]
        [Display(Name = "Letter File")]
        public IFormFile LetterDoc { get; set; }

    }


and here is the (roughly) desired code in the controller

Code: Select all
       Document doc = new Document();
       doc.LoadFromFile(AddLetterViewModel.LetterDoc.FileName);

       // this is for an extension that I created for the validation it requires a spire.doc Document
       var resultModel = new ModelStateResults();
       var result = doc.VerifyDocument(resultModel);
       doc.Close();




if this validates as true then it creates the file in the file structure, and database entry for retrieval later.

Obviously the above doesn't work, but I'm hoping for some assistance to point me in the right direction.

tjones1467
 
Posts: 4
Joined: Sat Nov 16, 2019 8:39 pm

Sat Nov 30, 2019 11:35 pm

I figured it out and thought I would post it here in case anyone else needs it.

Code: Select all
                    Document doc = new Document();
                    using (var stream = model.LetterDoc.OpenReadStream())
                    {
                        doc.LoadFromStream(stream, FileFormat.Docx);
                    }
                    var resultModel = new ModelStateResults();
                    var result = doc.VerifyDocument(resultModel);
                    doc.Close();

tjones1467
 
Posts: 4
Joined: Sat Nov 16, 2019 8:39 pm

Mon Dec 02, 2019 1:55 am

Hi,

Thanks for sharing.
If there is any question while using our produce, please feel free to contact 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