locked
Transform Html Code to WYSWYG Layout in .Docx when transformation from .DOTX RRS feed

  • Question

  • User-893002196 posted

    Hi All,

    1. I have an .dotx with 1 fieldcode called <<Notes>>

    2. I have a table with field called Notes contains data in html code as below:

    "<strong>2.&nbsp;DEFINITIONS, INTERPRETATION, AND STRUCTURE OF ORDER FORM.</strong><br>2.1.<u>&nbsp;Incorporation of XXXs</u>.&nbsp; This Order Form incorporates by reference the terms of the XXXs entered into by the Parties on the date set forth on the first page of this Order Form under XXXs Effective Date.<br>2.2.<u>&nbsp;Definitions</u>.&nbsp; Capitalised terms used in this Order Form without definition shall have the meanings ascribed to them in the XXXs.<br>2.3.&nbsp;<u>Structure of Order Form</u>.&nbsp; Customer shall be subject to and will comply with (a) the terms of each Product or Services Schedule identified in Section 1 of this Order Form, which are hereby incorporated into and made a part of this Order Form, and (b) the terms of each Addendum identified in Section 1 of this Order Form, which are incorporated into and made a part of the XXXs.<br>2.4.&nbsp;<u>Order of Priority</u>.&nbsp; In the event of any inconsistencies between:&nbsp; (a) the XXXs and this Order Form, this Order Form shall take precedence, (b) this Order Form and any Schedule, this Order Form will take precedence (except for express deviations from this Order Form which are identified in such Schedule), and (c) this Order Form and any Addendum, this Order Form shall take precedence.<br><a></a>"

    When I generate the docx from dotx is displayed the html code instead the WYSWYG layout. ANy idea to transform my html code to be docx format?
    Mean:

    <strong></strong> will be bold
    <u></u> will be underline
    &nbsp; will be space
    <br> will be linebreak

    private string GetMergeValue(string FieldName)

    {

    switch (FieldName)

    {

    case "Notes":

    return _dataReader[0].Notes == null ? "-" : _dataReader[0].Notes.ToString().Replace("<br>", Environment.NewLine);

    default:

    throw new Exception(message: "FieldName (" + FieldName + ") was not found");

    }

    }

    public RETURN_VAL GenerateDocument(string fileName)

    {

    try

    {

    _filename = fileName;

    // Don't continue if the template file name is not found

    if (!File.Exists(_templateFileName))

    {

    throw new Exception(message: "TemplateFileName (" + _templateFileName + ") does not exist");

    }

    // If the file is a DOTX file convert it to docx

    if (_templateFileName.ToUpper().EndsWith("DOTX"))

    {

    RETURN_VAL resultValue = ConvertTemplate();

    if (!resultValue.Value)

    {

    return resultValue;

    }

    }

    else

    {

    // Otherwise make a copy of the Word Document to the targetFileName

    File.Copy(_templateFileName, _targetFileName);

    }

    using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(_targetFileName, true))

    {

    docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (FieldCode field in docGenerated.MainDocumentPart.RootElement.Descendants<FieldCode>())

    {

    var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);

    var fieldname = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();

    var fieldValue = GetMergeValue(FieldName: fieldname);

    // Go through all of the Run elements and replace the Text Elements Text Property

    foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())

    {

    foreach (DocumentFormat.OpenXml.Wordprocessing.Text txtFromRun in run.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where(a => a.Text == "«" + fieldname + "»"))

    {

    txtFromRun.Text = fieldValue;

    }

    }

    }

    }

    return new RETURN_VAL { Value = true };

    }

    catch (Exception ex)

    {

    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::generateDocument() - " + ex.ToString() };

    }

    Tuesday, September 10, 2019 8:12 AM

Answers

  • User-893002196 posted

    Hi Expert,

    Basically I have a .dotx file with 2 fields:

    1. template.dotx as below:-

    Name: <<names>>

    Notes:

    <<Notes>>

    2. I have Form UI

    Registration Form

    Names: [      Texbox    ] 

    Notes: [    RichText Editor - WYSWYG   ]    

    [ Generate Receipt Button] 

    Example values

    Names : [ Micheale ]

    Notes:

    ----- Richtext Headers with all the function here -----

    Company Contact Country
    Alfreds Futterkiste Maria Anders Germany
    Centro comercial Moctezuma Francisco Chang Mexico
    Ernst Handel Roland Mendel Austria
    Island Trading Helen Bennett UK
    Laughing Bacchus Winecellars Yoshi Tannamuri Canada
    Magazzini Alimentari Riuniti Giovanni Rovelli Italy

    3. Once the Generate Receipt Button click on, .docx expecting to be generate via the template as below, result.docx:

    Name: Micheale

    Notes:

    Company Contact Country
    Alfreds Futterkiste Maria Anders Germany
    Centro comercial Moctezuma Francisco Chang Mexico
    Ernst Handel Roland Mendel Austria
    Island Trading Helen Bennett UK
    Laughing Bacchus Winecellars Yoshi Tannamuri Canada
    Magazzini Alimentari Riuniti Giovanni Rovelli Italy

    Hope someone expert here can help me. I am stuck.

    My codes which not able to complete:

    public RETURN_VAL GenerateDocument2(string fileName)

    {

    try

    {

    _filename = fileName;

    // Don't continue if the template file name is not found

    if (!File.Exists(_templateFileName))

    {

    throw new Exception(message: "TemplateFileName (" + _templateFileName + ") does not exist");

    }

    // If the file is a DOTX file convert it to docx

    if (_templateFileName.ToUpper().EndsWith("DOTX"))

    {

    RETURN_VAL resultValue = ConvertTemplate();

    if (!resultValue.Value)

    {

    return resultValue;

    }

    }

    else

    {

    // Otherwise make a copy of the Word Document to the targetFileName

    File.Copy(_templateFileName, _targetFileName);

    }

    using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(_targetFileName, true))

    {

    docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (FieldCode field in docGenerated.MainDocumentPart.RootElement.Descendants<FieldCode>())

    {

    var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);

    var fieldname = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();

    var fieldValue = GetMergeValue(FieldName: fieldname);

     

     

    // Go through all of the Run elements and replace the Text Elements Text Property

    foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())

    {

    foreach (DocumentFormat.OpenXml.Wordprocessing.Text txtFromRun in run.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where(a => a.Text == "«" + fieldname + "»"))

    {

    if (fieldname == "Notes")

    {

    var tb = "<table style='width:100%'><tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr><tr><td>Jill</td><td>Smith</td> <td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";

    ??????????????  WHAT SHOULD I NEED TO DO HERE ???????????????

    }

    else

    {

    txtFromRun.Text = fieldValue.ToString();

    }

    }

    }

    }

    }

    return new RETURN_VAL { Value = true };

    }

    catch (Exception ex)

    {

    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::generateDocument2() - " + ex.ToString() };

    }

    }

    Thanks.

    Regards,

    Micheale

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 7:56 AM

All replies

  • User1634355159 posted

    Hi micnie2020​​​​​​​,

    Could you give a demo that can reproduce your question, because we can't see your problem from the code you gave?There are too many classes and methods in your code that are undefined. Do you have used any plugins?

    Best Regards ,

    Lewis Lu

    Wednesday, September 11, 2019 9:17 AM
  • User-893002196 posted

    Hi,

    A) I am installing nuget

    1. Spire.Doc
    2. DocumentFormat.OpenXml
    3. OpenXmlPowerTools

    B) I have an Test.dotx template called Test.dotx with only 1 fieldcode <<Notes>>

    C) DocumentGeneration2.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using A = DocumentFormat.OpenXml.Drawing;
    using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
    using System.Xml.Linq;
    using System.Xml;


    // Additional References
    using Newtonsoft.Json;
    using ProjectName;
    using OpenXmlPowerTools;
    using ProjectName.Models;
    using System.Net;
    using System.Text.RegularExpressions;

    namespace ProjectName
    {

       
        public class DocumentGeneration2
        {
            // Struct to hold the return value and possible exception
            public struct RETURN_VAL
            {
                public bool Value;
                public string Exception;
            }

            private const string FieldDelimeter = " MERGEFIELD ";

            private string _templateFileName;
            private string _targetFileName;
            private string _filename;
            private List<Models.TblTest> _dataReader;

            /// <summary>
            /// Constructor for the DocumentGeneration Class
            /// </summary>
            /// <param name="templateFileName">File to base the document off of</param>
            /// <param name="targetFileName">End File to write the Mail Merge to</param>
            public DocumentGeneration2(string templateFileName, string targetFileName, List<Models.TblTest> dataReader)
            {
                _templateFileName = templateFileName;
                _targetFileName = targetFileName;
                _dataReader = dataReader;
            }

            public void parseTextForOpenXML(Run run, string textualData)
            {
                string[] newLineArray = { Environment.NewLine };
                string[] textArray = textualData.Split(newLineArray, StringSplitOptions.None);

                bool first = true;

                foreach (string line in textArray)
                {
                    if (!first)
                    {
                        run.Append(new Break());
                    }

                    first = false;

                    Text txt = new Text();
                    txt.Text = line;
                    run.Append(txt);
                }
            }

            public void RemovePageBlank(WordprocessingDocument document, string file)
            {
                int count = 0;
                XDocument xdoc = new XDocument();
                xdoc = XDocument.Load(file);
                count += xdoc.Descendants("PAGE").Count();         

                // save the xdoc back to wdoc, not quoted here since this code can be easily found in the internet
                document.MainDocumentPart.Document.Save();
            }

            public static void InsertAPicture(string document, string fileName)
            {
                using (WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(document, true))
                {
                    MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

                    ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

                    using (FileStream stream = new FileStream(fileName, FileMode.Open))
                    {
                        imagePart.FeedData(stream);
                    }

                    AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
                }
            }


            private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
            {
                // Define the reference of the image.
                var element =
                     new Drawing(
                         new DW.Inline(
                             new DW.Extent() { Cx = 6850000L, Cy = 590000L },
                             new DW.EffectExtent()
                             {
                                 LeftEdge = 0L,
                                 TopEdge = 0L,
                                 RightEdge = 0L,
                                 BottomEdge = 0L
                             },
                             new DW.DocProperties()
                             {
                                 Id = (UInt32Value)1U,
                                 Name = "Picture 1"
                             },
                             new DW.NonVisualGraphicFrameDrawingProperties(
                                 new A.GraphicFrameLocks() { NoChangeAspect = true }),
                             new A.Graphic(
                                 new A.GraphicData(
                                     new PIC.Picture(
                                         new PIC.NonVisualPictureProperties(
                                             new PIC.NonVisualDrawingProperties()
                                             {
                                                 Id = (UInt32Value)0U,
                                                 Name = "New Bitmap Image.jpg"
                                             },
                                             new PIC.NonVisualPictureDrawingProperties()),
                                         new PIC.BlipFill(
                                             new A.Blip(
                                                 new A.BlipExtensionList(
                                                     new A.BlipExtension()
                                                     {
                                                         Uri =
                                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                                     })
                                             )
                                             {
                                                 Embed = relationshipId,
                                                 CompressionState =
                                                 A.BlipCompressionValues.Print
                                             },
                                             new A.Stretch(
                                                 new A.FillRectangle())),
                                         new PIC.ShapeProperties(
                                             new A.Transform2D(
                                                 new A.Offset() { X = 0L, Y = 0L },
                                                 new A.Extents() { Cx = 6850000L, Cy = 590000L }),
                                             new A.PresetGeometry(
                                                 new A.AdjustValueList()
                                             )
                                             { Preset = A.ShapeTypeValues.Rectangle }))
                                 )
                                 { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                         )
                         {
                             DistanceFromTop = (UInt32Value)0U,
                             DistanceFromBottom = (UInt32Value)0U,
                             DistanceFromLeft = (UInt32Value)0U,
                             DistanceFromRight = (UInt32Value)0U,
                             EditId = "50D07946"
                         });

                // Append the reference to body, the element should be in a Run.
                //wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
                wordDoc.MainDocumentPart.Document.Body.InsertBefore(new Paragraph(new Run(element)), wordDoc.MainDocumentPart.Document.Body.FirstChild);
            }

            private static void AddImageNoPOToBody(WordprocessingDocument wordDoc, string relationshipId)
            {
                // Define the reference of the image.
                var element =
                     new Drawing(
                         new DW.Inline(
                             new DW.Extent() { Cx = 1010000L, Cy = 812000L },
                             new DW.EffectExtent()
                             {
                                 LeftEdge = 0L,
                                 TopEdge = 0L,
                                 RightEdge = 0L,
                                 BottomEdge = 0L
                             },
                             new DW.DocProperties()
                             {
                                 Id = (UInt32Value)1U,
                                 Name = "Picture 1"
                             },
                             new DW.NonVisualGraphicFrameDrawingProperties(
                                 new A.GraphicFrameLocks() { NoChangeAspect = true }),
                             new A.Graphic(
                                 new A.GraphicData(
                                     new PIC.Picture(
                                         new PIC.NonVisualPictureProperties(
                                             new PIC.NonVisualDrawingProperties()
                                             {
                                                 Id = (UInt32Value)0U,
                                                 Name = "New Bitmap Image.jpg"
                                             },
                                             new PIC.NonVisualPictureDrawingProperties()),
                                         new PIC.BlipFill(
                                             new A.Blip(
                                                 new A.BlipExtensionList(
                                                     new A.BlipExtension()
                                                     {
                                                         Uri =
                                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                                     })
                                             )
                                             {
                                                 Embed = relationshipId,
                                                 CompressionState =
                                                 A.BlipCompressionValues.Print
                                             },
                                             new A.Stretch(
                                                 new A.FillRectangle())),
                                         new PIC.ShapeProperties(
                                             new A.Transform2D(
                                                 new A.Offset() { X = 0L, Y = 0L },
                                                 new A.Extents() { Cx = 1010000L, Cy = 812000L }),
                                             new A.PresetGeometry(
                                                 new A.AdjustValueList()
                                             )
                                             { Preset = A.ShapeTypeValues.Rectangle }))
                                 )
                                 { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                         )
                         {
                             DistanceFromTop = (UInt32Value)0U,
                             DistanceFromBottom = (UInt32Value)0U,
                             DistanceFromLeft = (UInt32Value)0U,
                             DistanceFromRight = (UInt32Value)0U,
                             EditId = "50D07946"
                         });

                // Append the reference to body, the element should be in a Run.
                wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
            }

            private static Paragraph InsertFormatRun(string _text)
            {
                Paragraph _head = new Paragraph();
                Run _run = new Run();
                Text _line = new Text(_text);
                _run.Append(_line);
                _head.Append(_run);
                return _head;
            }


            /// <summary>
            /// Converts the DOTX to DOCX
            /// </summary>
            /// <returns>True or False (with an exception) if successful in converting the document</returns>
            private RETURN_VAL ConvertTemplate()
            {
                try
                {
                    MemoryStream msFile = null;

                    using (Stream sTemplate = File.Open(_templateFileName, FileMode.Open, FileAccess.Read))
                    {
                        msFile = new MemoryStream((int)sTemplate.Length);
                        sTemplate.CopyTo(msFile);
                        msFile.Position = 0L;
                    }

                    using (WordprocessingDocument wpdFile = WordprocessingDocument.Open(msFile, true))
                    {
                        wpdFile.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

                        MainDocumentPart docPart = wpdFile.MainDocumentPart;
                        docPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(_templateFileName, UriKind.RelativeOrAbsolute));

                        docPart.Document.Save();
                    }

                    // Flush the MemoryStream to the file
                    File.WriteAllBytes(_targetFileName, msFile.ToArray());

                    msFile.Close();

                    return new RETURN_VAL { Value = true };
                }
                catch (Exception ex)
                {
                    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::convertTemplate() - " + ex.ToString() };
                }
            }

            /// <summary>
            /// Gets the Mail Merge Value
            /// </summary>
            /// <param name="FieldName">Field Name from the Word Template/Document</param>
            /// <returns>The Mail Merge value, but if the Field could not be found, throw an exception</returns>
            private string GetMergeValue(string FieldName)
            {

                switch (FieldName)
                {
                    case "Notes":
                        return _dataReader[0].Notes == null ? "-" : _dataReader[0].Notes.ToString().Replace("<br>", Environment.NewLine);               
                   
                    default:
                        throw new Exception(message: "FieldName (" + FieldName + ") was not found");
                }
            }

            private static Footer GeneratePageFooterPart(string FooterText)
            {
                var element =
                    new Footer(
                        new Paragraph(
                            new ParagraphProperties(
                                new ParagraphStyleId() { Val = "Footer" }),
                            new Run(
                                new DocumentFormat.OpenXml.Wordprocessing.Text(FooterText))
                        ));

                return element;
            }

            private static Header GeneratePageHeaderPart(string HeaderText)
            {
                var element =
                    new Header(
                        new Paragraph(
                            new ParagraphProperties(
                                new ParagraphStyleId() { Val = "Header" }),
                            new Run(
                                new DocumentFormat.OpenXml.Wordprocessing.Text(HeaderText))
                        ));

                return element;
            }

            private static RunProperties GetRunPropertyFromTableCell(DocumentFormat.OpenXml.Wordprocessing.TableRow rowCopy, int cellIndex)
            {
                var runProperties = new RunProperties();
                var fontname = "Arial";
                var fontSize = "20";
                try
                {
                    fontname =
                        rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>()
                           .ElementAt(cellIndex)
                           .GetFirstChild<Paragraph>()
                           .GetFirstChild<ParagraphProperties>()
                           .GetFirstChild<ParagraphMarkRunProperties>()
                           .GetFirstChild<RunFonts>()
                           .Ascii;
                }
                catch
                {
                    //swallow
                }
                try
                {
                    fontSize =
                           rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>()
                              .ElementAt(cellIndex)
                              .GetFirstChild<Paragraph>()
                              .GetFirstChild<ParagraphProperties>()
                              .GetFirstChild<ParagraphMarkRunProperties>()
                              .GetFirstChild<FontSize>()
                              .Val;
                }
                catch
                {
                    //swallow
                }
                runProperties.AppendChild(new RunFonts() { Ascii = fontname });
                runProperties.AppendChild(new FontSize() { Val = fontSize });
                return runProperties;
            }

            private static readonly System.Text.RegularExpressions.Regex instructionRegEx =
                   new System.Text.RegularExpressions.Regex(
                               @"^[\s]*MERGEFIELD[\s]+(?<name>[#\w]*){1}               # This retrieves the field's name (Named Capture Group -> name)
                                   [\s]*(\\\*[\s]+(?<Format>[\w]*){1})?                # Retrieves field's format flag (Named Capture Group -> Format)
                                   [\s]*(\\b[\s]+[""]?(?<PreText>[^\\]*){1})?         # Retrieves text to display before field data (Named Capture Group -> PreText)
                                                                                       # Retrieves text to display after field data (Named Capture Group -> PostText)
                                   [\s]*(\\f[\s]+[""]?(?<PostText>[^\\]*){1})?",
                               System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.ExplicitCapture | System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Singleline);

            private static string GetFieldName(SimpleField field, out string[] switches)
            {
                var a = field.GetAttribute("instr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                switches = new string[0];
                string fieldname = string.Empty;
                string instruction = a.Value;

                if (!string.IsNullOrEmpty(instruction))
                {
                    System.Text.RegularExpressions.Match m = instructionRegEx.Match(instruction);
                    if (m.Success)
                    {
                        fieldname = m.Groups["name"].ToString().Trim();
                        int pos = fieldname.IndexOf('#');
                        if (pos > 0)
                        {
                            // Process the switches, correct the fieldname.
                            switches = fieldname.Substring(pos + 1).ToLower().Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
                            fieldname = fieldname.Substring(0, pos);
                        }
                    }
                }

                return fieldname;
            }

            /// <summary>
            /// Generates the Word Document and performs the Mail Merge
            /// </summary>
            /// <returns>True or false(with exception) if the generation was successful or not</returns>
            public RETURN_VAL GenerateDocument2(string fileName)
            {
                try
                {
                    _filename = fileName;
                    // Don't continue if the template file name is not found
                    if (!File.Exists(_templateFileName))
                    {
                        throw new Exception(message: "TemplateFileName (" + _templateFileName + ") does not exist");
                    }

                    // If the file is a DOTX file convert it to docx
                    if (_templateFileName.ToUpper().EndsWith("DOTX"))
                    {
                        RETURN_VAL resultValue = ConvertTemplate();

                        if (!resultValue.Value)
                        {
                            return resultValue;
                        }
                    }
                    else
                    {
                        // Otherwise make a copy of the Word Document to the targetFileName
                        File.Copy(_templateFileName, _targetFileName);
                    }

                   
                    using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(_targetFileName, true))
                    {
                        docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);
                        foreach (FieldCode field in docGenerated.MainDocumentPart.RootElement.Descendants<FieldCode>())
                        {
                            var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);
                            var fieldname = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();

                            var fieldValue = GetMergeValue(FieldName: fieldname);

                            // Go through all of the Run elements and replace the Text Elements Text Property
                            foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())
                            {
                                foreach (DocumentFormat.OpenXml.Wordprocessing.Text txtFromRun in run.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where(a => a.Text == "«" + fieldname + "»"))
                                {
                                    txtFromRun.Text = fieldValue;
                                }
                            }
                        }
                     
                    }
                return new RETURN_VAL { Value = true };
                }
                catch (Exception ex)
                {
                    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::generateDocument2() - " + ex.ToString() };
                }
            }
        }
    }

    D) Test.cshtml

    @{
        ViewBag.Title = "Create XXX";
        var profileData = ViewBag.UserData;
    }
    @model ProjectName.Models.TblTest
    @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
    @functions{
        public string GetAntiXsrfRequestToken()
        {
            return Xsrf.GetAndStoreTokens(Context).RequestToken;
        }
    }
    @section scripts{

        <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
        <script src="~/Scripts/jquery-1.8.11.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
        <script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
        <script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>


        <style>
            .container {
                padding-right: 5px;
                padding-left: 5px;
                margin-right: auto;
                margin-left: auto;
                width: 90%;
            }


            #panel, #flip {
                padding: 1px;
                text-align: left;
                background-color: #e5eecc;
                border: solid 1px #c3c3c3;
            }

            #panel {
                padding: 10px;
                display: none;
            }


            table {
                font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
                width: 100%;
                border-collapse: collapse;
            }

                table td, table th {
                    font-size: small;
                    border: 1px solid #638eb5;
                    padding: 3px 7px 2px 7px;
                }

                table th {
                    font-size: small;
                    text-align: left;
                    padding-top: 5px;
                    padding-bottom: 4px;
                    background-color: #6e9eca;
                    color: #fff;
                }

                table tr.alt td {
                    color: #000;
                    background-color: #EAF2D3;
                }

            th.hide_me, td.hide_me {
                display: none;
            }

            .classol1 {
                marker-start: inherit;
                counter-reset: item 0;
                padding-left: 10px;
            }

            .classol2 {
                marker-start: inherit;
                counter-reset: item 1;
                padding-left: 10px;
            }

            .classol3 {
                marker-start: inherit;
                counter-reset: item 2;
                padding-left: 10px;
            }

            .classol4 {
                marker-start: inherit;
                counter-reset: item 3;
                padding-left: 10px;
            }

            .classol5 {
                marker-start: inherit;
                counter-reset: item 4;
                padding-left: 10px;
            }

            .classol6 {
                marker-start: inherit;
                counter-reset: item 5;
                padding-left: 10px;
            }

            .classli1 {
                display: block
            }

                .classli1:before {
                    content: counters(item, ".") " ";
                    counter-increment: item
                }
        </style>

        <link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>

        <script type='text/javascript'>

          $(document).ready(function () {


              $('.summernote').summernote({
                 tabsize: 1,
                 height: 500,
                 placeholder: "Place content here"
              });

              var HTMLstring5 = '<ol class=classol5><li class=classli1><b>XXXX SCHEDULE.</b></li>';
              HTMLstring5 = HTMLstring5 + '<ol  class=classol1><li class=classli1><u>Products and Services.</u>  xxxx Services contained in the Order Summary set forth in Section 1.</li>';
              HTMLstring5 = HTMLstring5 + '<li class=classli1><u>Invoicing Schedule.</u>.  Customer will pay to xxx the fees for the Products and Services set forth in this Order Form within thirty (30) days of the invoice date.  Invoice Customer for the Products and Services in accordance with the following schedule (as applicable):<br><br>';
              HTMLstring5 = HTMLstring5 + '<table>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td><b>Fee Type</b></td>';
              HTMLstring5 = HTMLstring5 + '<td><b>Product/Service Type</b></td>';
              HTMLstring5 = HTMLstring5 + '<td><b>Invoicing Schedule</b></td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td rowspan=2>Initial Fee</td>';
              HTMLstring5 = HTMLstring5 + '<td>Initial Software License Fees</td>';
              HTMLstring5 = HTMLstring5 + '<td rowspan=2>Invoiced on the Order Form Effective Date</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>Initial SaaS Product Fees</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>Rental Model Fee</td>';
              HTMLstring5 = HTMLstring5 + '<td>Rental Model License Fee</td>';
              HTMLstring5 = HTMLstring5 + '<td>Invoiced on the Order Form Effective Date for the entire rental term.</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<td rowspan=3>Monthly Fee</td>';
              HTMLstring5 = HTMLstring5 + '<td>Monthly Software License Fees</td>';
              HTMLstring5 = HTMLstring5 + '<td rowspan=2>Invoiced on the Order Form Effective Date.  Subsequent monthly invoices will be provided on or before the fifteenth (15th) day during each calendar month.  Invoices are provided monthly in advance.</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>Monthly SaaS Product Fees</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>Monthly Service Fees</td>';
              HTMLstring5 = HTMLstring5 + '<td>Invoiced on or before the fifteenth (15th) of each calendar month for services performed during the prior calendar month.</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<td rowspan=4>Annual Fee</td>';
              HTMLstring5 = HTMLstring5 + '<td>Annual Software License Fees</td>';
              HTMLstring5 = HTMLstring5 + '<td rowspan=4>Invoiced on the Order Form Effective Date.  If there are renewal terms, invoices will be provided on or before the fifteenth (15th) day following each anniversary of the Order Form Effective Date.  Invoices are provided annually in advance.</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>Annual SaaS Product Fees</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>Annual Support Fees</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>SimSci Tokens</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '<tr>';
              HTMLstring5 = HTMLstring5 + '<td>On-Demand Fee</td>';
              HTMLstring5 = HTMLstring5 + '<td>CALM or UBL Tokens</td>';
              HTMLstring5 = HTMLstring5 + '<td>Invoiced on purchase.</td>';
              HTMLstring5 = HTMLstring5 + '</tr>';
              HTMLstring5 = HTMLstring5 + '</table> ';
              HTMLstring5 = HTMLstring5 + '</li><br><br>';
              HTMLstring5 = HTMLstring5 + '<li class=classli1><u>Price Adjustments.</u>.  Following the expiration of the initial term for a Product or Service, the fees for such Product or Service shall automatically increase at a rate of three percent (3%) per year at the beginning of each year of the renewal period.</li>';
              HTMLstring5 = HTMLstring5 + '<li class=classli1><u>Excess Use.</u>.  If Customer’s use of any Product exceeds the permitted usage metrics (including, but not limited to, number of users or installation location), then Customer will be subject to additional fees for such excess usage at XXX’s then-current rates.  Customer will execute an additional Order Form or amendment to this Order Form for such additional usage and the fees for such additional usage will accrue from the date the excess usage began (together with an interest rate of one and one-half percent (1.5%) per month or partial month from the date such excess usage began until payment).  The assessment of additional fees shall be without prejudice to XXX’s other rights and remedies with respect to such excess usage.</li>';
              HTMLstring5 = HTMLstring5 + '</ol>';
              HTMLstring5 = HTMLstring5 + '</ol>';
              $('.summernote').summernote('pasteHTML', HTMLstring5);

          });

        function PDF_Save() {
            $.post('@Url.Content("~/Home/generatePDF/")' + parseInt($("#Id").val()),
                { "__RequestVerificationToken": '@GetAntiXsrfRequestToken()' },
                function (result) {
            });
        }

        function Contract_Save() {
            var tblMain = { "Id": "", "Notes": "" }
            tblMain.Id=$("#Id").val() === undefined ? 0 : parseInt($("#Id").val());
            tblMain.Notes = $("#Notes").val() === undefined ? "" : $("#Notes").val();

             $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: '@Url.Content("~/Home/Create")',
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("XSRF-TOKEN",
                                    $('input:hidden[name="__RequestVerificationToken"]').val());
                            },
                            data: JSON.stringify(tblMain),
                            dataType: 'json',
                            error: function (request) {
                                if (request.responseText == 'True' || request.responseText == true) {
                                    $("#Save").removeAttr("disabled");
                                    $("#Submit").removeAttr("disabled");
                                    if ($("#StatusId").val() == "1") {
                                        alert("Successfully saved!");
                                    } else {
                                        alert("Successfully Approved");
                                    }
                                    window.location.href = '@Url.Content("~/Home/Index")';

                                } else {
                                    alert(request.responseText);
                                }
                            }, success: function (result) {
                                if (result == true || result == "True") {
                                    if ($("#StatusId").val() == "1") {
                                        alert("Successfully saved!");
                                    } else {
                                        alert("Successfully Approved");
                                    }
                                    $("#Save").removeAttr("disabled");
                                    $("#Submit").removeAttr("disabled");
                                    window.location.href = '@Url.Content("~/Home/Index")';

                                }
                                else {
                                    alert("Submission Failed!");
                                    $('#Save').removeAttr("disabled");
                                }
                            }
                        });
        }

        </script>
        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    }
    <br />
    <div id="flip"><img src="~/img/info.png" /><b>Hint(s)</b></div>
    <div id="panel"></div>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>CREATE</legend>
            <table>
                <tr>
                    <td valign="top">
                        <fieldset>
                            <legend>TERM AND RENEWAL</legend>
                            <table>
                                <tr>
                                    <td width="35%">
                                        @Html.TextArea("Notes", null, new { @class = "summernote" })
                                        @Html.ValidationMessageFor(model => model.Notes)
                                    </td>
                                </tr>
                            </table>
                        </fieldset>
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input type="button" aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all" id="Save" name="Save" onclick="Contract_Save()" value="Save" /><input type="button" aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all" id="Save" name="Save" onclick="PDF_Save()" value="Generate as PDF" /> </td>
                </tr>
            </table>
        </fieldset>
    }

    E) Home.cs

    public IActionResult Test(int Id)

    {

    TblTest tblMain = dataContext.TblTest.FirstOrDefault(p => p.Id == Id);

    return View(tblMain);

    }

    public static bool IsSectionProps(ParagraphProperties pPr)

    {

    SectionProperties sectPr = pPr.GetFirstChild<SectionProperties>();

    if (sectPr == null)

    return false;

    else

    return true;

    }

    public static void RemoveSectionBreaks(string filename)

    {

    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true))

    {

    MainDocumentPart mainPart = myDoc.MainDocumentPart;

    List<ParagraphProperties> paraProps = mainPart.Document.Descendants<ParagraphProperties>()

    .Where(pPr => IsSectionProps(pPr)).ToList();

    foreach (ParagraphProperties pPr in paraProps)

    {

    pPr.RemoveChild<SectionProperties>(pPr.GetFirstChild<SectionProperties>());

    }

    mainPart.Document.Save();

    }

    }

    [HttpPost]

    [ValidateAntiForgeryToken]

    public IActionResult generatePDF(int Id)

    {

    ViewData["POResed"] = "activeTab";

    var Header = dataContext.TblTest.Where(a => a.Id.Equals(Id)).Select(a => a).SingleOrDefault();

    List<TblTest> dataReader = dataContext.TblTest.Where(a => a.Id == Id).Select(a => a).ToList();

    var fileName = Id.ToString() + "_" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Today) + ".docx";

    var paths = System.IO.Path.Combine("C:\\Template\\", fileName);

    var location = "";

    if (Header.Id == 0)

    {

    ViewBag.Message = "The Record does not exist!";

    }

    else

    {

    Object oTemplatePath = System.IO.Path.Combine("C:\\Template\\Test.dotx");

    DocumentGeneration2 docGeneration = new DocumentGeneration2(oTemplatePath.ToString(), paths, dataReader);

    DocumentGeneration2.RETURN_VAL returnValue = docGeneration.GenerateDocument2(fileName);

    if (!returnValue.Value)

    {

    location = "";

    }

    else

    {

     

    Spire.Doc.Document doc2 = new Spire.Doc.Document();

    doc2.LoadFromFile(@"" + paths);

    doc2.SaveToFile(paths, Spire.Doc.FileFormat.Docx);

    location = paths;

    }

    string pdf = location;

    if (pdf != "")

    {

    RemoveSectionBreaks(pdf);

    Spire.Doc.Document doc2 = new Spire.Doc.Document();

    Spire.Doc.Section section = doc2.AddSection();

    section.PageSetup.PageSize = Spire.Doc.Documents.PageSize.A4;

    section.PageSetup.Orientation = PageOrientation.Portrait;

    section.PageSetup.Margins.Bottom = 50;

    doc2.LoadFromFile(@"" + pdf);

    var filename = pdf.Replace(".docx", ".pdf");

    doc2.SaveToFile(filename, Spire.Doc.FileFormat.PDF);

    pdf = pdf.Replace(".docx", ".pdf");

    }

    ViewBag.Message = "Successful generated Contract; Email Sent to Creator.";

    }

    return View();

    }

    [HttpPost]

    [ValidateAntiForgeryToken]

    public JsonResult Create([FromBody]TblTest tblMain)

    {

    bool success = false;

    TblTest tblConMains = new TblTest();

    tblConMains.Notes = tblMain.Notes;

    tblConMains.Id = tblMain.Id;

    if (tblConMains.Id == 0)

    {

    dataContext.TblTest.Add(tblConMains);

    }

    else

    {

    dataContext.TblTest.Update(tblConMains);

    }

    try

    {

    dataContext.SaveChanges();

    success = true;

    }

    catch (Exception ex)

    {

    Console.WriteLine(ex.ToString());

    }

    return Json(success.ToString());

    }

    That's all.

    Above is my codes. Hope you can help me.

    I just want what you see from the text editor with pre-formatted will exactly re-write into the template & save as PDF.


    Thanks.

    Regards,

    Micheale

    Wednesday, September 18, 2019 9:34 AM
  • User-893002196 posted

    Hi Expert,

    Anyone can help me please?

    Thanks,

    Regards,

    Micheale

    Friday, September 20, 2019 10:58 AM
  • User-893002196 posted

    Hi All,

    I found out that this nuget able to convert my html code as OpenXML code & how can I integrated with below DocumentGeneration.cs file for Notes field?

    Notes Fields is the Text Editor Fields - WYSWYG Text Editor.

    Install-Package HtmlToOpenXml.dll

    I have this function:-

    private static byte[] HtmlToWord(string html)

    {

    using (MemoryStream memoryStream = new MemoryStream())

    using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(

    memoryStream, WordprocessingDocumentType.Document))

    {

    MainDocumentPart mainPart = wordDocument.MainDocumentPart;

    if (mainPart == null)

    {

    mainPart = wordDocument.AddMainDocumentPart();

    new Document(new Body()).Save(mainPart);

    }

    HtmlConverter converter = new HtmlConverter(mainPart);

    converter.ImageProcessing = ImageProcessing.AutomaticDownload;

    Body body = mainPart.Document.Body;

    IList<OpenXmlCompositeElement> paragraphs = converter.Parse(html);

    body.Append(paragraphs);

    mainPart.Document.Save();

    return memoryStream.ToArray();

    }

    }

    How can I used in this DocumentGeneration.cs class:-

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using A = DocumentFormat.OpenXml.Drawing;
    using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
    using System.Xml.Linq;
    using System.Xml;


    // Additional References
    using Newtonsoft.Json;
    using ProjectName;
    using OpenXmlPowerTools;
    using ProjectName.Models;
    using System.Net;
    using System.Text.RegularExpressions;

    namespace ProjectName
    {

       
        public class DocumentGeneration2
        {
            // Struct to hold the return value and possible exception
            public struct RETURN_VAL
            {
                public bool Value;
                public string Exception;
            }

            private const string FieldDelimeter = " MERGEFIELD ";

            private string _templateFileName;
            private string _targetFileName;
            private string _filename;
            private List<Models.TblTest> _dataReader;

            /// <summary>
            /// Constructor for the DocumentGeneration Class
            /// </summary>
            /// <param name="templateFileName">File to base the document off of</param>
            /// <param name="targetFileName">End File to write the Mail Merge to</param>
            public DocumentGeneration2(string templateFileName, string targetFileName, List<Models.TblTest> dataReader)
            {
                _templateFileName = templateFileName;
                _targetFileName = targetFileName;
                _dataReader = dataReader;
            }

            public void parseTextForOpenXML(Run run, string textualData)
            {
                string[] newLineArray = { Environment.NewLine };
                string[] textArray = textualData.Split(newLineArray, StringSplitOptions.None);

                bool first = true;

                foreach (string line in textArray)
                {
                    if (!first)
                    {
                        run.Append(new Break());
                    }

                    first = false;

                    Text txt = new Text();
                    txt.Text = line;
                    run.Append(txt);
                }
            }

            public void RemovePageBlank(WordprocessingDocument document, string file)
            {
                int count = 0;
                XDocument xdoc = new XDocument();
                xdoc = XDocument.Load(file);
                count += xdoc.Descendants("PAGE").Count();         

                // save the xdoc back to wdoc, not quoted here since this code can be easily found in the internet
                document.MainDocumentPart.Document.Save();
            }

            public static void InsertAPicture(string document, string fileName)
            {
                using (WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(document, true))
                {
                    MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

                    ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

                    using (FileStream stream = new FileStream(fileName, FileMode.Open))
                    {
                        imagePart.FeedData(stream);
                    }

                    AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
                }
            }


            private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
            {
                // Define the reference of the image.
                var element =
                     new Drawing(
                         new DW.Inline(
                             new DW.Extent() { Cx = 6850000L, Cy = 590000L },
                             new DW.EffectExtent()
                             {
                                 LeftEdge = 0L,
                                 TopEdge = 0L,
                                 RightEdge = 0L,
                                 BottomEdge = 0L
                             },
                             new DW.DocProperties()
                             {
                                 Id = (UInt32Value)1U,
                                 Name = "Picture 1"
                             },
                             new DW.NonVisualGraphicFrameDrawingProperties(
                                 new A.GraphicFrameLocks() { NoChangeAspect = true }),
                             new A.Graphic(
                                 new A.GraphicData(
                                     new PIC.Picture(
                                         new PIC.NonVisualPictureProperties(
                                             new PIC.NonVisualDrawingProperties()
                                             {
                                                 Id = (UInt32Value)0U,
                                                 Name = "New Bitmap Image.jpg"
                                             },
                                             new PIC.NonVisualPictureDrawingProperties()),
                                         new PIC.BlipFill(
                                             new A.Blip(
                                                 new A.BlipExtensionList(
                                                     new A.BlipExtension()
                                                     {
                                                         Uri =
                                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                                     })
                                             )
                                             {
                                                 Embed = relationshipId,
                                                 CompressionState =
                                                 A.BlipCompressionValues.Print
                                             },
                                             new A.Stretch(
                                                 new A.FillRectangle())),
                                         new PIC.ShapeProperties(
                                             new A.Transform2D(
                                                 new A.Offset() { X = 0L, Y = 0L },
                                                 new A.Extents() { Cx = 6850000L, Cy = 590000L }),
                                             new A.PresetGeometry(
                                                 new A.AdjustValueList()
                                             )
                                             { Preset = A.ShapeTypeValues.Rectangle }))
                                 )
                                 { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                         )
                         {
                             DistanceFromTop = (UInt32Value)0U,
                             DistanceFromBottom = (UInt32Value)0U,
                             DistanceFromLeft = (UInt32Value)0U,
                             DistanceFromRight = (UInt32Value)0U,
                             EditId = "50D07946"
                         });

                // Append the reference to body, the element should be in a Run.
                //wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
                wordDoc.MainDocumentPart.Document.Body.InsertBefore(new Paragraph(new Run(element)), wordDoc.MainDocumentPart.Document.Body.FirstChild);
            }

            private static void AddImageNoPOToBody(WordprocessingDocument wordDoc, string relationshipId)
            {
                // Define the reference of the image.
                var element =
                     new Drawing(
                         new DW.Inline(
                             new DW.Extent() { Cx = 1010000L, Cy = 812000L },
                             new DW.EffectExtent()
                             {
                                 LeftEdge = 0L,
                                 TopEdge = 0L,
                                 RightEdge = 0L,
                                 BottomEdge = 0L
                             },
                             new DW.DocProperties()
                             {
                                 Id = (UInt32Value)1U,
                                 Name = "Picture 1"
                             },
                             new DW.NonVisualGraphicFrameDrawingProperties(
                                 new A.GraphicFrameLocks() { NoChangeAspect = true }),
                             new A.Graphic(
                                 new A.GraphicData(
                                     new PIC.Picture(
                                         new PIC.NonVisualPictureProperties(
                                             new PIC.NonVisualDrawingProperties()
                                             {
                                                 Id = (UInt32Value)0U,
                                                 Name = "New Bitmap Image.jpg"
                                             },
                                             new PIC.NonVisualPictureDrawingProperties()),
                                         new PIC.BlipFill(
                                             new A.Blip(
                                                 new A.BlipExtensionList(
                                                     new A.BlipExtension()
                                                     {
                                                         Uri =
                                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                                     })
                                             )
                                             {
                                                 Embed = relationshipId,
                                                 CompressionState =
                                                 A.BlipCompressionValues.Print
                                             },
                                             new A.Stretch(
                                                 new A.FillRectangle())),
                                         new PIC.ShapeProperties(
                                             new A.Transform2D(
                                                 new A.Offset() { X = 0L, Y = 0L },
                                                 new A.Extents() { Cx = 1010000L, Cy = 812000L }),
                                             new A.PresetGeometry(
                                                 new A.AdjustValueList()
                                             )
                                             { Preset = A.ShapeTypeValues.Rectangle }))
                                 )
                                 { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                         )
                         {
                             DistanceFromTop = (UInt32Value)0U,
                             DistanceFromBottom = (UInt32Value)0U,
                             DistanceFromLeft = (UInt32Value)0U,
                             DistanceFromRight = (UInt32Value)0U,
                             EditId = "50D07946"
                         });

                // Append the reference to body, the element should be in a Run.
                wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
            }

            private static Paragraph InsertFormatRun(string _text)
            {
                Paragraph _head = new Paragraph();
                Run _run = new Run();
                Text _line = new Text(_text);
                _run.Append(_line);
                _head.Append(_run);
                return _head;
            }


            /// <summary>
            /// Converts the DOTX to DOCX
            /// </summary>
            /// <returns>True or False (with an exception) if successful in converting the document</returns>
            private RETURN_VAL ConvertTemplate()
            {
                try
                {
                    MemoryStream msFile = null;

                    using (Stream sTemplate = File.Open(_templateFileName, FileMode.Open, FileAccess.Read))
                    {
                        msFile = new MemoryStream((int)sTemplate.Length);
                        sTemplate.CopyTo(msFile);
                        msFile.Position = 0L;
                    }

                    using (WordprocessingDocument wpdFile = WordprocessingDocument.Open(msFile, true))
                    {
                        wpdFile.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

                        MainDocumentPart docPart = wpdFile.MainDocumentPart;
                        docPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(_templateFileName, UriKind.RelativeOrAbsolute));

                        docPart.Document.Save();
                    }

                    // Flush the MemoryStream to the file
                    File.WriteAllBytes(_targetFileName, msFile.ToArray());

                    msFile.Close();

                    return new RETURN_VAL { Value = true };
                }
                catch (Exception ex)
                {
                    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::convertTemplate() - " + ex.ToString() };
                }
            }

            /// <summary>
            /// Gets the Mail Merge Value
            /// </summary>
            /// <param name="FieldName">Field Name from the Word Template/Document</param>
            /// <returns>The Mail Merge value, but if the Field could not be found, throw an exception</returns>
            private string GetMergeValue(string FieldName)
            {
                switch (FieldName)
                {
                    case "Notes":
                        return _dataReader[0].Notes == null ? "-" : _dataReader[0].Notes.ToString();               
                   
                    default:
                        throw new Exception(message: "FieldName (" + FieldName + ") was not found");
                }
            }

           

            private static Footer GeneratePageFooterPart(string FooterText)
            {
                var element =
                    new Footer(
                        new Paragraph(
                            new ParagraphProperties(
                                new ParagraphStyleId() { Val = "Footer" }),
                            new Run(
                                new DocumentFormat.OpenXml.Wordprocessing.Text(FooterText))
                        ));

                return element;
            }

            private static Header GeneratePageHeaderPart(string HeaderText)
            {
                var element =
                    new Header(
                        new Paragraph(
                            new ParagraphProperties(
                                new ParagraphStyleId() { Val = "Header" }),
                            new Run(
                                new DocumentFormat.OpenXml.Wordprocessing.Text(HeaderText))
                        ));

                return element;
            }

            private static RunProperties GetRunPropertyFromTableCell(DocumentFormat.OpenXml.Wordprocessing.TableRow rowCopy, int cellIndex)
            {
                var runProperties = new RunProperties();
                var fontname = "Arial";
                var fontSize = "20";
                try
                {
                    fontname =
                        rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>()
                           .ElementAt(cellIndex)
                           .GetFirstChild<Paragraph>()
                           .GetFirstChild<ParagraphProperties>()
                           .GetFirstChild<ParagraphMarkRunProperties>()
                           .GetFirstChild<RunFonts>()
                           .Ascii;
                }
                catch
                {
                    //swallow
                }
                try
                {
                    fontSize =
                           rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>()
                              .ElementAt(cellIndex)
                              .GetFirstChild<Paragraph>()
                              .GetFirstChild<ParagraphProperties>()
                              .GetFirstChild<ParagraphMarkRunProperties>()
                              .GetFirstChild<FontSize>()
                              .Val;
                }
                catch
                {
                    //swallow
                }
                runProperties.AppendChild(new RunFonts() { Ascii = fontname });
                runProperties.AppendChild(new FontSize() { Val = fontSize });
                return runProperties;
            }


            private static readonly System.Text.RegularExpressions.Regex instructionRegEx =
                   new System.Text.RegularExpressions.Regex(
                               @"^[\s]*MERGEFIELD[\s]+(?<name>[#\w]*){1}               # This retrieves the field's name (Named Capture Group -> name)
                                   [\s]*(\\\*[\s]+(?<Format>[\w]*){1})?                # Retrieves field's format flag (Named Capture Group -> Format)
                                   [\s]*(\\b[\s]+[""]?(?<PreText>[^\\]*){1})?         # Retrieves text to display before field data (Named Capture Group -> PreText)
                                                                                       # Retrieves text to display after field data (Named Capture Group -> PostText)
                                   [\s]*(\\f[\s]+[""]?(?<PostText>[^\\]*){1})?",
                               System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.ExplicitCapture | System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Singleline);

            private static string GetFieldName(SimpleField field, out string[] switches)
            {
                var a = field.GetAttribute("instr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                switches = new string[0];
                string fieldname = string.Empty;
                string instruction = a.Value;

                if (!string.IsNullOrEmpty(instruction))
                {
                    System.Text.RegularExpressions.Match m = instructionRegEx.Match(instruction);
                    if (m.Success)
                    {
                        fieldname = m.Groups["name"].ToString().Trim();
                        int pos = fieldname.IndexOf('#');
                        if (pos > 0)
                        {
                            // Process the switches, correct the fieldname.
                            switches = fieldname.Substring(pos + 1).ToLower().Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
                            fieldname = fieldname.Substring(0, pos);
                        }
                    }
                }

                return fieldname;
            }


            private static byte[] HtmlToWord(string html, string fileName)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(
                    memoryStream, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = wordDocument.MainDocumentPart;
                    if (mainPart == null)
                    {
                        mainPart = wordDocument.AddMainDocumentPart();
                        new Document(new Body()).Save(mainPart);
                    }

                    HtmlConverter converter = new HtmlConverter(mainPart);
                    converter.ImageProcessing = ImageProcessing.AutomaticDownload;
                    Body body = mainPart.Document.Body;

                    IList<OpenXmlCompositeElement> paragraphs = converter.Parse(html);
                    body.Append(paragraphs);

                    mainPart.Document.Save();
                    return memoryStream.ToArray();
                }

            }

            //public static byte[] HtmlToWord(String html)
            //{
            //    const string filename = "test.docx";
            //    //if (File.Exists(filename)) File.Delete(filename);

            //    using (MemoryStream generatedDocument = new MemoryStream())
            //    {
            //        using (WordprocessingDocument package = WordprocessingDocument.Create(
            //               generatedDocument, WordprocessingDocumentType.Document))
            //        {
            //            MainDocumentPart mainPart = package.MainDocumentPart;
            //            if (mainPart == null)
            //            {
            //                mainPart = package.AddMainDocumentPart();
            //                new Document(new Body()).Save(mainPart);
            //            }

            //            HtmlConverter converter = new HtmlConverter(mainPart);
            //            Body body = mainPart.Document.Body;

            //            var paragraphs = converter.Parse(html);
            //            for (int i = 0; i < paragraphs.Count; i++)
            //            {
            //                body.Append(paragraphs[i]);
            //            }

            //            mainPart.Document.Save();
            //        }

            //        return generatedDocument.ToArray();
            //    }
            //}


            /// <summary>
            /// Generates the Word Document and performs the Mail Merge
            /// </summary>
            /// <returns>True or false(with exception) if the generation was successful or not</returns>
            public RETURN_VAL GenerateDocument2(string fileName)
            {
                try
                {
                    _filename = fileName;
                    // Don't continue if the template file name is not found
                    if (!File.Exists(_templateFileName))
                    {
                        throw new Exception(message: "TemplateFileName (" + _templateFileName + ") does not exist");
                    }

                    // If the file is a DOTX file convert it to docx
                    if (_templateFileName.ToUpper().EndsWith("DOTX"))
                    {
                        RETURN_VAL resultValue = ConvertTemplate();

                        if (!resultValue.Value)
                        {
                            return resultValue;
                        }
                    }
                    else
                    {
                        // Otherwise make a copy of the Word Document to the targetFileName
                        File.Copy(_templateFileName, _targetFileName);
                    }

                   
                    using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(_targetFileName, true))
                    {
                        docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

                        foreach (FieldCode field in docGenerated.MainDocumentPart.RootElement.Descendants<FieldCode>())
                        {
                            var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);
                            var fieldname = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();

                            var fieldValue = GetMergeValue(FieldName: fieldname);

                            // Go through all of the Run elements and replace the Text Elements Text Property
                            foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())
                            {
                                foreach (DocumentFormat.OpenXml.Wordprocessing.Text txtFromRun in run.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where(a => a.Text == "«" + fieldname + "»"))
                                {
                                    txtFromRun.Text = fieldValue.ToString();
                                }
                            }
                        }
                     
                    }
                return new RETURN_VAL { Value = true };
                }
                catch (Exception ex)
                {
                    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::generateDocument2() - " + ex.ToString() };
                }
            }
        }
    }

    How can I called HtmlToWord into Notes field as below [  var fieldValue = GetMergeValue(FieldName: fieldname);  ]:-

    using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(_targetFileName, true))

    {

    docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (FieldCode field in docGenerated.MainDocumentPart.RootElement.Descendants<FieldCode>())

    {

    var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);

    var fieldname = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();

    var fieldValue = GetMergeValue(FieldName: fieldname);

     

     

    // Go through all of the Run elements and replace the Text Elements Text Property

    foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())

    {

    foreach (DocumentFormat.OpenXml.Wordprocessing.Text txtFromRun in run.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where(a => a.Text == "«" + fieldname + "»"))

    {

    txtFromRun.Text = fieldValue.ToString();

    }

    }

    }

    }

     

    Please advise.

    Thanks.

    Regards,

    Micheale

    Monday, September 23, 2019 1:24 AM
  • User-893002196 posted

    Hi Expert,

    Basically I have a .dotx file with 2 fields:

    1. template.dotx as below:-

    Name: <<names>>

    Notes:

    <<Notes>>

    2. I have Form UI

    Registration Form

    Names: [      Texbox    ] 

    Notes: [    RichText Editor - WYSWYG   ]    

    [ Generate Receipt Button] 

    Example values

    Names : [ Micheale ]

    Notes:

    ----- Richtext Headers with all the function here -----

    Company Contact Country
    Alfreds Futterkiste Maria Anders Germany
    Centro comercial Moctezuma Francisco Chang Mexico
    Ernst Handel Roland Mendel Austria
    Island Trading Helen Bennett UK
    Laughing Bacchus Winecellars Yoshi Tannamuri Canada
    Magazzini Alimentari Riuniti Giovanni Rovelli Italy

    3. Once the Generate Receipt Button click on, .docx expecting to be generate via the template as below, result.docx:

    Name: Micheale

    Notes:

    Company Contact Country
    Alfreds Futterkiste Maria Anders Germany
    Centro comercial Moctezuma Francisco Chang Mexico
    Ernst Handel Roland Mendel Austria
    Island Trading Helen Bennett UK
    Laughing Bacchus Winecellars Yoshi Tannamuri Canada
    Magazzini Alimentari Riuniti Giovanni Rovelli Italy

    Hope someone expert here can help me. I am stuck.

    My codes which not able to complete:

    public RETURN_VAL GenerateDocument2(string fileName)

    {

    try

    {

    _filename = fileName;

    // Don't continue if the template file name is not found

    if (!File.Exists(_templateFileName))

    {

    throw new Exception(message: "TemplateFileName (" + _templateFileName + ") does not exist");

    }

    // If the file is a DOTX file convert it to docx

    if (_templateFileName.ToUpper().EndsWith("DOTX"))

    {

    RETURN_VAL resultValue = ConvertTemplate();

    if (!resultValue.Value)

    {

    return resultValue;

    }

    }

    else

    {

    // Otherwise make a copy of the Word Document to the targetFileName

    File.Copy(_templateFileName, _targetFileName);

    }

    using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(_targetFileName, true))

    {

    docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (FieldCode field in docGenerated.MainDocumentPart.RootElement.Descendants<FieldCode>())

    {

    var fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal);

    var fieldname = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim();

    var fieldValue = GetMergeValue(FieldName: fieldname);

     

     

    // Go through all of the Run elements and replace the Text Elements Text Property

    foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())

    {

    foreach (DocumentFormat.OpenXml.Wordprocessing.Text txtFromRun in run.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where(a => a.Text == "«" + fieldname + "»"))

    {

    if (fieldname == "Notes")

    {

    var tb = "<table style='width:100%'><tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr><tr><td>Jill</td><td>Smith</td> <td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";

    ??????????????  WHAT SHOULD I NEED TO DO HERE ???????????????

    }

    else

    {

    txtFromRun.Text = fieldValue.ToString();

    }

    }

    }

    }

    }

    return new RETURN_VAL { Value = true };

    }

    catch (Exception ex)

    {

    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::generateDocument2() - " + ex.ToString() };

    }

    }

    Thanks.

    Regards,

    Micheale

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 7:56 AM
  • User-893002196 posted

    Hi All,

    Finally my issue resolved. Here are the skeleton example solution:-

    //This will be pass from Rich Text Editor, but for testing purposes. I hardcoded the html code here

    var htmlcode = "<p><br></p><table><tbody><tr><td><b>Fee Type</b></td><td><b>Product/Service Type</b></td><td><b>Invoicing Schedule</b></td></tr><tr><td rowspan='2'>Initial Fee</td><td>Initial Software License Fees</td><td rowspan='2'>Invoiced on the Order Form Effective Date</td></tr><tr><td>Initial SaaS Product Fees</td></tr><tr><td>Rental Model Fee</td><td>Rental Model License Fee</td><td>Invoiced on the Order Form Effective Date for the entire rental term.</td></tr><tr><td rowspan='3'>Monthly Fee</td><td>Monthly Software License Fees</td><td rowspan='2'>Invoiced on the Order Form Effective Date. Subsequent monthly invoices will be provided on or before the fifteenth (15th) day during each calendar month. Invoices are provided monthly in advance.</td></tr><tr><td>Monthly SaaS Product Fees</td></tr><tr><td>Monthly Service Fees</td><td>Invoiced on or before the fifteenth (15th) of each calendar month for services performed during the prior calendar month.</td></tr><tr><td rowspan='4'>Annual Fee</td><td>Annual Software License Fees</td><td rowspan='4'>Invoiced on the Order Form Effective Date. If there are renewal terms, invoices will be provided on or before the fifteenth (15th) day following each anniversary of the Order Form Effective Date. Invoices are provided annually in advance.</td></tr><tr><td>Annual SaaS Product Fees</td></tr><tr><td>Annual Support Fees</td></tr><tr><td>SimSci Tokens</td></tr><tr><td>On-Demand Fee</td><td>CALM or UBL Tokens</td><td>Invoiced on purchase.</td></tr></tbody></table>";

    var paths = System.IO.Path.Combine("C:\\Scripts\\Template\\", "Testing.dotx");

    System.IO.File.Copy(paths, paths.Replace(".dotx",".docx"), true);

    string _targetFileName = paths.Replace(".dotx",".docx");

    using (WordprocessingDocument doc = WordprocessingDocument.Open("C:\\Scripts\\Template\\test.docx", true))

    {

    string altChunkId = "myId";

    MainDocumentPart mainDocPart = doc.MainDocumentPart;

    var run = new Run(new Text("test"));

    var p = new Paragraph(new ParagraphProperties(

    new Justification() { Val = JustificationValues.Center }),

    run);

    var body = mainDocPart.Document.Body;

    body.Append(p);

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(htmlcode));

    AlternativeFormatImportPart formatImportPart =

    mainDocPart.AddAlternativeFormatImportPart(

    AlternativeFormatImportPartType.Html, altChunkId);

    formatImportPart.FeedData(ms);

    AltChunk altChunk = new AltChunk();

    altChunk.Id = altChunkId;

     

    var mergeField = body.Descendants<FieldCode>().Where(fc => fc.Text.Trim().Replace(" ", "") == "MERGEFIELDNotes").FirstOrDefault();

    var para = mergeField.Ancestors<Paragraph>().First();

    para.Append(altChunk);

    mergeField.Remove();

    doc.Save();

    That's it. Basically I need to apply above code to my DocumentGenerations.cs.

    Thanks.

    Regards,

    Micheale

    Tuesday, September 24, 2019 10:54 AM