Exemplo n.º 1
0
        /// <summary>
        /// genearate document
        /// </summary>
        /// <param name="fileByte"></param>
        /// <param name="documentContent"></param>
        /// <returns></returns>
        public byte[] GenerateDocument(byte[] fileByte, List <DocContent> documentContent, string outputFileType)
        {
            #region logging
            //System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Date : " + DateTime.Now.ToString());
            //System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Input FileBytes : " +  fileByte.Length.ToString());
            //System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Memory created : " + fileMemoryStream.Length.ToString());
            //System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Add parameters and value" + documentTemplate.Sections.Count.ToString());
            //System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Saving docio : " + documentTemplate.Document.Count.ToString());
            ///System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Save success memory stream length : " + docIoMemoryStream.Length.ToString());
            //System.IO.File.AppendAllText(@"C:\ErrorLog.txt", "\r\n Save success pdf memory stream length : " + docIoPdfMemoryStream.Length.ToString());
            #endregion

            byte[] docIoFileByte = null;
            if (fileByte != null)
            {
                MemoryStream fileMemoryStream = new MemoryStream();
                fileMemoryStream.Write(fileByte, 0, fileByte.Length);
                _documentTemplate = new WordDocument(fileMemoryStream);

                //read the text parameters values and placeholders to be replacedin document
                foreach (DocContent itemDocContent in documentContent)
                {
                    _documentTemplate.Replace(itemDocContent.ContentKey, itemDocContent.ContentText, true, true);
                }
                MemoryStream docIoMemoryStream = null;
                if (outputFileType.ToUpper() == PdfExtension.ToUpper())
                {
                    docIoMemoryStream = GetPDFStream();
                }
                else if (outputFileType.ToUpper() == DocExtension.ToUpper())
                {
                    docIoMemoryStream = GetWordStream();
                }
                docIoMemoryStream.Seek(0, SeekOrigin.Begin);
                docIoFileByte = docIoMemoryStream.ToArray();
                docIoMemoryStream.Close();
            }
            return(docIoFileByte);
        }