1

I am new here in the forum and I just start to work in a new web application on Visual studio 2013. I need to create an application that copy all the content from one Word Document to another. I had found this plugin that should make the job but I dont know how to put it in my code and make it work. I need to do it in a MVC application, so maybe I am not putting the code in the right place (now it is in the Model). Someone can help me telling me how to make it work? Please see the code that I have:

using Spire.Doc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DocumentApplication.Models
{
public class HomeModel
{
    public string Message { get; set; }        
}

public class CopyDocument
{
    Document sourceDoc = new Document("source.docx");
    Document destinationDoc = new Document("target.docx");        
    foreach (Section sec in sourceDoc.Sections) 
    {
        foreach (DocumentObject obj in sec.Body.ChildObjects)
        {
            destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone());
        }
    }
    destinationDoc.SaveToFile("target.docx");
    System.Diagnostics.Process.Start("target.docx");    
}

public class OpenDocument
{        
    Document document = new Document(@"C:\Users\daniel\Documents\ElAl-DRP.doc");
}
}

I cannot compile this because I have an error on the "foreach" line that says: "Invalid token 'foreach' in class' struct' or interface member declaration".

Please help me.

Thanks in advance

Daniel
  • 15
  • 1
  • 5
  • Which "foreach"? there are 2. – MichaelLake Dec 29 '15 at 09:15
  • This "System.Diagnostics.Process.Start("target.docx");" is a really bad idea in a web application. – MichaelLake Dec 29 '15 at 09:27
  • MichaelLake, The first "foreach" is the problem. I had copied this code from the link in the post. Actually I will not use this line ("System.Diagnostics.Process.Start("target.docx"); because I don't need to open the file. – Daniel Dec 29 '15 at 09:50

1 Answers1

0

Okay this is pretty rough but it at least works.

I could not make the example from the Spire work, I had to make some changes.

This example will take an uploaded file, save it in to the "AppData/uploads" folder then it will create a copy of that saved file in the "AppData/copies" folder.

My Changes

  1. the way we create the destination Doc in memory then save later.
  2. Create a new section in the "foreach" loop
  3. Add the cloned sections to the new section List item
  4. Document Format on the SaveDocument Method (This is a huge assumption)

Controller

    using System.IO;
    using System.Web;
    using System.Web.Mvc;
    using Spire.Doc;
    using Spire.Doc.Collections;

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                file.SaveAs(path);

                var outputPath = Path.Combine(Server.MapPath("~/App_Data/copies"), fileName);

                var sourceDoc = new Document(path);
                var destinationDoc = new Document();
                foreach (Section sec in sourceDoc.Sections)
                {
                    var newSection = destinationDoc.AddSection();
                    foreach (DocumentObject obj in sec.Body.ChildObjects)
                    {
                        newSection.Body.ChildObjects.Add(obj.Clone());
                    }
                }
                destinationDoc.SaveToFile(outputPath, FileFormat.Docx);
            }

            return View();
        }    
    }

View

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" id="file"/>
    <button type="submit">Save</button>
}
MichaelLake
  • 1,735
  • 14
  • 17