locked
Best way to use data to fill out a document! RRS feed

  • Question

  • User-2051275383 posted

    Hey guys,

    I have a series of word documents that I want to use my Razor skills to build a better way of filling them out. At present they are filled out by hand then printed out.

    What I am proposing is using data that sits in a database to 'fill' out the forms. What is the best way of doing this?

    The simplest solution that I have working is converting the word doc into a HTML page and populating it with an SQL query - Works fine. I haven't found a decent Word to HTML converter though :( 

    Interested to see if anyone has gone down this path?

    Cheers!

    Tuesday, June 30, 2015 5:13 AM

All replies

  • User-166373564 posted

    Hi

    I haven't found a decent Word to HTML converter though :( 

    It's recommended to add reference to OpenXmlPowerTools.dll, and convert .docx file to HTML format

    using OpenXmlPowerTools;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    byte[] byteArray = File.ReadAllBytes(DocxFilePath);
    using (MemoryStream memoryStream = new MemoryStream())
    {
         memoryStream.Write(byteArray, 0, byteArray.Length);
         using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
         {
              HtmlConverterSettings settings = new HtmlConverterSettings()
              {
                   PageTitle = "My Page Title"
              };
              XElement html = HtmlConverter.ConvertToHtml(doc, settings);
    
              File.WriteAllText(HTMLFilePath, html.ToStringNewLineOnAttributes());
         }
    }

    With regards,

    Angie

    Wednesday, July 8, 2015 2:58 AM
  • User-2051275383 posted

    Will this enable me to use the doc and add information into it? Or simply convert it?

    Wednesday, July 8, 2015 5:01 AM
  • User-166373564 posted

    Hi,

    This will help convert it before you use the doc document.

    With regards,

    Angie

    Sunday, July 12, 2015 10:22 AM
  • User753101303 posted

    Hi,

    What do you want? Do you want to add information to a Word document and send this document client side or convert it to HTML? See perhaps https://msdn.microsoft.com/EN-US/library/office/cc850833.aspx

    Sunday, July 19, 2015 7:32 AM
  • User-1157115462 posted

    Check out this document generation toolkit. You can use Word document as templates and then populate them with the data from the database. The toolkit doesn't use Office COM which makes it a suitable solution for Asp.NET applications. What you need to do is just creating a Word document, inserting tags/placeholders and then calling several lines of code to populate the template document with the data:

    // Load products from the database.
    IEnumerable<Product> products = DataAccess.GetProducts();
    
    // Create report engine and provide 'products' as the value for the default data source.
    DocumentGenerator dg = new DocumentGenerator(products);
    
    // Execute document generation by specifying the template and the output document.
    DocumentGenerationResult result = dg.GenerateDocument("Template.docx", "GeneratedDocument.docx");
    if (result.HasErrors)
    {
        foreach (Error error in result.Errors) Console.WriteLine(error);
    }

    Thursday, July 23, 2015 9:20 AM