private void CreateSubFlowWordCopy(string originalDocFilePath, string duplicateDocFilePath) { using (WordprocessingDocument originalDoc = WordprocessingDocument.Open(originalDocFilePath, false)) using (WordprocessingDocument duplicateDoc = WordprocessingDocument.Create(duplicateDocFilePath, WordprocessingDocumentType.Document)) { foreach (var part in originalDoc.Parts) { duplicateDoc.AddPart(part.OpenXmlPart, part.RelationshipId); } } }
/// <summary> /// Opens the document and creates a copy of it. Saves the "toPath" to an internal property. /// </summary> /// <param name="fromPath">Original template's destination</param> /// <param name="toPath">New file's destionation</param> public void OpenDocument(string fromPath, string toPath) { using (WordprocessingDocument sourceDoc = WordprocessingDocument.Open(fromPath, false)) using (WordprocessingDocument toDoc = WordprocessingDocument.Create(toPath, WordprocessingDocumentType.Document)) { foreach (var part in sourceDoc.Parts) { toDoc.AddPart(part.OpenXmlPart, part.RelationshipId); } Console.WriteLine("Copy document has been created!"); } path = toPath; }
public MyWord(string documentToClonePath, string newDocumentPath) { this.path = newDocumentPath; using (var mainDoc = WordprocessingDocument.Open(documentToClonePath, false)) { this.wordProcessingDocument = WordprocessingDocument.Create(new MemoryStream(), WordprocessingDocumentType.Document); // copy parts from source document to new document foreach (var part in mainDoc.Parts) { wordProcessingDocument.AddPart(part.OpenXmlPart, part.RelationshipId); } // Save не работает -- почему? //this.wordProcessingDocument.Dispose(); //this.wordProcessingDocument = WordprocessingDocument.Open(newDocumentPath, true); this.mainDocumentPart = wordProcessingDocument.MainDocumentPart; this.body = this.wordProcessingDocument.MainDocumentPart.Document.Body; } }
public IActionResult OpenEdit(Processo modelo) { //string modelo = Path.Combine(@"c:\word\ABC.docx"); var modelFile = FileInputUtil.GetFileInfo("Data", "ABC.docx").FullName; using (MemoryStream memoryRepository = new MemoryStream()) // => Cria um repositório em memória { using (WordprocessingDocument doc = WordprocessingDocument.Open(modelFile, false)) // => faz a leitura do doc modelo using (WordprocessingDocument newDoc = WordprocessingDocument.Create(memoryRepository, WordprocessingDocumentType.Document)) // => instancia um novo docx no repositório em memória { foreach (var part in doc.Parts) { newDoc.AddPart(part.OpenXmlPart, part.RelationshipId); // => copia o texto do docx modelo para o novo docx em memória } var document = newDoc.MainDocumentPart.Document; // => Separa o texto do novo docx em partes para a leitura foreach (var text in document.Descendants <Text>()) { if (text.Text.Contains("101")) // => Nesse bloco são identificados os caracteres e substituídos caso existam no texto { text.Text = text.Text.Replace("101", modelo.Vara); } if (text.Text.Contains("102")) { text.Text = text.Text.Replace("102", modelo.Comarca); } if (text.Text.Contains("103")) { text.Text = text.Text.Replace("103", modelo.NrProcessoCnj); } if (text.Text.Contains("104")) { text.Text = text.Text.Replace("104", modelo.BancoNome); } if (text.Text.Contains("105")) { text.Text = text.Text.Replace("105", modelo.Acao); } if (text.Text.Contains("106")) { text.Text = text.Text.Replace("106", modelo.ClienteNome); } if (text.Text.Contains("107")) { text.Text = text.Text.Replace("107", modelo.TotalDivida.ToString("C") + "(" + ConverteParaExtenso.ValorParaExtenso2(modelo.TotalDivida) + ")"); } if (text.Text.Contains("108")) { text.Text = text.Text.Replace("108", modelo.ValorEntrada.ToString("C") + "(" + ConverteParaExtenso.ValorParaExtenso2(modelo.ValorEntrada) + ")"); } if (text.Text.Contains("109")) { text.Text = text.Text.Replace("109", modelo.DataVencimento.ToString("dd/MM/yyyy")); } if (text.Text.Contains("110")) { text.Text = text.Text.Replace("110", modelo.NrParcelas.ToString() + "(" + ConverteParaExtenso.NumeroParaExtenso(modelo.NrParcelas) + ")"); } if (text.Text.Contains("111")) { text.Text = text.Text.Replace("111", modelo.ValorParcela.ToString("C") + "(" + ConverteParaExtenso.ValorParaExtenso2(modelo.ValorParcela) + ")"); } } } return(File(memoryRepository.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Documento.docx")); // => Faz o dowload do docx em memória } }