コード例 #1
0
        static void Main(string[] args)
        {
            string path = Directory.GetCurrentDirectory() + @"\Document\TestConvention.docx";

            if (!File.Exists(path))
            {
                string pathCopy = Directory.GetCurrentDirectory() + @"\Document\Copy.docx";
                File.Copy(path, pathCopy);
                //using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
                //{

                //}
                if (!File.Exists(pathCopy))
                {
                    return;
                }
                using (WordprocessingDocument doc = WordprocessingDocument.Open(pathCopy, true))
                {
                    IEnumerable <FieldCode> docWorld = OpenXmlWordHelpers.GetMergeFields(doc);
                    Entreprise ent = Program._context.Entreprises.First();
                    foreach (FieldCode item in docWorld)
                    {
                        if (item.InnerXml.Contains("Nom_Entreprise"))
                        {
                            item.ReplaceWithText(ent.RaisonSociale);
                        }

                        if (item.InnerXml.Contains("Adresse_entreprise"))
                        {
                            item.ReplaceWithText(ent.NumeroNomVoieEntreprise);
                        }

                        //if (item.InnerXml.Contains("Adresse_entreprise_suite"))
                        //{
                        //    if (ent.ComplementAdresseEntreprise == string.Empty)
                        //        item.ReplaceWithText("0");
                        //    else
                        //        item.ReplaceWithText(ent.ComplementAdresseEntreprise);
                        //}

                        if (item.InnerXml.Contains("Code_postal_entreprise"))
                        {
                            item.ReplaceWithText(ent.CodePostalEntreprise);
                        }

                        if (item.InnerXml.Contains("Ville_entreprise"))
                        {
                            item.ReplaceWithText(ent.VilleEntreprise);
                        }

                        if (item.InnerXml.Contains("Téléphone_entreprise"))
                        {
                            item.ReplaceWithText("");
                        }
                    }
                }
            }

            string toto = HashPassword("toto");
        }
コード例 #2
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
        {
            log.LogInformation("Procesing template");

            try
            {
                var data = await Helpers.GetModelFromBodyAsync <TemplateModel>(req.Body);

                if (data == null)
                {
                    log.LogWarning("Posted data are not correct");
                    return(new BadRequestObjectResult("Posted data are not correct"));
                }

                var container = await Helpers.GetContainerAsync(Environment.GetEnvironmentVariable(Constants.TemplatesContainerName));

                var blob = container.GetBlockBlobReference(data.BlobName);

                var fields = new List <string>();
                using (var sourceStream = new MemoryStream())
                {
                    await blob.DownloadToStreamAsync(sourceStream);

                    using (WordprocessingDocument doc = WordprocessingDocument.Open(sourceStream, false))
                    {
                        foreach (var field in doc.GetMergeFields())
                        {
                            string fieldName = OpenXmlWordHelpers.GetFieldNameFromMergeField(field.InnerText).Trim('\"');
                            if (!fields.Contains(fieldName) && fieldName.Trim() != "PAGE")
                            {
                                fields.Add(fieldName);
                            }
                        }
                    }
                }

                blob.Metadata.Add("name", Uri.EscapeDataString(data.Name));

                if (!string.IsNullOrEmpty(data.Description))
                {
                    blob.Metadata.Add("description", Uri.EscapeDataString(data.Description));
                }

                string fieldsString = string.Join(";", fields);
                if (fields.Count > 0)
                {
                    blob.Metadata.Add("fields", Uri.EscapeDataString(fieldsString));
                }
                else
                {
                    log.LogWarning("No fields were found");
                }

                await blob.SetMetadataAsync();

                log.LogInformation("Template processed");

                return(new OkObjectResult(new TemplateModel()
                {
                    Name = data.Name,
                    BlobName = data.BlobName,
                    Description = data.Description,
                    Fields = Helpers.GetFieldsWithoutHiddenArray(fieldsString)
                }));
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                throw ex;
            }
        }