public static void Create(DB_Connector connection, string templateFile, string resultFile, uint id, bool readOnly = false) { #region Contracts CheckConnectionAndFilenames(connection, templateFile, resultFile); #endregion XDocument template = XDocument.Load(templateFile, LoadOptions.PreserveWhitespace); Validate(template); if (template.Root.Element("Document").Element("Word") != null) { Novacode.DocX doc = Word.CreateFromTemplate(connection, GetFonts(template.Root.Element("Fonts")), template.Root.Element("Document").Element("Word"), id, resultFile); if (readOnly) { doc.AddProtection(Novacode.EditRestrictions.readOnly); } doc.Save(); } else { throw new System.ArgumentException("Эта перегрузка принимат только тип шаблона \"Word\".", nameof(templateFile)); } }
public NameValueCollection CreateDocxMassive(ExcelDocumentReader excelDocumentReader, NameValueCollection collectionForReplace, string sPathTemplateDocx, string sColumnIdName, out string errTags) { NameValueCollection oResult = new NameValueCollection(); try { errTags = ""; string cfSelected = string.Empty; var columnNames = excelDocumentReader.GetColumnNames(); ResourceFileManager resourceFileManager = ResourceFileManager.Instance; resourceFileManager.SetResources(); string sColumnIdSubject = resourceFileManager.getConfigData(sColumnIdName); //var getData = excelDocumentReader.getData<LinqToExcel.Row>(); // Ciclo i singoli record estrapolati dal file excel Int16 iIndiceFile = 1; foreach (var a in excelDocumentReader.getData) { //Apro il FileStyleUriParser template DocX da valorizzare using (Novacode.DocX document = Novacode.DocX.Load(sPathTemplateDocx)) { // Ciclo tra i nomi delle colonne foreach (var columnName in columnNames) { // sostituisco il valore dei nomi colonne formattandole come TAG che sia suppone sia presente nel template DOCX document.ReplaceText("«" + columnName + "»", a[columnName].ToString()); if (columnName == sColumnIdSubject) { cfSelected = a[columnName].ToString(); } } for (int i = 0; i < collectionForReplace.Count; i++) { document.ReplaceText("«" + collectionForReplace.GetKey(i) + "»", collectionForReplace.GetValues(i)[0]); } // Verifico che tutti i TAG presenti all'interno del file docx siano stati valorizzati var lineBreaks = document.FindUniqueByPattern("«*\\b[^»]*»", System.Text.RegularExpressions.RegexOptions.None); // Qualora siano presenti restituisco i tag non ripristinati come segnalazione di una anomalia if (lineBreaks.Count > 0) { foreach (var tagErr in lineBreaks) { errTags += tagErr + " "; } throw new ArgumentException("Sono stati trovati i seguenti tag non sostituiti: " + errTags); //break; } else { document.SaveAs(excelDocumentReader.sTransactionPath + iIndiceFile + ".docx"); oResult.Add(cfSelected, excelDocumentReader.sTransactionPath + iIndiceFile + ".docx"); } } iIndiceFile += 1; } } catch (Exception ex) { throw new ArgumentException(ex.Message); } finally { } return(oResult); }
public static void Create(string resultFile, IEnumerable <DocumentParameters> documents, bool readOnly = false) { #region Contracts if (string.IsNullOrWhiteSpace(resultFile)) { throw new System.ArgumentException("Некорректное имя выходного файла.", nameof(resultFile)); } if (System.Linq.Enumerable.Count(documents) == 0) { throw new System.ArgumentException("Коллекция с документами должна содержать хотя бы один элемент.", nameof(documents)); } #endregion Novacode.DocX doc = null; foreach (var document in documents) { XDocument template = XDocument.Load(document.Template, LoadOptions.PreserveWhitespace); Validate(template); if (template.Root.Element("Document").Element("Word") != null) { Novacode.DocX buf; if (document.Connection != null) { buf = Word.CreateFromTemplate(document.Connection, GetFonts(template.Root.Element("Fonts")), template.Root.Element("Document").Element("Word"), document.ID.Value, resultFile); } else { buf = Word.CreateFromTemplate(GetFonts(template.Root.Element("Fonts")), template.Root.Element("Document").Element("Word"), document.SingleParameters, document.TableParameters, resultFile); } if (doc == null) { doc = buf; } else { doc.InsertSectionPageBreak(); doc.InsertDocument(buf); } } else { throw new System.ArgumentException("Эта перегрузка принимат только тип шаблонов \"Word\".", nameof(documents)); } if (readOnly) { doc.AddProtection(Novacode.EditRestrictions.readOnly); } doc.Save(); } }
public void AddParagraph() { Novacode.DocX d = Novacode.DocX.Create("thing.docx"); List <List <string> > p = new List <List <string> >(); p.Add(new List <string>()); p[0].Add("let's get TeXy with {1} and {2}"); p[0].Add("\\textbf{Bold bits}"); p[0].Add("\\textit{italic bits}"); WordifyThings.AddParagraph(p, d); d.Save(); }
public void rlapTest() { Novacode.DocX d = Novacode.DocX.Create(@"rlap.docx"); List <List <string> > tex = new List <List <string> >(); tex.Add(new List <string>()); tex[0].Add("`This is a quoted sentence{0}"); tex[0].Add("\\rlap{.}{'}"); WordifyThings.AddParagraph(tex, d); d.Save(); }
public static void Create(string templateFile, string resultFile, string[] singleParams, IEnumerable <string[]>[] tableParams, bool readOnly = false) { #region Contracts CheckFilenames(templateFile, resultFile); if (singleParams == null && tableParams == null) { throw new System.ArgumentException("Необходимо передать хотя бы один одиночный или табличный параметр."); } if (singleParams != null && singleParams.Length == 0) { throw new System.ArgumentException("Массив с одиночными параметрами должен содержать хотя бы один элемент.", nameof(singleParams)); } if (tableParams != null && tableParams.Length == 0) { throw new System.ArgumentException("Массив с табличными параметрами должен содержать хотя бы один элемент.", nameof(tableParams)); } #endregion XDocument template = XDocument.Load(templateFile, LoadOptions.PreserveWhitespace); Validate(template); if (template.Root.Element("Document").Element("Word") != null) { Novacode.DocX doc = Word.CreateFromTemplate(GetFonts(template.Root.Element("Fonts")), template.Root.Element("Document").Element("Word"), singleParams, tableParams, resultFile); if (readOnly) { doc.AddProtection(Novacode.EditRestrictions.readOnly); } doc.Save(); } else { throw new System.ArgumentException("Эта перегрузка принимат только тип шаблона \"Word\".", nameof(templateFile)); } }