/// <summary> /// Export the UI data to Word document. /// </summary> /// <param name="invoiceItems">The InvoiceItems.</param> /// <param name="billInfo">The BillingInformation.</param> /// <param name="totalDue">The TotalDue.</param> public void CreateWord(IList <InvoiceItem> invoiceItems, BillingInformation billInfo, double totalDue) { //Load Template document stream Stream inputStream = new FileStream("Assets/Template.docx", FileMode.Open, FileAccess.Read); //Create instance WordDocument document = new WordDocument(); //Open Template document document.Open(inputStream, FormatType.Word2013); //Dispose input stream inputStream.Dispose(); //Set Clear Fields to false document.MailMerge.ClearFields = false; //Create Mail Merge Data Table MailMergeDataTable mailMergeDataTable = new MailMergeDataTable("Invoice", invoiceItems); //Executes mail merge using the data generated in the app. document.MailMerge.ExecuteGroup(mailMergeDataTable); //Mail Merge Billing information string[] fieldNames = { "Name", "Address", "Date", "InvoiceNumber", "DueDate", "TotalDue" }; string[] fieldValues = { billInfo.Name, billInfo.Address, billInfo.Date.ToString("d"), billInfo.InvoiceNumber, billInfo.DueDate.ToString("d"), totalDue.ToString("#,###.00", CultureInfo.InvariantCulture) }; document.MailMerge.Execute(fieldNames, fieldValues); //Create Output file in Document library location document.Save("Invoice.docx", FormatType.Word2013); document.Close(); }
private MailMergeDataTable GetTestOrderDetails(int TestOrderId) { List <TestOrderDetail> orders = new List <TestOrderDetail>(); FileStream fs = new FileStream(_hostingEnvironment.WebRootPath + @"/DocIO/TestOrderDetails.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "TestOrderDetails") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "TestOrderDetails") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "TestOrderDetail": TestOrderDetail testOrder = GetTestOrderDetail(reader); if (testOrder.OrderID == TestOrderId.ToString()) { orders.Add(testOrder); break; } break; } } else { reader.Read(); if ((reader.LocalName == "TestOrderDetails") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable = new MailMergeDataTable("Order", orders); reader.Dispose(); fs.Dispose(); return(dataTable); }
private MailMergeDataTable GetTestOrder(int TestOrderId) { List <TestOrder> orders = new List <TestOrder>(); FileStream fs = new FileStream(@"wwwroot/data/docio/test-order.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "TestOrders") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "TestOrders") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "TestOrder": TestOrder testOrder = GetTestOrder(reader); if (testOrder.OrderID == TestOrderId.ToString()) { orders.Add(testOrder); break; } break; } } else { reader.Read(); if ((reader.LocalName == "TestOrders") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable = new MailMergeDataTable("Orders", orders); reader.Dispose(); fs.Dispose(); return(dataTable); }
void Word_Clicked(object sender, EventArgs args) { var assembly = typeof(App).GetTypeInfo().Assembly; Stream inputStream = assembly.GetManifestResourceStream("XamarinIOInvoice.Template.docx"); //Load Template document stream // Stream inputStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Showcase.InvoiceGenerater.Assets.Template.docx"); //Create instance WordDocument document = new WordDocument(); //Open Template document document.Open(inputStream, FormatType.Doc); //Dispose input stream inputStream.Dispose(); //Set Clear Fields to false document.MailMerge.ClearFields = false; //Create Mail Merge Data Table MailMergeDataTable mailMergeDataTable = new MailMergeDataTable("Invoice", GetDataSource()); //Executes mail merge using the data generated in the app. document.MailMerge.ExecuteGroup(mailMergeDataTable); //Mail Merge Billing information string[] fieldNames = { "Name", "Address", "Date", "InvoiceNumber", "DueDate", "TotalDue" }; string[] fieldValues = { Customer.CustomerName, Customer.Address, Customer.Date, Customer.Number, DateTime.Now.ToString("d"), "13600" }; document.MailMerge.Execute(fieldNames, fieldValues); MemoryStream data = new MemoryStream(); document.Save(data, FormatType.Docx); document.Close(); DependencyService.Get <ISave>().SaveTextAsync("Invoice.docx", "application/msword", data); }
/// <summary> /// Update fields in the Word document /// </summary> /// <returns>Return the created Word document as stream</returns> public MemoryStream CreateWordDocument(int id, string documentType, string button) { if (id == 0) { id = 10248; } string basePath = @"wwwroot/"; string dataPath = basePath + @"/DocIO/SalesInvoiceDemo.doc"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (button == "View Template") { MemoryStream ms = new MemoryStream(); fileStream.Position = 0; fileStream.CopyTo(ms); fileStream.Close(); return(ms); } // Create a new document WordDocument document = new WordDocument(); // Load the template. document.Open(fileStream, FormatType.Automatic); fileStream.Dispose(); fileStream = null; //Create MailMergeDataTable MailMergeDataTable mailMergeDataTableOrder = GetTestOrder(id); MailMergeDataTable mailMergeDataTableOrderTotals = GetTestOrderTotals(id); MailMergeDataTable mailMergeDataTableOrderDetails = GetTestOrderDetails(id); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTableOrder); document.MailMerge.ExecuteGroup(mailMergeDataTableOrderTotals); // Using Merge events to do conditional formatting during runtime. document.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField); document.MailMerge.ExecuteGroup(mailMergeDataTableOrderDetails); FormatType formatType = FormatType.Docx; //Save as .doc format if (documentType == "WordDoc") { formatType = FormatType.Doc; } //Save as .xml format else if (documentType == "WordML") { formatType = FormatType.WordML; } //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created Word document to MemoryStream document.Save(stream, formatType); document.Close(); stream.Position = 0; return(stream); } }
public ActionResult UpdateFields(string Group1, string Button) { string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/TemplateUpdateFields.docx"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (Group1 == null) { return(View()); } string contenttype1 = "application/vnd.ms-word.document.12"; if (Button == "View Template") { return(File(fileStream, contenttype1, "TemplateUpdateFields.docx")); } //Loads the template document. string dataPathField = basePath + @"/DocIO/TemplateUpdateFields.docx"; FileStream fileStreamField = new FileStream(dataPathField, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); WordDocument document = new WordDocument(fileStreamField, FormatType.Docx); fileStreamField.Dispose(); fileStreamField = null; //Create MailMergeDataTable MailMergeDataTable mailMergeDataTableStock = GetMailMergeDataTableStock(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTableStock); //Update fields in the document. document.UpdateDocumentFields(); FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .doc format if (Group1 == "WordDoc") { type = FormatType.Doc; filename = "Sample.doc"; contenttype = "application/msword"; } //Save as .xml format else if (Group1 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
/// <summary> /// Gets the mail merge data table. /// </summary> /// <returns></returns> /// <exception cref="System.Exception">reader</exception> /// <exception cref="XmlException">Unexpected xml tag + reader.LocalName</exception> private MailMergeDataTable GetMailMergeDataTable(string basePath) { List <EmployeeDetailsImplicit> employees = new List <EmployeeDetailsImplicit>(); FileStream stream = new FileStream(basePath + @"/DocIO/Employees.xml", FileMode.OpenOrCreate); XmlReader reader = XmlReader.Create(stream); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "EmployeesList") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "EmployeesList") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "Employees": employees.Add(GetEmployee(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "EmployeesList") && reader.NodeType == XmlNodeType.EndElement) { break; } } } reader.Dispose(); stream.Dispose(); MailMergeDataTable dataTable = new MailMergeDataTable("Employees", employees); return(dataTable); }
/// <summary> /// Gets the mail merge data table. /// </summary> /// <returns></returns> /// <exception cref="System.Exception">reader</exception> /// <exception cref="XmlException">Unexpected xml tag + reader.LocalName</exception> private MailMergeDataTable GetMailMergeDataTable() { List <EmployeeDetailsImplicit> employees = new List <EmployeeDetailsImplicit>(); FileStream fs = new FileStream(ResolveApplicationDataPath("Employees.xml", "App_Data\\DocIO"), FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "EmployeesList") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "EmployeesList") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "Employees": employees.Add(GetEmployee(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "EmployeesList") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable = new MailMergeDataTable("Employees", employees); reader.Close(); fs.Dispose(); return(dataTable); }
/// <summary> /// Gets the mail merge data table. /// </summary> /// <returns></returns> /// <exception cref="System.Exception">reader</exception> /// <exception cref="XmlException">Unexpected xml tag + reader.LocalName</exception> private MailMergeDataTable GetMailMergeDataTable() { List <EmployeeDetailsImplicit> employees = new List <EmployeeDetailsImplicit>(); Stream stream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Employees.xml"); XmlReader reader = XmlReader.Create(stream); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "EmployeesList") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "EmployeesList") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "Employees": employees.Add(GetEmployee(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "EmployeesList") && reader.NodeType == XmlNodeType.EndElement) { break; } } } reader.Dispose(); stream.Dispose(); MailMergeDataTable dataTable = new MailMergeDataTable("Employees", employees); return(dataTable); }
/// <summary> /// Generates the sales invoice from an input Word template using Mail Merge. /// </summary> private void Button_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { //Gets the selected customer id. int id = Convert.ToInt32(listCustomer.SelectedItem); //Gets the input Word document. string resourcePath = "syncfusion.dociodemos.winui.Assets.DocIO.SalesInvoiceDemo.docx"; using Stream fileStream = assembly.GetManifestResourceStream(resourcePath); //Creates a new Word document instance. using WordDocument document = new(); //Opens an existing Word document. document.Open(fileStream, FormatType.Automatic); //Creates MailMergeDataTable. MailMergeDataTable mailMergeDataTableOrder = GetTestOrder(id); MailMergeDataTable mailMergeDataTableOrderTotals = GetTestOrderTotals(id); MailMergeDataTable mailMergeDataTableOrderDetails = GetTestOrderDetails(id); //Executes Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTableOrder); document.MailMerge.ExecuteGroup(mailMergeDataTableOrderTotals); document.MailMerge.ExecuteGroup(mailMergeDataTableOrderDetails); #region Document SaveOption using MemoryStream ms = new(); string filename = string.Empty; //Saves as .docx format. if (worddocx.IsChecked == true) { filename = "Sales Invoice.docx"; //Saves the Word document to the memory stream. document.Save(ms, FormatType.Docx); } //Saves as .doc format. else if (worddoc.IsChecked == true) { filename = "Sales Invoice.doc"; //Saves the Word document to the memory stream. document.Save(ms, FormatType.Doc); } //Saves as .pdf format. else if (pdf.IsChecked == true) { filename = "Sales Invoice.pdf"; //Creates a new DocIORenderer instance. using DocIORenderer renderer = new(); //Converts Word document into PDF. using PdfDocument pdfDoc = renderer.ConvertToPDF(document); //Saves the PDF document to the memory stream. pdfDoc.Save(ms); } ms.Position = 0; //Saves the memory stream as file. SaveHelper.SaveAndLaunch(filename, ms); #endregion Document SaveOption }
/// <summary> /// Export the UI data to Word document. /// </summary> /// <param name="invoiceItems">The InvoiceItems.</param> /// <param name="billInfo">The BillingInformation.</param> /// <param name="totalDue">The TotalDue.</param> public void CreateWord(IList <InvoiceItem> invoiceItems, BillingInformation billInfo, double totalDue) { //Load Template document stream Assembly assembly = typeof(ExportWord).Assembly; Stream inputStream = assembly.GetManifestResourceStream("Invoice.Assets.Template.docx"); //Create instance WordDocument document = new WordDocument(); //Open Template document document.Open(inputStream, FormatType.Word2013); //Dispose input stream inputStream.Dispose(); //Set Clear Fields to false document.MailMerge.ClearFields = false; //Create Mail Merge Data Table MailMergeDataTable mailMergeDataTable = new MailMergeDataTable("Invoice", invoiceItems); //Executes mail merge using the data generated in the app. document.MailMerge.ExecuteGroup(mailMergeDataTable); //Mail Merge Billing information string[] fieldNames = { "Name", "Address", "Date", "InvoiceNumber", "DueDate", "TotalDue" }; string[] fieldValues = { billInfo.Name, billInfo.Address, billInfo.Date.ToString("d"), billInfo.InvoiceNumber, billInfo.DueDate.ToString("d"), totalDue.ToString("#,###.00", CultureInfo.InvariantCulture) }; document.MailMerge.Execute(fieldNames, fieldValues); //Create Output file in Document library location document.Save("Invoice.docx", FormatType.Word2013); //Save as Docx format //Message box confirmation to view the created PDF document. if (MessageBox.Show("Do you want to view the Word file?", "Word Document Created", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) { //Launching the PDF file using the default Application.[Acrobat Reader] #if !NETCORE System.Diagnostics.Process.Start("Invoice.docx"); #else ProcessStartInfo psi = new ProcessStartInfo { FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, CreateNoWindow = true, Arguments = "/c start Invoice.docx" }; Process.Start(psi); #endif //this.Close(); } //Close instance document.Close(); }
/// <summary> /// Update fields in the Word document /// </summary> /// <returns>Return the resultant Word document as stream</returns> public MemoryStream UpdateFields(string documentType, string button) { string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/data/docio/template-update-fields.docx"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (button == "View Template") { MemoryStream ms = new MemoryStream(); fileStream.Position = 0; fileStream.CopyTo(ms); fileStream.Close(); return(ms); } //Loads the template document. string dataPathField = basePath + @"/data/docio/template-update-fields.docx"; FileStream fileStreamField = new FileStream(dataPathField, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); WordDocument document = new WordDocument(fileStreamField, FormatType.Docx); fileStreamField.Dispose(); fileStreamField = null; //Create MailMergeDataTable MailMergeDataTable mailMergeDataTableStock = GetMailMergeDataTableStock(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTableStock); //Update fields in the document. document.UpdateDocumentFields(); FormatType formatType = FormatType.Docx; //Save as .doc format if (documentType == "WordDoc") { formatType = FormatType.Doc; } //Save as .xml format else if (documentType == "WordML") { formatType = FormatType.WordML; } //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created Word document to MemoryStream document.Save(stream, formatType); document.Close(); stream.Position = 0; return(stream); } }
/// <summary> /// Generate an employee report using Mail merge functionality of Essential DocIO /// </summary> /// <returns>Return the created Word document as stream</returns> public MemoryStream EmployeeReport(string documentType, string button) { string basePath = _hostingEnvironment.WebRootPath; string dataPathEmployee = basePath + @"/data/docio/employees-report-demo.doc"; //Load Template document stream. FileStream fileStream = new FileStream(dataPathEmployee, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (button == "View Template") { MemoryStream ms = new MemoryStream(); fileStream.Position = 0; fileStream.CopyTo(ms); fileStream.Dispose(); return(ms); } fileStream = null; // Creating a new document. WordDocument document = new WordDocument(); // Load template fileStream = new FileStream(dataPathEmployee, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); document.Open(fileStream, FormatType.Doc); fileStream.Dispose(); fileStream = null; document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_EmployeeImage); //Create MailMergeDataTable MailMergeDataTable mailMergeDataTable = GetMailMergeDataTableEmployee(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTable); FormatType formatType = FormatType.Docx; //Save as .doc format if (documentType == "WordDoc") { formatType = FormatType.Doc; } //Save as .xml format else if (documentType == "WordML") { formatType = FormatType.WordML; } //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created Word document to MemoryStream document.Save(stream, formatType); document.Close(); stream.Position = 0; return(stream); } }
/// <summary> /// Export the UI data to Word document. /// </summary> /// <param name="invoiceItems">The InvoiceItems.</param> /// <param name="billInfo">The BillingInformation.</param> /// <param name="totalDue">The TotalDue.</param> public void CreateWord(IList <InvoiceItem> invoiceItems, BillingInformation billInfo, double totalDue) { //Load Template document stream #if NETCORE Stream inputStream = new FileStream("../../../Assets/Template.docx", FileMode.Open, FileAccess.Read); #else Stream inputStream = new FileStream("../../Assets/Template.docx", FileMode.Open, FileAccess.Read); #endif //Create instance WordDocument document = new WordDocument(); //Open Template document document.Open(inputStream, FormatType.Word2013); //Dispose input stream inputStream.Dispose(); //Set Clear Fields to false document.MailMerge.ClearFields = false; //Create Mail Merge Data Table MailMergeDataTable mailMergeDataTable = new MailMergeDataTable("Invoice", invoiceItems); //Executes mail merge using the data generated in the app. document.MailMerge.ExecuteGroup(mailMergeDataTable); //Mail Merge Billing information string[] fieldNames = { "Name", "Address", "Date", "InvoiceNumber", "DueDate", "TotalDue" }; string[] fieldValues = { billInfo.Name, billInfo.Address, billInfo.Date.ToString("d"), billInfo.InvoiceNumber, billInfo.DueDate.ToString("d"), totalDue.ToString("#,###.00", CultureInfo.InvariantCulture) }; document.MailMerge.Execute(fieldNames, fieldValues); //Create Output file in Document library location document.Save("Invoice.docx", FormatType.Word2013); //Save as Docx format //Message box confirmation to view the created PDF document. if (MessageBox.Show("Do you want to view the Word file?", "Word Document Created", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) { //Launching the PDF file using the default Application.[Acrobat Reader] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Invoice.docx") { UseShellExecute = true }; process.Start(); #else Process.Start("Invoice.docx"); #endif //this.Close(); } //Close instance document.Close(); }
/// <summary> /// Generates an employee report using Mail merge. /// </summary> private void Button_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { //Gets the input Word document. string dataPathEmployee = @"syncfusion.dociodemos.winui.Assets.DocIO.EmployeesReportDemo.docx"; using Stream fileStream = assembly.GetManifestResourceStream(dataPathEmployee); //Creates a new Word document instance. using WordDocument document = new(); //Opens an existing Word document. document.Open(fileStream, FormatType.Doc); document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_EmployeeImage); //Creates MailMergeDataTable. MailMergeDataTable mailMergeDataTable = GetMailMergeDataTableEmployee(); //Executes Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTable); #region Document SaveOption using MemoryStream ms = new(); string filename = string.Empty; //Saves as .docx format. if (worddocx.IsChecked == true) { filename = "Employee Report.docx"; //Saves the Word document to the memory stream. document.Save(ms, FormatType.Docx); } //Saves as .doc format. else if (worddoc.IsChecked == true) { filename = "Employee Report.doc"; //Saves the Word document to the memory stream. document.Save(ms, FormatType.Doc); } //Saves as .pdf format. else if (pdf.IsChecked == true) { filename = "Employee Report.pdf"; //Creates a new DocIORenderer instance. using DocIORenderer renderer = new(); //Converts Word document into PDF. using PdfDocument pdfDoc = renderer.ConvertToPDF(document); //Saves the PDF document to the memory stream. pdfDoc.Save(ms); } ms.Position = 0; //Saves the memory stream as file. SaveHelper.SaveAndLaunch(filename, ms); #endregion Document SaveOption }
public ActionResult TableStyles(string Group1) { if (Group1 == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; string dataPath = string.Empty; dataPath = basePath + @"/DocIO/TemplateTableStyle.doc"; WordDocument document = new WordDocument(); FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); document.Open(fileStream, FormatType.Doc); fileStream.Dispose(); fileStream = null; //Create MailMergeDataTable MailMergeDataTable mailMergeDataTable = GetMailMergeDataTable(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTable); #region Built-in table styles //Get table to apply style. WTable table = (WTable)document.LastSection.Tables[0]; //Apply built-in table style to the table. table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent5); #endregion FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .xml format if (Group1 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
/// <summary> /// Gets the mail merge data table. /// </summary> private MailMergeDataTable GetMailMergeDataTablePriceList() { List <Product_PriceList> product_PriceList = new List <Product_PriceList>(); FileStream fs = new FileStream(@"wwwroot/data/docio/product-price-list.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "Products") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "Products") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "Product_PriceList": product_PriceList.Add(GetProduct_PriceList(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "Products") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable1 = new MailMergeDataTable("Product_PriceList", product_PriceList); reader.Dispose(); fs.Dispose(); return(dataTable1); }
/// <summary> /// Gets the mail merge data table. /// </summary> private MailMergeDataTable GetMailMergeDataTableEmployee() { List <Employees> employees = new List <Employees>(); FileStream fs = new FileStream(_hostingEnvironment.WebRootPath + @"/data/docio/employees-list.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "Employees") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "Employees") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "Employee": employees.Add(GetEmployees(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "Employees") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable = new MailMergeDataTable("Employees", employees); reader.Dispose(); fs.Dispose(); return(dataTable); }
/// <summary> /// Gets the mail merge data table. /// </summary> private MailMergeDataTable GetMailMergeDataTableStock() { List <StockDetails> stockDetails = new List <StockDetails>(); FileStream fs = new FileStream(_hostingEnvironment.WebRootPath + @"/DocIO/StockDetails.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "StockMarket") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "StockMarket") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "StockDetails": stockDetails.Add(GetStockDetails(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "StockMarket") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable = new MailMergeDataTable("StockDetails", stockDetails); reader.Dispose(); fs.Dispose(); return(dataTable); }
/// <summary> /// Gets the mail merge data table. /// </summary> private MailMergeDataTable GetMailMergeDataTableProductData() { List <ProductDetail> productDetail = new List <ProductDetail>(); FileStream fs = new FileStream(_hostingEnvironment.WebRootPath + @"/DocIO/Product.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "ProductList") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "ProductList") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "Products": productDetail.Add(GetProductDetail(reader)); break; } } else { reader.Read(); if ((reader.LocalName == "ProductList") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable2 = new MailMergeDataTable("ProductDetail", productDetail); reader.Dispose(); fs.Dispose(); return(dataTable2); }
/// <summary> /// Replace mail merge with list of object /// </summary> /// <param name="processedData">Dictionary with list object</param> protected void ProcessDocumentGroup() { if (DataTable.Count() == 0) { return; } List <DictionaryEntry> commands = new List <DictionaryEntry>(); foreach (var data in DataTable) { DocumentTemplate.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_ImageHandler); Options = data.Options; MailMergeDataTable dataTable = new MailMergeDataTable(data.Key, data.Data); Options = data.Options; DocumentTemplate.MailMerge.ExecuteGroup(dataTable); } }
public ActionResult MacroPreservation(string Button) { string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/MacroTemplate.dotm"; string contenttype1 = "application/msword"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (Button == null) { return(View()); } if (Button == "View Template") { return(File(fileStream, contenttype1, "MacroTemplate.dotm")); } try { string dataPathMacro = basePath + @"/DocIO/MacroTemplate.dotm"; fileStream = new FileStream(dataPathMacro, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Load the template. WordDocument document = new WordDocument(fileStream, FormatType.Dotm); fileStream.Dispose(); fileStream = null; //Create MailMergeDataTable MailMergeDataTable mailMergeDataTableProductListData = GetMailMergeDataTableProductListData(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTableProductListData); #region Document SaveOption //Save as .docm format FormatType type = FormatType.Word2013Docm; string filename = "Sample.docm"; string contenttype = "application/msword"; #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); } catch (Exception) { } return(View()); }
/// <summary> /// Gets the relational data to perform mail merge /// </summary> /// <returns></returns> static MailMergeDataTable GetRelationalData() { List <ExpandoObject> customers = new List <ExpandoObject>(); //Gets data from XML Stream xmlStream = File.OpenRead(Path.GetFullPath(@"../../../CustomerDetails.xml")); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(xmlStream); xmlStream.Dispose(); ExpandoObject customer = new ExpandoObject(); GetDataAsExpandoObject((xmlDocument as XmlNode).LastChild, ref customer); customers = (((customer as IDictionary <string, object>)["CustomerDetails"] as List <ExpandoObject>)[0] as IDictionary <string, object>)["Customers"] as List <ExpandoObject>; //Creates an instance of "MailMergeDataTable" by specifying mail merge group name and "IEnumerable" collection MailMergeDataTable dataTable = new MailMergeDataTable("Customers", customers); return(dataTable); }
public Class194(MailMergeDataTable A_0) { this.int_0 = -1; this.string_1 = A_0.GroupName; A_0.SourceData.Reset(); A_0.SourceData.MoveNext(); this.ienumerator_0 = A_0.SourceData; try { this.type_0 = this.ienumerator_0.Current.GetType(); this.method_2(this.ienumerator_0); } catch { this.type_0 = null; this.string_0 = null; } this.method_3(); }
public static WordDocument Generate(TemplateData templateData) { using (var fileStreamPath = new FileStream($"{templateData.Template}.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var document = new WordDocument(fileStreamPath, FormatType.Docx); IEnumerable <object> data = templateData.Data.GetType().GetInterface(typeof(IEnumerable <>).FullName) != null ? templateData.Data as IEnumerable <object> : new[] { templateData.Data }; MailMergeDataTable dataTable = new MailMergeDataTable(templateData.Template, data); document.MailMerge.ExecuteNestedGroup(dataTable); foreach (var subTemplate in templateData.SubTemplates) { var subDocument = Generate(subTemplate); document.Replace($"<template:{subTemplate.Template}>", subDocument, false, true, false); } document.Replace(new Regex(@"<template:.+>"), string.Empty); return(document); } }
public ActionResult MailMergeEvent(string Group1, string Button) { string basePath = _hostingEnvironment.WebRootPath; string contenttype1 = "application/msword"; string dataPath = basePath + @"/DocIO/MailMergeEventTemplate.doc"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (Group1 == null) { return(View()); } if (Button == "View Template") { return(File(fileStream, contenttype1, "MailMergeEventTemplate.doc")); } fileStream.Dispose(); fileStream = null; try { // Load the template. FileStream fileStreamPath = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); WordDocument document = new WordDocument(fileStreamPath, FormatType.Doc); fileStreamPath.Dispose(); fileStreamPath = null; // Using Merge events to do conditional formatting during runtime. document.MailMerge.MergeField += new MergeFieldEventHandler(AlternateRows_MergeField); document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_ProductImage); //Create MailMergeDataTable MailMergeDataTable mailMergeDataTablePriceList = GetMailMergeDataTablePriceList(); MailMergeDataTable mailMergeDataTableProductData = GetMailMergeDataTableProductData(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTablePriceList); document.MailMerge.ExecuteGroup(mailMergeDataTableProductData); FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .doc format if (Group1 == "WordDoc") { type = FormatType.Doc; filename = "Sample.doc"; contenttype = "application/msword"; } //Save as .xml format else if (Group1 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); } catch (Exception) { } return(View()); }
public ActionResult PieChart(string Group1) { if (Group1 == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; string datapath = basePath + @"/DocIO/PieChart.docx"; //A new document is created. FileStream fileStream = new FileStream(datapath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); WordDocument document = new WordDocument(fileStream, FormatType.Docx); //Create MailMergeDataTable MailMergeDataTable mailMergeDataTable = GetMailMergeDataTableProductDetails(); //Merge the product table in the Word document document.MailMerge.ExecuteGroup(mailMergeDataTable); //Find the Placeholder of Pie chart to insert TextSelection selection = document.Find("<Pie Chart>", false, false); WParagraph paragraph = selection.GetAsOneRange().OwnerParagraph; paragraph.ChildEntities.Clear(); //Create and Append chart to the paragraph WChart pieChart = paragraph.AppendChart(446, 270); //Set chart data pieChart.ChartType = OfficeChartType.Pie; pieChart.ChartTitle = "Best Selling Products"; pieChart.ChartTitleArea.FontName = "Calibri (Body)"; pieChart.ChartTitleArea.Size = 14; GetChartData(pieChart, 0); //Create a new chart series with the name “Sales” IOfficeChartSerie pieSeries = pieChart.Series.Add("Sales"); pieSeries.Values = pieChart.ChartData[2, 2, 11, 2]; //Setting data label pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsPercentage = true; pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside; //Setting background color pieChart.ChartArea.Fill.ForeColor = Syncfusion.Drawing.Color.FromArgb(242, 242, 242); pieChart.PlotArea.Fill.ForeColor = Syncfusion.Drawing.Color.FromArgb(242, 242, 242); pieChart.ChartArea.Border.LinePattern = OfficeChartLinePattern.None; pieChart.PrimaryCategoryAxis.CategoryLabels = pieChart.ChartData[2, 1, 11, 1]; string filename = ""; string contenttype = ""; MemoryStream ms = new MemoryStream(); #region Document SaveOption if (Group1 == "WordDocx") { filename = "Sample.docx"; contenttype = "application/msword"; document.Save(ms, FormatType.Docx); } else if (Group1 == "WordML") { filename = "Sample.xml"; contenttype = "application/msword"; document.Save(ms, FormatType.WordML); } else { filename = "Sample.pdf"; contenttype = "application/pdf"; DocIORenderer renderer = new DocIORenderer(); renderer.ConvertToPDF(document).Save(ms); } #endregion Document SaveOption document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
/// <summary> /// Creates word document with built - in table styles /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, System.EventArgs e) { try { // Open the template document. WordDocument document = new WordDocument(fileName + "TemplateTableStyle.doc"); //Create MailMergeDataTable MailMergeDataTable mailMergeDataTable = GetMailMergeDataTable(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTable); #region Built-in table styles //Get table to apply style. WTable table = (WTable)document.LastSection.Tables[0]; //Apply built-in table style to the table. table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent5); #endregion # region Save Document //Save as docx format if (wordDocxRadioBtn.Checked) { //Saving the document as .docx document.Save("Sample.docx", FormatType.Docx); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.docx") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.docx"); #endif //Exit this.Close(); } catch (Win32Exception ex) { MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } } //Save as pdf format else if (pdfRadioBtn.Checked) { DocToPDFConverter converter = new DocToPDFConverter(); //Convert word document into PDF document PdfDocument pdfDoc = converter.ConvertToPDF(document); //Save the pdf file pdfDoc.Save("Sample.pdf"); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated PDF?", " Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.pdf"); #endif //Exit this.Close(); } catch (Exception ex) { MessageBoxAdv.Show("PDF Viewer is not installed in this system"); Console.WriteLine(ex.ToString()); } } } else { // Exit this.Close(); } # endregion }
private async void Button_Click_1(object sender, RoutedEventArgs e) { WordDocument document = new WordDocument(); #if STORE_SB Stream inputStream = inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Template_Report.doc"); //Open Template document await document.OpenAsync(inputStream, FormatType.Word2013); inputStream.Dispose(); #else if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) { Stream inputStream = inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Template_Letter_WP.doc"); await document.OpenAsync(inputStream, FormatType.Doc); inputStream.Dispose(); } else { Stream inputStream; if (rdReport.IsChecked == true) { inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Template_Report.doc"); } else { inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Template_Letter.doc"); } //Open Template document await document.OpenAsync(inputStream, FormatType.Word2013); inputStream.Dispose(); } #endif #region Execute Mail merge if (rdExplicit.IsChecked.Value) { MailMergeDataSet dataSet = GetMailMergeDataSet(); List <DictionaryEntry> commands = new List <DictionaryEntry>(); //DictionaryEntry contain "Source table" (KEY) and "Command" (VALUE) DictionaryEntry entry = new DictionaryEntry("Employees", string.Empty); commands.Add(entry); // To retrive customer details entry = new DictionaryEntry("Customers", "EmployeeID = %Employees.EmployeeID%"); commands.Add(entry); // To retrieve order details entry = new DictionaryEntry("Orders", "CustomerID = %Customers.CustomerID%"); commands.Add(entry); //Executes nested Mail merge using explicit relational data. document.MailMerge.ExecuteNestedGroup(dataSet, commands); } else { MailMergeDataTable dataTable = GetMailMergeDataTable(); //Executes nested Mail merge using implicit relational data. document.MailMerge.ExecuteNestedGroup(dataTable); } #endregion Save(rdDoc.IsChecked == true, document); }
void Word_Clicked(object sender, EventArgs args) { var assembly = typeof(App).GetTypeInfo().Assembly; Stream inputStream = assembly.GetManifestResourceStream("XamarinIOInvoice.Template.docx"); //Load Template document stream // Stream inputStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Showcase.InvoiceGenerater.Assets.Template.docx"); //Create instance WordDocument document = new WordDocument(); //Open Template document document.Open(inputStream, FormatType.Doc); //Dispose input stream inputStream.Dispose(); //Set Clear Fields to false document.MailMerge.ClearFields = false; //Create Mail Merge Data Table MailMergeDataTable mailMergeDataTable = new MailMergeDataTable("Invoice", GetDataSource()); //Executes mail merge using the data generated in the app. document.MailMerge.ExecuteGroup(mailMergeDataTable); //Mail Merge Billing information string[] fieldNames = { "Name", "Address", "Date", "InvoiceNumber", "DueDate", "TotalDue" }; string[] fieldValues = { Customer.CustomerName, Customer.Address, Customer.Date, Customer.Number, DateTime.Now.ToString("d"), "13600" }; document.MailMerge.Execute(fieldNames, fieldValues); MemoryStream data = new MemoryStream(); document.Save(data, FormatType.Docx); document.Close(); DependencyService.Get<ISave>().SaveTextAsync("Invoice.docx", "application/msword", data); }
public ActionResult NestedMailMerge(string Group1, string Group2, string Group3, string Button) { if (Group1 == null || Group2 == null || Group3 == null) { return(View()); } if (Button == "View Template") { if (Group2 == "Report") { return(new TemplateResult("Template_Report.doc", ResolveApplicationDataPath("App_Data\\DocIO"), HttpContext.ApplicationInstance.Response)); } else { return(new TemplateResult("Template_Letter.doc", ResolveApplicationDataPath("App_Data\\DocIO"), HttpContext.ApplicationInstance.Response)); } } string dataPath = string.Empty; if (Group2 == "Report") { dataPath = ResolveApplicationDataPath("Template_Report.doc", "App_Data\\DocIO"); } else { dataPath = ResolveApplicationDataPath("Template_Letter.doc", "App_Data\\DocIO"); } // Creating a new document. WordDocument document = new WordDocument(); // Load template document.Open(dataPath, FormatType.Doc); if (Group3 == "Explicit") { DataSet ds = new DataSet(); ds.ReadXml(ResolveApplicationDataPath("Employees.xml", "App_Data\\DocIO")); ArrayList commands = new ArrayList(); //DictionaryEntry contain "Source table" (KEY) and "Command" (VALUE) DictionaryEntry entry = new DictionaryEntry("Employees", string.Empty); commands.Add(entry); // To retrive customer details DataTable table = ds.Tables["Customers"]; string relation = table.ParentRelations[0].ChildColumns[0].ColumnName + " = %Employees." + table.ParentRelations[0].ParentColumns[0].ColumnName + "%"; entry = new DictionaryEntry("Customers", relation); commands.Add(entry); // To retrieve order details table = ds.Tables["Orders"]; relation = table.ParentRelations[0].ChildColumns[0].ColumnName + " = %Customers." + table.ParentRelations[0].ParentColumns[0].ColumnName + "%"; entry = new DictionaryEntry("Orders", relation); commands.Add(entry); //Executes nested Mail merge using explicit relational data. document.MailMerge.ExecuteNestedGroup(ds, commands); } else { MailMergeDataTable dataTable = GetMailMergeDataTable(); //Executes nested Mail merge using implicit relational data. document.MailMerge.ExecuteNestedGroup(dataTable); } try { #region Document SaveOption //Save as .doc format if (Group1 == "WordDoc") { return(document.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .docx format else if (Group1 == "WordDocx") { return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } // Save as WordML(.xml) format else if (Group1 == "WordML") { return(document.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .pdf format else if (Group1 == "Pdf") { DocToPDFConverter converter = new DocToPDFConverter(); PdfDocument pdfDoc = converter.ConvertToPDF(document); return(pdfDoc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } #endregion Document SaveOption } catch (Exception) { } return(View()); }
void WordButton_Clicked(object sender, EventArgs e) { var assembly = typeof(App).GetTypeInfo().Assembly; Stream inputStream = null; //Load Template document stream if (isIOS) inputStream = assembly.GetManifestResourceStream("XamarinIOInvoice.Template_iOS.docx"); else inputStream = assembly.GetManifestResourceStream("XamarinIOInvoice.Template.docx"); //Create instance WordDocument document = new WordDocument(); //Open Template document document.Open(inputStream, FormatType.Doc); //Dispose input stream inputStream.Dispose(); //Set Clear Fields to false document.MailMerge.ClearFields = false; //Create Mail Merge Data Table MailMergeDataTable mailMergeDataTable = new MailMergeDataTable("Invoice", this.ListSource); //Executes mail merge using the data generated in the app. document.MailMerge.ExecuteGroup(mailMergeDataTable); //Mail Merge Billing information string[] fieldNames = { "Name", "Address", "Date", "InvoiceNumber", "DueDate", "TotalDue" }; string[] fieldValues = { billInfo.Name, billInfo.Address, billInfo.Date, billInfo.InvoiceNumber, DateTime.Now.ToString("d"), GetNetAmount().ToString() }; document.MailMerge.Execute(fieldNames, fieldValues); MemoryStream data = new MemoryStream(); document.Save(data, FormatType.Docx); document.Close(); DependencyService.Get<ISave>().SaveTextAsync("Invoice.docx", "application/msword", data); }