/// <summary> /// Adds a new paragraph in collection. /// </summary> /// <returns>Added <see cref="SCParagraph" /> instance.</returns> public IParagraph Add() { // Create a new paragraph from the last paragraph and insert at the end A.Paragraph lastAParagraph = this.paragraphs.Value.Last().AParagraph; A.Paragraph newAParagraph = (A.Paragraph)lastAParagraph.CloneNode(true); lastAParagraph.InsertAfterSelf(newAParagraph); var newParagraph = new SCParagraph(newAParagraph, this.textBox) { Text = string.Empty }; this.paragraphs.Reset(); return(newParagraph); }
public void generaPreventivo(Preventivo prev, List<PreventivoRiga> righe) { _preventivo = prev; _preventivoRighe = righe; //declare and open a Word document object WordprocessingDocument objWordDocx; System.IO.File.Copy(_TemplatePath, _DestinationPath, true); objWordDocx = WordprocessingDocument.Open(_DestinationPath, true); _MainPart = objWordDocx.MainDocumentPart; //get the main document section of the document OpenXmlElement objMainDoc; objMainDoc = _MainPart.Document; #region fieldMerge //fill a dummy dictionary of values to fill the template Dictionary<string, string> values = new Dictionary<string, string>(); values["RagioneSociale"] = _preventivo.RagioneSociale; values["Indirizzo01"] = _preventivo.Indirizzo01; values["Indirizzo02"] = _preventivo.Indirizzo02; values["Oggetto"] = _preventivo.Oggetto; values["ModalitaPagamento"] = _preventivo.ModalitaPagamento; values["TrasportoMontaggio"] = _preventivo.TrasportoMontaggio; values["validitaOfferta"] = _preventivo.ValiditaOfferta; values["UtenteReferente"] = _preventivo.UtenteReferente; values["TelefonoReferente"] = _preventivo.TelefonoReferente; values["DataPreventivo"] = _preventivo.DataPreventivo; //Loop through merge fields foreach (var objField in objMainDoc.Descendants<SimpleField>()) { //Clean the field name string strFieldName = GetFieldName(objField); if (!string.IsNullOrEmpty(strFieldName)) { //check if we have a value for this merge field if (values.ContainsKey(strFieldName) && !string.IsNullOrEmpty(values[strFieldName])) { //Find the XML placeholder string strRunProp = null; if (objField != null) { foreach (RunProperties objRP in objField.Descendants<RunProperties>()) { strRunProp = objRP.OuterXml; break; // break at first } } Run objRun = new Run(); if (!string.IsNullOrEmpty(strRunProp)) { objRun.Append(new RunProperties(strRunProp)); } //add the text to the place holder objRun.Append(new Text(values[strFieldName])); //replace the merge field with the value objField.Parent.ReplaceChild<SimpleField>(objRun, objField); } } } #endregion #region Create Table var res = from bm in objWordDocx.MainDocumentPart.Document.Body.Descendants<BookmarkStart>() where bm.Name == "CORPO" select bm; var bookmark = res.SingleOrDefault(); if (bookmark != null) { var parent = bookmark.Parent; // bookmark's parent element // simple paragraph in one declaration //Paragraph newParagraph = new Paragraph(new Run(new Text("Hello, World!"))); // build paragraph piece by piece //Text text = new Text("Contenuto"); Run run = new Run(new RunProperties(new Bold())); //run.Append(text); Paragraph newParagraph = new Paragraph(run); // insert after bookmark parent parent.InsertAfterSelf(newParagraph); // insert after new paragraph newParagraph.InsertAfterSelf(GenerateTable()); } #endregion //save this part objWordDocx.MainDocumentPart.Document.Save(); //save and close the document objWordDocx.Close(); //Open the document //System.Diagnostics.Process.Start(_DestinationPath); //InsertAPicture(_DestinationPath, _ImagePath); }