Spire.Doc for .NET (337)
Children categories
Add checkbox and picture content control to word document in C#
2019-05-29 07:31:42 Written by KoohjiBesides the Combo Box, Text, Date Picker and Drop-Down List content controls, Checkbox and picture content control also are the mostly used content control in word document. Spire.Doc supports to add many kinds of content controls to the word document. This article will show you how to add checkbox and picture content control to word document by Spire.Doc for .NET.
Code snippets of how to add checkbox and picture content control:
using System;
using System.Drawing;
namespace AddCheckbox
{
class Program
{
static void Main(string[] args)
{
//Create a new word document
Document document = new Document();
//Add a section to the document
Section section = document.AddSection();
//Add a document to the section
Paragraph paragraph = section.AddParagraph();
//Add checkbox content control
StructureDocumentTagInline sdt = new StructureDocumentTagInline(document);
paragraph = section.AddParagraph();
sdt = new StructureDocumentTagInline(document);
sdt.CharacterFormat.FontSize = 20;
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.CheckBox;
SdtCheckBox scb = new SdtCheckBox();
sdt.SDTProperties.ControlProperties = scb;
TextRange tr = new TextRange(document);
tr.CharacterFormat.FontName = "MS Gothic";
tr.CharacterFormat.FontSize = 20;
sdt.ChildObjects.Add(tr);
scb.Checked = true;
sdt.SDTProperties.Alias = "CheckoBox";
sdt.SDTProperties.Tag = "Checkbox";
//Add picture content control
paragraph = section.AddParagraph();
sdt = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.ControlProperties = new SdtPicture();
sdt.SDTProperties.Alias = "Picture";
sdt.SDTProperties.Tag = "Picture";
DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };
pic.LoadImage(Image.FromFile("Logo.jpg"));
sdt.SDTContent.ChildObjects.Add(pic);
document.SaveToFile("Sample.docx", FileFormat.Docx2013);
}
}
}
Effective screenshot after adding checkbox and picture content control to word document:

C# add new text strings after the searched text string in word document
2019-04-04 07:54:55 Written by KoohjiWith Spire.Doc for .NET, we can easily insert new text to word document at exact position, it also supports to insert new text after the certain text strings at many places. This article will show you how to insert new text strings after the searched text string in word document.
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace Word
{
class Program
{
static void Main(string[] args)
{
//load the sample document
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
//find all the text string “New Zealand” from the sample document
TextSelection[] selections = doc.FindAllString("New Zealand", true, true);
int index = 0;
//defines text range
TextRange range = new TextRange(doc);
//insert new text string (NY) after the searched text string
foreach (TextSelection selection in selections)
{
range = selection.GetAsOneRange();
TextRange newrange = new TextRange(doc);
newrange.Text = ("(NY)");
index = range.OwnerParagraph.ChildObjects.IndexOf(range);
range.OwnerParagraph.ChildObjects.Insert(index + 1, newrange);
}
//find and highlight the newly added text string NY
TextSelection[] text2 = doc.FindAllString("NY", false, true);
foreach (TextSelection seletion in text2)
{
seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
}
//save the document
doc.SaveToFile("Result.docx", FileFormat.Docx2010);
}
}
}
Effective screenshot after adding the text strings to the searched text:

PCL File is Digital printed document created in the Printer Command Language (more commonly referred to as PCL) page description language. From v7.1.19, Spire.Doc supports to convert word document to PCL. There are many kinds of standard for PCL document; the PCL here refers to PCL 6 (PCL 6 Enhanced or PCL XL). This article will show you how to save word document to PCL in C# and VB.NET by only three lines of codes.
using Spire.Doc;
namespace DOCPCL
{
class Program
{
static void Main(string[] args)
{
//load the sample document
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
//save the document as a PCL file
doc.SaveToFile("Result.pcl", FileFormat.PCL);
}
}
}
Imports Spire.Doc
Namespace DOCPCL
Class Program
Private Shared Sub Main(args As String())
'load the sample document
Dim doc As New Document()
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)
'save the document as a PCL file
doc.SaveToFile("Result.pcl", FileFormat.PCL)
End Sub
End Class
End Namespace