/// <summary> /// Inserting a Picture content control. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-picture-net-csharp-vb.php /// </remarks> static void InsertPicture() { // Let's create a simple document. DocumentCore dc = new DocumentCore(); Picture pict, pict1, pict2; // See the remarks on https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-5.0 using (WebClient client = new WebClient()) { byte[] imageBytes = client.DownloadData("https://www.sautinsoft.com/images/banner_sautinsoft.jpg"); using (MemoryStream ms = new MemoryStream(imageBytes)) { pict = new Picture(dc, new InlineLayout(new Size(400, 100)), ms); } } using (HttpClient client = new HttpClient()) { Stream firstImageStream = client.GetStreamAsync("https://sautinsoft.com/images/developer.png").Result; var ms = new MemoryStream(); firstImageStream.CopyTo(ms); pict1 = new Picture(dc, new InlineLayout(new Size(50, 50)), ms); } Section section = new Section(dc); dc.Sections.Add(section); section.Blocks.Add(new Paragraph(dc, "Picture below is inside the block-level content control:")); // Create a picture content control. BlockContentControl control = new BlockContentControl(dc, ContentControlType.Picture, new Paragraph(dc, pict)); section.Blocks.Add(control); Paragraph par = new Paragraph(dc, new Run(dc, "Following picture is inside the inline-level content control: "), new InlineContentControl(dc, ContentControlType.Picture, pict1)); section.Blocks.Add(par); section.Blocks.Add(new Paragraph(dc, "Insert a picture content control from the local disk:")); string pictPath = @"..\..\picture.jpg"; pict2 = new Picture(dc, new InlineLayout(new Size(100, 100)), pictPath); BlockContentControl localpict = new BlockContentControl(dc, ContentControlType.Picture, new Paragraph(dc, pict2)); section.Blocks.Add(localpict); // Save our document into DOCX format. string resultPath = @"result.docx"; dc.Save(resultPath); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true }); }
/// <summary> /// Iterates into body items. /// </summary> private void RemoveFieldCodesInTextBody(WTextBody textBody) { for (int i = 0; i < textBody.ChildEntities.Count; i++) { //IEntity is the basic unit in DocIO DOM. IEntity bodyItemEntity = textBody.ChildEntities[i]; //A Text body has 3 types of elements - Paragraph, Table, and Block Content Control //Decides the element type by using EntityType switch (bodyItemEntity.EntityType) { case EntityType.Paragraph: WParagraph paragraph = bodyItemEntity as WParagraph; //Iterates into paragraph items. RemoveFieldCodesInParagraph(paragraph.Items); break; case EntityType.Table: //Table is a collection of rows and cells //Iterates through table's DOM RemoveFieldCodesInTable(bodyItemEntity as WTable); break; case EntityType.BlockContentControl: BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl; //Iterates to the body items of Block Content Control. RemoveFieldCodesInTextBody(blockContentControl.TextBody); break; } } }
static void Example1() { var document = new DocumentModel(); var section = new Section(document); document.Sections.Add(section); // Create locked Rich Text Content Control. var richTextControl = new BlockContentControl(document, ContentControlType.RichText, new Paragraph(document, "This text is inside Rich Text Content Control."), new Paragraph(document, "It cannot be deleted or edited.")); richTextControl.Properties.LockEditing = true; richTextControl.Properties.LockDeleting = true; section.Blocks.Add(richTextControl); // Create named Plain Text Content Control. var plainTextControl = new BlockContentControl(document, ContentControlType.PlainText, new Paragraph(document, "Plain Text Content Control with tag and title.")); plainTextControl.Properties.Tag = "Plain Text Name"; plainTextControl.Properties.Title = "Plain Text Title"; section.Blocks.Add(plainTextControl); // Create CheckBox Content Control. var checkBoxControl = new InlineContentControl(document, ContentControlType.CheckBox, new Run(document, "☒") { CharacterFormat = { FontName = "MS Gothic" } }); checkBoxControl.Properties.Checked = true; // Create ComboBox Content Control. var comboBoxControl = new InlineContentControl(document, ContentControlType.ComboBox, new Run(document, "<Select GemBox Component>")); comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("<Select GemBox Component>", "NONE")); comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Spreadsheet", "GBS")); comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Document", "GBD")); comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Pdf", "GBA")); comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Presentation", "GBP")); comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Email", "GBE")); section.Blocks.Add(new Paragraph(document, checkBoxControl, new SpecialCharacter(document, SpecialCharacterType.LineBreak), comboBoxControl)); document.Save("Content Controls.docx"); }
/// <summary> /// Inserting a Rich text content control. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-rich-text-net-csharp-vb.php /// </remarks> static void InsertRichText() { // Let's create a simple document. DocumentCore dc = new DocumentCore(); // Create a rich text content control. BlockContentControl rt = new BlockContentControl(dc, ContentControlType.RichText); dc.Sections.Add(new Section(dc, rt)); // Add the content control properties. rt.Properties.Title = "Rich text content control."; rt.Properties.Color = Color.Blue; rt.Properties.LockDeleting = true; rt.Properties.LockEditing = true; rt.Document.DefaultCharacterFormat.FontColor = Color.Orange; // Add new paragraphs with formatted text. rt.Blocks.Add(new Paragraph(dc, "Line 1")); rt.Blocks.Add(new Paragraph(dc, "Line 2")); rt.Blocks.Add(new Paragraph(dc, "Line 3")); // Add a picture and shape to the block. string pictPath = @"..\..\banner_sautinsoft.jpg"; Picture pict = new Picture(dc, InlineLayout.Inline(new Size(400, 100)), pictPath); Shape shp = new Shape(dc, Layout.Inline(new Size(4, 1, LengthUnit.Centimeter))); Paragraph im = new Paragraph(dc); rt.Blocks.Add(im); im.Inlines.Add(pict); Paragraph sh = new Paragraph(dc); rt.Blocks.Add(sh); sh.Inlines.Add(shp); rt.Blocks.Add(new Paragraph(dc, "Line 4")); // Save our document into DOCX format. string resultPath = @"result.docx"; dc.Save(resultPath, new DocxSaveOptions()); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true }); }
/// <summary> /// Inserting a plain text content control. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-plain-text-net-csharp-vb.php /// </remarks> static void InsertPlainText() { // Let's create a simple document. DocumentCore dc = new DocumentCore(); // Create a plain text content control. BlockContentControl pt = new BlockContentControl(dc, ContentControlType.PlainText); // Add a new section. dc.Sections.Add(new Section(dc, pt)); // Add the content control properties. pt.Properties.Title = "Title"; pt.Properties.Multiline = true; pt.Properties.Color = Color.Blue; pt.Document.DefaultCharacterFormat.FontColor = Color.Orange; // Add new paragraph with formatted text. pt.Blocks.Add(new Paragraph(dc, new Run(dc, "This is first paragraph with symbols added on a new line."), new SpecialCharacter(dc, SpecialCharacterType.LineBreak), new Run(dc, "This is a new line in the first paragraph."), new SpecialCharacter(dc, SpecialCharacterType.LineBreak), new Run(dc, "Insert the \"Wingdings\" font family with formatting."), new Run(dc, "\xFC" + "\xF0" + "\x32") { CharacterFormat = { FontName = "Wingdings", FontColor = new Color("#000000"), Size = 48 } })); // Save our document into DOCX format. string resultPath = @"result.docx"; dc.Save(resultPath, new DocxSaveOptions()); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true }); }
private void button1_Click(object sender, System.EventArgs e) { try { // Get Template document and database path. string dataPath = Application.StartupPath + @"..\..\..\..\..\..\..\common\Data\DocIO\"; //Creates an empty Word document instance. WordDocument document = new WordDocument(); //Opens template document. document.Open(System.IO.Path.Combine(dataPath, "ContentControlTemplate.docx")); IWTextRange textRange; //Gets table from the template document. IWTable table = document.LastSection.Tables[0]; WTableRow row = table.Rows[1]; #region Inserting content controls #region Calendar content control IWParagraph cellPara = row.Cells[0].Paragraphs[0]; //Accesses the date picker content control. IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl); textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets today's date to display. textRange.Text = DateTime.Now.ToShortDateString(); textRange.CharacterFormat.FontSize = 14; //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; #endregion #region Plain text content controls table = document.LastSection.Tables[1]; row = table.Rows[0]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind Analytics"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind"; textRange.CharacterFormat.FontSize = 14; row = table.Rows[1]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "10"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "Nancy Davolio"; textRange.CharacterFormat.FontSize = 14; #endregion #region CheckBox Content control row = table.Rows[2]; cellPara = row.Cells[0].LastParagraph; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("C#, "); textRange.CharacterFormat.FontSize = 14; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("VB"); textRange.CharacterFormat.FontSize = 14; #endregion #region Drop down list content control cellPara = row.Cells[1].LastParagraph; //Accesses the dropdown list content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default option to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "ASP.NET"; textRange.CharacterFormat.FontSize = 14; inlineControl.ParagraphItems.Add(textRange); //Adds items to the dropdown list. ContentControlListItem item; item = new ContentControlListItem(); item.DisplayText = "ASP.NET MVC"; item.Value = "2"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Windows Forms"; item.Value = "3"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "WPF"; item.Value = "4"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Xamarin"; item.Value = "5"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); #endregion #region Calendar content control row = table.Rows[3]; cellPara = row.Cells[0].LastParagraph; //Accesses the date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(-5).ToShortDateString(); textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Inserts date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(10).ToShortDateString(); textRange.CharacterFormat.FontSize = 14; #endregion #endregion #region Block content control //Accesses the block content control. BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl); //Protects the block content control blockContentControl.ContentControlProperties.LockContents = true; #endregion //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()); } } // Exit this.Close(); } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
public IActionResult FormFillingAndProtection(string Button) { if (Button == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/ContentControlTemplate.docx"; string contenttype1 = "application/vnd.ms-word.document.12"; // Load Template document stream. FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (Button == "View Template") { return(File(fileStream, contenttype1, "ContentControlTemplate.docx")); } // Creates an empty Word document instance. WordDocument document = new WordDocument(); // Opens template document. document.Open(fileStream, FormatType.Docx); fileStream.Dispose(); fileStream = null; IWTextRange textRange; //Gets table from the template document. IWTable table = document.LastSection.Tables[0]; WTableRow row = table.Rows[1]; #region Inserting content controls #region Calendar content control IWParagraph cellPara = row.Cells[0].Paragraphs[0]; //Accesses the date picker content control. IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl); textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets today's date to display. textRange.Text = DateTime.Now.ToString("d"); textRange.CharacterFormat.FontSize = 14; //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; #endregion #region Plain text content controls table = document.LastSection.Tables[1]; row = table.Rows[0]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind Analytics"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind"; textRange.CharacterFormat.FontSize = 14; row = table.Rows[1]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "10"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "Nancy Davolio"; textRange.CharacterFormat.FontSize = 14; #endregion #region CheckBox Content control row = table.Rows[2]; cellPara = row.Cells[0].LastParagraph; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("C#, "); textRange.CharacterFormat.FontSize = 14; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("VB"); textRange.CharacterFormat.FontSize = 14; #endregion #region Drop down list content control cellPara = row.Cells[1].LastParagraph; //Accesses the dropdown list content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default option to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "ASP.NET"; textRange.CharacterFormat.FontSize = 14; inlineControl.ParagraphItems.Add(textRange); //Adds items to the dropdown list. ContentControlListItem item; item = new ContentControlListItem(); item.DisplayText = "ASP.NET MVC"; item.Value = "2"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Windows Forms"; item.Value = "3"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "WPF"; item.Value = "4"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Xamarin"; item.Value = "5"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); #endregion #region Calendar content control row = table.Rows[3]; cellPara = row.Cells[0].LastParagraph; //Accesses the date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(-5).ToString("d"); textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Inserts date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(10).ToString("d"); textRange.CharacterFormat.FontSize = 14; #endregion #endregion #region Block content control //Accesses the block content control. BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl); //Protects the block content control blockContentControl.ContentControlProperties.LockContents = true; #endregion FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
/// <summary> /// Button click event for FormFillingAndProtection. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event args</param> private void Button_Click_1(object sender, RoutedEventArgs e) { // Load Template document stream. Stream inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.ContentControlTemplate.docx"); // Creates an empty Word document instance. WordDocument document = new WordDocument(); // Opens template document. document.Open(inputStream, FormatType.Docx); IWTextRange textRange; //Gets table from the template document. IWTable table = document.LastSection.Tables[0]; WTableRow row = table.Rows[1]; #region Inserting content controls #region Calendar content control IWParagraph cellPara = row.Cells[0].Paragraphs[0]; //Accesses the date picker content control. IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl); textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets today's date to display. textRange.Text = DateTime.Now.ToString("d"); textRange.CharacterFormat.FontSize = 14; //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; #endregion #region Plain text content controls table = document.LastSection.Tables[1]; row = table.Rows[0]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind Analytics"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind"; textRange.CharacterFormat.FontSize = 14; row = table.Rows[1]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "10"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "Nancy Davolio"; textRange.CharacterFormat.FontSize = 14; #endregion #region CheckBox Content control row = table.Rows[2]; cellPara = row.Cells[0].LastParagraph; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("C#, "); textRange.CharacterFormat.FontSize = 14; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("VB"); textRange.CharacterFormat.FontSize = 14; #endregion #region Drop down list content control cellPara = row.Cells[1].LastParagraph; //Accesses the dropdown list content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default option to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "ASP.NET"; textRange.CharacterFormat.FontSize = 14; inlineControl.ParagraphItems.Add(textRange); //Adds items to the dropdown list. ContentControlListItem item; item = new ContentControlListItem(); item.DisplayText = "ASP.NET MVC"; item.Value = "2"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Windows Forms"; item.Value = "3"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "WPF"; item.Value = "4"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Xamarin"; item.Value = "5"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); #endregion #region Calendar content control row = table.Rows[3]; cellPara = row.Cells[0].LastParagraph; //Accesses the date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(-5).ToString("d"); textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Inserts date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(10).ToString("d"); textRange.CharacterFormat.FontSize = 14; #endregion #endregion #region Block content control //Accesses the block content control. BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl); //Protects the block content control blockContentControl.ContentControlProperties.LockContents = true; #endregion Save(document); }
/// <summary> /// Button click event for FormFillingAndProtection. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event args</param> private void OnButtonClicked(object sender, EventArgs e) { #if COMMONSB string rootPath = "SampleBrowser.Samples.DocIO.Samples.Templates."; #else string rootPath = "SampleBrowser.DocIO.Samples.Templates."; #endif // Load Template document stream. Stream inputStream = typeof(FormFillingAndProtection).GetTypeInfo().Assembly.GetManifestResourceStream(rootPath + "ContentControlTemplate.docx"); // Creates an empty Word document instance. WordDocument document = new WordDocument(); // Opens template document. document.Open(inputStream, FormatType.Docx); IWTextRange textRange; //Gets table from the template document. IWTable table = document.LastSection.Tables[0]; WTableRow row = table.Rows[1]; #region Inserting content controls #region Calendar content control IWParagraph cellPara = row.Cells[0].Paragraphs[0]; //Accesses the date picker content control. IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl); textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets today's date to display. textRange.Text = DateTime.Now.ToString("d"); textRange.CharacterFormat.FontSize = 14; //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; #endregion #region Plain text content controls table = document.LastSection.Tables[1]; row = table.Rows[0]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind Analytics"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind"; textRange.CharacterFormat.FontSize = 14; row = table.Rows[1]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "10"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "Nancy Davolio"; textRange.CharacterFormat.FontSize = 14; #endregion #region CheckBox Content control row = table.Rows[2]; cellPara = row.Cells[0].LastParagraph; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("C#, "); textRange.CharacterFormat.FontSize = 14; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("VB"); textRange.CharacterFormat.FontSize = 14; #endregion #region Drop down list content control cellPara = row.Cells[1].LastParagraph; //Accesses the dropdown list content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default option to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "ASP.NET"; textRange.CharacterFormat.FontSize = 14; inlineControl.ParagraphItems.Add(textRange); //Adds items to the dropdown list. ContentControlListItem item; item = new ContentControlListItem(); item.DisplayText = "ASP.NET MVC"; item.Value = "2"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Windows Forms"; item.Value = "3"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "WPF"; item.Value = "4"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Xamarin"; item.Value = "5"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); #endregion #region Calendar content control row = table.Rows[3]; cellPara = row.Cells[0].LastParagraph; //Accesses the date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(-5).ToString("d"); textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Inserts date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(10).ToString("d"); textRange.CharacterFormat.FontSize = 14; #endregion #endregion #region Block content control //Accesses the block content control. BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl); //Protects the block content control blockContentControl.ContentControlProperties.LockContents = true; #endregion MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); document.Close(); if (Device.RuntimePlatform == Device.UWP) DependencyService.Get<ISaveWindowsPhone>() .Save("FormFillingAndProtection.docx", "application/msword", stream); else DependencyService.Get<ISave>().Save("FormFillingAndProtection.docx", "application/msword", stream); }
public ActionResult FormFillingAndProtection(string Button) { if (Button == null) { return(View()); } if (Button == "View Template") { return(new TemplateResult("ContentControlTemplate.docx", ResolveApplicationDataPath("App_Data\\DocIO"), HttpContext.ApplicationInstance.Response)); } //Creates an Word document instance. WordDocument document = new WordDocument(); //Loads the template document. document.Open(ResolveApplicationDataPath("ContentControlTemplate.docx", "App_Data\\DocIO")); IWTextRange textRange; //Gets table from the template document. IWTable table = document.LastSection.Tables[0]; WTableRow row = table.Rows[1]; #region Fill data and lock the contents of content control #region Calendar content control IWParagraph cellPara = row.Cells[0].Paragraphs[0]; //Accesses the date picker content control. IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl); textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets today's date to display. textRange.Text = DateTime.Now.ToShortDateString(); textRange.CharacterFormat.FontSize = 14; //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; #endregion #region Plain text content controls table = document.LastSection.Tables[1]; row = table.Rows[0]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind Analytics"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; textRange = inlineControl.ParagraphItems[0] as WTextRange; //Sets text in plain text content control. textRange.Text = "Northwind"; textRange.CharacterFormat.FontSize = 14; row = table.Rows[1]; cellPara = row.Cells[0].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "10"; textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Accesses the plain text content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); //Protects the content control. inlineControl.ContentControlProperties.LockContents = true; //Sets text in plain text content control. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "Nancy Davolio"; textRange.CharacterFormat.FontSize = 14; #endregion #region CheckBox Content control row = table.Rows[2]; cellPara = row.Cells[0].LastParagraph; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("C#, "); textRange.CharacterFormat.FontSize = 14; //Inserts checkbox content control. inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox); inlineControl.ContentControlProperties.LockContents = true; //Sets checkbox as checked state. inlineControl.ContentControlProperties.IsChecked = true; textRange = cellPara.AppendText("VB"); textRange.CharacterFormat.FontSize = 14; #endregion #region Drop down list content control cellPara = row.Cells[1].LastParagraph; //Accesses the dropdown list content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default option to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = "ASP.NET"; textRange.CharacterFormat.FontSize = 14; inlineControl.ParagraphItems.Add(textRange); //Adds items to the dropdown list. ContentControlListItem item; item = new ContentControlListItem(); item.DisplayText = "ASP.NET MVC"; item.Value = "2"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Windows Forms"; item.Value = "3"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "WPF"; item.Value = "4"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); item = new ContentControlListItem(); item.DisplayText = "Xamarin"; item.Value = "5"; inlineControl.ContentControlProperties.ContentControlListItems.Add(item); #endregion #region Calendar content control row = table.Rows[3]; cellPara = row.Cells[0].LastParagraph; //Accesses the date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(-5).ToShortDateString(); textRange.CharacterFormat.FontSize = 14; cellPara = row.Cells[1].LastParagraph; //Inserts date picker content control. inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl); inlineControl.ContentControlProperties.LockContents = true; //Sets default date to display. textRange = inlineControl.ParagraphItems[0] as WTextRange; textRange.Text = DateTime.Now.AddDays(10).ToShortDateString(); textRange.CharacterFormat.FontSize = 14; #endregion #region Block content control //Accesses the block content control. BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl); //Protects the block content control blockContentControl.ContentControlProperties.LockContents = true; #endregion #endregion return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); }