static void Main(string[] args) { //Creates new Word document instance for Word processing using (WordDocument document = new WordDocument()) { //Opens the input Word document Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx")); document.Open(docStream, FormatType.Docx); docStream.Dispose(); //Finds all the placeholder text enclosed within '«' and '»' in the Word document TextSelection[] textSelections = document.FindAll(new Regex("«([(?i)image(?-i)]*:*[a-zA-Z0-9 ]*:*[a-zA-Z0-9 ]+)»")); string[] searchedPlaceholders = new string[textSelections.Length]; for (int i = 0; i < textSelections.Length; i++) { searchedPlaceholders[i] = textSelections[i].SelectedText; } for (int i = 0; i < searchedPlaceholders.Length; i++) { //Replaces the placeholder text enclosed within '«' and '»' with desired merge field WParagraph paragraph = new WParagraph(document); paragraph.AppendField(searchedPlaceholders[i].TrimStart('«').TrimEnd('»'), FieldType.FieldMergeField); TextSelection newSelection = new TextSelection(paragraph, 0, paragraph.Items.Count); TextBodyPart bodyPart = new TextBodyPart(document); bodyPart.BodyItems.Add(paragraph); document.Replace(searchedPlaceholders[i], bodyPart, true, true, true); } //Saves the resultant file in the given path docStream = File.Create(Path.GetFullPath(@"Result.docx")); document.Save(docStream, FormatType.Docx); docStream.Dispose(); } }
private void InsertFirstPageHeaderFooter(WordDocument doc, IWSection section) { Assembly execAssm = typeof(HeadersAndFootersDemo).GetTypeInfo().Assembly; Stream inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Northwind_logo.png"); // Add a new paragraph for header to the document. IWParagraph headerPar = new WParagraph(doc); // Add a new table to the header. IWTable table = section.HeadersFooters.FirstPageHeader.AddTable(); RowFormat format = new RowFormat(); // Setting cleared table border style. format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Cleared; // Inserting table with a row and two columns. table.ResetCells(1, 2, format, 265); // Inserting logo image to the table first cell. headerPar = table[0, 0].AddParagraph() as WParagraph; headerPar.AppendPicture(inputStream); //Set Image size (headerPar.Items[0] as WPicture).Width = 232.5f; (headerPar.Items[0] as WPicture).Height = 54.75f; // Inserting text to the table second cell. headerPar = table[0, 1].AddParagraph() as WParagraph; IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638."); txt.CharacterFormat.FontSize = 12; txt.CharacterFormat.CharacterSpacing = 1.7f; headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right; // Add a new paragraph to the header with address text. headerPar = new WParagraph(doc); headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; txt = headerPar.AppendText("\nFirst Page Header"); txt.CharacterFormat.CharacterSpacing = 1.7f; section.HeadersFooters.FirstPageHeader.Paragraphs.Add(headerPar); // Add a footer paragraph text to the document. WParagraph footerPar = new WParagraph(doc); footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader); // Add text. footerPar.AppendText("Copyright Northwind Inc. 2001 - 2015"); // Add page and Number of pages field to the document. footerPar.AppendText("\tFirst Page "); footerPar.AppendField("Page", FieldType.FieldPage); section.HeadersFooters.FirstPageFooter.Paragraphs.Add(footerPar); #region Page Number Settings section.PageSetup.RestartPageNumbering = true; section.PageSetup.PageStartingNumber = Convert.ToInt32(this.numericUpDown1.Value); section.PageSetup.PageNumberStyle = (PageNumberStyle)Enum.Parse(typeof(PageNumberStyle), this.comboBox1.SelectedItem.ToString(), true); #endregion }
private void InsertFirstPageHeaderFooter(WordDocument doc, IWSection section) { // Add a new paragraph for header to the document. IWParagraph headerPar = new WParagraph(doc); // Add a new table to the header. IWTable table = section.HeadersFooters.FirstPageHeader.AddTable(); RowFormat format = new RowFormat(); // Setting cleared table border style. format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Cleared; // Inserting table with a row and two columns. table.ResetCells(1, 2, format, 265); // Inserting logo image to the table first cell. headerPar = table[0, 0].AddParagraph() as WParagraph; string basePath = _hostingEnvironment.WebRootPath; FileStream imageStream = new FileStream(basePath + @"/images/DocIO/Northwind_logo.png", FileMode.Open, FileAccess.Read); headerPar.AppendPicture(imageStream); //Set Image size (headerPar.Items[0] as WPicture).Width = 232.5f; (headerPar.Items[0] as WPicture).Height = 54.75f; // Inserting text to the table second cell. headerPar = table[0, 1].AddParagraph() as WParagraph; IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638."); txt.CharacterFormat.FontSize = 12; txt.CharacterFormat.CharacterSpacing = 1.7f; headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right; // Add a new paragraph to the header with address text. headerPar = new WParagraph(doc); headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; txt = headerPar.AppendText("\nFirst Page Header"); txt.CharacterFormat.CharacterSpacing = 1.7f; section.HeadersFooters.FirstPageHeader.Paragraphs.Add(headerPar); // Add a footer paragraph text to the document. WParagraph footerPar = new WParagraph(doc); footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader); // Add text. footerPar.AppendText("Copyright Northwind Inc. 2001 - 2017"); // Add page and Number of pages field to the document. footerPar.AppendText("\tFirst Page "); footerPar.AppendField("Page", FieldType.FieldPage); section.HeadersFooters.FirstPageFooter.Paragraphs.Add(footerPar); #region Page Number Settings section.PageSetup.RestartPageNumbering = true; section.PageSetup.PageStartingNumber = 1; section.PageSetup.PageNumberStyle = PageNumberStyle.Arabic; #endregion Page Number Settings }
private void InsertPageHeaderFooter(WordDocument doc, IWSection section1) { // Add a new paragraph for header to the document. IWParagraph headerPar = new WParagraph(doc); // Add a new table to the header IWTable table = section1.HeadersFooters.Header.AddTable(); RowFormat format = new RowFormat(); // Setting Single table border style. format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single; // Inserting table with a row and two columns. table.ResetCells(1, 2, format, 265); // Inserting logo image to the table first cell. headerPar = table[0, 0].AddParagraph() as WParagraph; #if NETCORE headerPar.AppendPicture(Image.FromFile(@"..\..\..\..\..\..\..\..\Common\images\DocIO\Northwind_logo.png")); #else headerPar.AppendPicture(Image.FromFile(@"..\..\..\..\..\..\..\Common\images\DocIO\Northwind_logo.png")); #endif //Set Image size. (headerPar.Items[0] as WPicture).Width = 232.5f; (headerPar.Items[0] as WPicture).Height = 54.75f; // Inserting text to the table second cell. headerPar = table[0, 1].AddParagraph() as WParagraph; IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638."); txt.CharacterFormat.FontSize = 12; txt.CharacterFormat.CharacterSpacing = 1.7f; headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right; // Add a footer paragraph text to the document. WParagraph footerPar = new WParagraph(doc); footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader); // Add text. footerPar.AppendText("Copyright Northwind Inc. 2001 - 2017"); // Add page and Number of pages field to the document. footerPar.AppendText("\tPage "); IWField ff = footerPar.AppendField("Page", Syncfusion.DocIO.FieldType.FieldPage); section1.HeadersFooters.Footer.Paragraphs.Add(footerPar); #region Page Number Settings section1.PageSetup.RestartPageNumbering = true; section1.PageSetup.PageStartingNumber = Convert.ToInt32(this.numericUpDown1.Value); section1.PageSetup.PageNumberStyle = (PageNumberStyle)Enum.Parse(typeof(PageNumberStyle), this.comboBox1.SelectedItem.ToString(), true); #endregion }
private void InsertPageHeaderFooter(WordDocument doc, IWSection section1) { // Add a new paragraph for header to the document. IWParagraph headerPar = new WParagraph(doc); // Add a new table to the header IWTable table = section1.HeadersFooters.Header.AddTable(); RowFormat format = new RowFormat(); // Setting Single table border style. format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single; // Inserting table with a row and two columns. table.ResetCells(1, 2, format, 265); // Inserting logo image to the table first cell. headerPar = table[0, 0].AddParagraph() as WParagraph; string s = ResolveApplicationDataPath("Northwind_logo.png", "Content\\DocIO"); headerPar.AppendPicture(System.Drawing.Image.FromFile(s)); //Set Image size. (headerPar.Items[0] as WPicture).Width = 232.5f; (headerPar.Items[0] as WPicture).Height = 54.75f; // Inserting text to the table second cell. headerPar = table[0, 1].AddParagraph() as WParagraph; IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638."); txt.CharacterFormat.FontSize = 12; txt.CharacterFormat.CharacterSpacing = 1.7f; headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right; // Add a footer paragraph text to the document. WParagraph footerPar = new WParagraph(doc); footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader); // Add text. footerPar.AppendText("Copyright Northwind Inc. 2001 - 2017"); // Add page and Number of pages field to the document. footerPar.AppendText("\tPage "); IWField ff = footerPar.AppendField("Page", FieldType.FieldPage); section1.HeadersFooters.Footer.Paragraphs.Add(footerPar); #region Page Number Settings section1.PageSetup.RestartPageNumbering = true; section1.PageSetup.PageStartingNumber = 1; section1.PageSetup.PageNumberStyle = PageNumberStyle.Arabic; #endregion Page Number Settings }
private void button1_Click_1(object sender, System.EventArgs e) { // Get Template document and database path. string dataPath = Application.StartupPath + @"..\..\..\..\..\..\..\..\Common\Data\"; try { //SDF the database and get the NorthWind AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true); DataTable table = new DataTable(); SqlCeConnection conn = new SqlCeConnection(); if (conn.ServerVersion.StartsWith("3.5")) { conn.ConnectionString = "Data Source = " + dataPath + "NorthwindIO_3.5.sdf"; } else { conn.ConnectionString = "Data Source = " + dataPath + "NorthwindIO.sdf"; } conn.Open(); SqlCeDataAdapter adapter = new SqlCeDataAdapter("Select CustomerID,CompanyName,ContactName,Address,Country,Phone from Customers", conn); adapter.Fill(table); adapter.Dispose(); conn.Close(); // Creating a new document. WordDocument document = new WordDocument(); // Adding a new section to the document. IWSection section = document.AddSection(); IWParagraph paragraph = section.AddParagraph(); paragraph.ParagraphFormat.BeforeSpacing = 20f; //Format the heading. IWTextRange text = paragraph.AppendText("Northwind Report"); text.CharacterFormat.Bold = true; text.CharacterFormat.FontName = "Cambria"; text.CharacterFormat.FontSize = 14.0f; text.CharacterFormat.TextColor = Color.DarkBlue; paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; paragraph = section.AddParagraph(); paragraph.ParagraphFormat.BeforeSpacing = 18f; //Create a new table WTextBody textBody = section.Body; IWTable docTable = textBody.AddTable(); //Set the format for rows RowFormat format = new RowFormat(); format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single; format.Borders.LineWidth = 1.0F; format.Borders.Color = Color.Black; //Initialize number of rows and cloumns. docTable.ResetCells(table.Rows.Count + 1, table.Columns.Count, format, 84); //Repeat the header. docTable.Rows[0].IsHeader = true; string colName; //Format the header rows for (int c = 0; c <= table.Columns.Count - 1; c++) { string[] Cols = table.Columns[c].ColumnName.Split('|'); colName = Cols[Cols.Length - 1]; IWTextRange theadertext = docTable.Rows[0].Cells[c].AddParagraph().AppendText(colName); theadertext.CharacterFormat.FontSize = 12f; theadertext.CharacterFormat.Bold = true; theadertext.CharacterFormat.TextColor = Color.White; docTable.Rows[0].Cells[c].CellFormat.BackColor = Color.FromArgb(33, 67, 126); docTable.Rows[0].Cells[c].CellFormat.Borders.Color = Color.Black; docTable.Rows[0].Cells[c].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single; docTable.Rows[0].Cells[c].CellFormat.Borders.LineWidth = 1.0f; docTable.Rows[0].Cells[c].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle; } //Format the table body rows for (int r = 0; r <= table.Rows.Count - 1; r++) { for (int c = 0; c <= table.Columns.Count - 1; c++) { string Value = table.Rows[r][c].ToString(); IWTextRange theadertext = docTable.Rows[r + 1].Cells[c].AddParagraph().AppendText(Value); theadertext.CharacterFormat.FontSize = 10; docTable.Rows[r + 1].Cells[c].CellFormat.BackColor = ((r & 1) == 0) ? Color.FromArgb(237, 240, 246) : Color.FromArgb(192, 201, 219); docTable.Rows[r + 1].Cells[c].CellFormat.Borders.Color = Color.Black; docTable.Rows[r + 1].Cells[c].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single; docTable.Rows[r + 1].Cells[c].CellFormat.Borders.LineWidth = 0.5f; docTable.Rows[r + 1].Cells[c].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle; } } // Add a footer paragraph text to the document. WParagraph footerPar = new WParagraph(document); // Add text. footerPar.AppendText("Copyright Syncfusion Inc. 2001 - 2017"); // Add page and Number of pages field to the document. footerPar.AppendText(" Page "); footerPar.AppendField("Page", Syncfusion.DocIO.FieldType.FieldPage); section.HeadersFooters.Footer.Paragraphs.Add(footerPar); //Save as doc format if (wordDocRadioBtn.Checked) { //Saving the document to disk. document.Save("Sample.doc"); //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) { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] System.Diagnostics.Process.Start("Sample.doc"); //Exit this.Close(); } } //Save as docx format else 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] System.Diagnostics.Process.Start("Sample.docx"); //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 { System.Diagnostics.Process.Start("Sample.pdf"); //Exit this.Close(); } catch (Exception ex) { MessageBoxAdv.Show("PDF Viewer is not installed in this system"); Console.WriteLine(ex.ToString()); } } } else { // Exit this.Close(); } } catch (Exception Ex) { // Shows the Message box with Exception message, if an exception throws. MessageBoxAdv.Show(Ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } }