Develop Microsoft Word © documents from your data with C1Word, a powerful, extensible UI-less library available for UWP, WinForms, and WPF. This beta's object model is easily programmable and provides all the advanced properties and methods required to generate both MS Word and RTF documents. The Document created using C1Word can be used in application to export/email data in standard Microsoft Word Document format, so that it can be further used. The C1Word generated documents can also be stored in file system for later use, or in any database that supports file streams. Some standard use-cases supported by C1Word are -
In 2016v1, C1Word Library (Beta) is supported on following platforms -
If you want to modify any existing Word document, you can parse the document elements and also modify them using C1Word Library. This helps you to modify the document programmatically at runtime. A document's elements can be accessed using C1Word.MainSection object Here is how you can access individual elements of a document -
// load the document
var doc = new C1WordDocument();
doc.Load("file path.docx");
// document's information
var info = doc.Info;
var a = info.Author + info.Company + info.CreationTime.ToString();
// document's structure
foreach (var obj in doc.MainSection.Header)
{
// header objects
}
foreach (var obj in doc.MainSection.Content)
{
// content objects
}
foreach (var obj in doc.MainSection.Footer)
{
// footer objects
}
foreach (var obj in doc.MainSection.Columns)
{
// columns
}
// page size
var sz = doc.MainSection.PageSize;
// margins
var lm = doc.MainSection.LeftMargin;
var rm = doc.MainSection.RightMargin;
var tm = doc.MainSection.TopMargin;
var bm = doc.MainSection.BottomMargin;
You can load .docx/.rtf files in C1Word component using C1Word.Load(
When you are working with large number of Word documents and you want to merge them in a single document, you can simply copy paste data, but only in cases where document content is not very large. But what if different Word documents have large data? In such cases, you can programmatically merge Word documents (.docx/.rtf) using C1Word library -
// load two files
var doc1 = new C1WordDocument();
doc1.Load("File1.docx");
var doc2 = new C1WordDocument();
doc2.Load("File2.rtf");
// merge (add to end)
foreach (var obj in doc2.MainSection)
{
doc1.Add(obj);
}
// save result
doc1.Save("MergedFile.docx");
You can not only add text to your existing documents, but can even specify the font and forecolor. This is particularly useful if you programmatically want to add text to a document in the middle of an application.
Following code shows how to add text using C1Word.AddParagraph method. This is a part of code used in the word document snapshot below -
//Add Text using AddParagraph method
Font font = new Font("Segoe UI", 18, FontStyle.Bold);
C1WordDocument.AddParagraph("What's Inside ComponentOne Studio", font, Color.Red, RtfHorizontalAlignment.Left)
//Add border to paragraph
RtfParagraph paragraph = new RtfParagraph();
paragraph.TopBorderStyle = RtfBorderStyle.Single;
c1Word.Add(paragraph);
Presenting data in tabular format is very useful and common in Word documents. C1Word includes RTFTable object that helps you add data to table cells. You can also format the cells like adding borders, background, etc., and edit the cell alignment. Following is a part of the code used to add a table to word document snaphot below -
//Create and Add Table
var doc = new C1WordDocument();
var fontName = "Segoe UI";
var headerFont = new Font(fontName, 12, FontStyle.Bold);
var tableFont = new Font(fontName, 12, FontStyle.Regular);
var contextFont = new Font(fontName, 10, FontStyle.Regular);
// Table data
var list = new List<string[]>();
list.Add(new string[] { "Service", "Standard support", "Platinum support" });
list.Add(new string[] { "24 Hour FastTrack Response *", "0", "1" });
list.Add(new string[] { "Industry Leading Phone Support", "1", "1" });
list.Add(new string[] { "Access to hotfix builds", "1", "1" });
list.Add(new string[] { "ComponentOne Studio Forums", "1", "1" });
list.Add(new string[] { "Access to 3 major releases per year", "1", "1" });
list.Add(new string[] { "Online Support", "1", "1" });
list.Add(new string[] { "Email project for Analysis", "1", "1" });
// Add Table
doc.LineBreak();
int rows = list.Count;
int cols = 3;
RtfTable table = new RtfTable(rows, cols);
doc.Add(table);
for (int row = 0; row < rows; row++)
{
var data = list[row];
for (int col = 0; col < cols; col++)
{
// call object and data of the cell
var cell = table.Rows[row].Cells[col];
text = data[col];
// cell padding
var padding = 7;
cell.LeftPadding = padding;
cell.RightPadding = padding;
cell.TopPadding = padding;
cell.BottomPadding = padding;
if (col > 0)
{
cell.LeftBorderColor = Color.Black;
cell.LeftBorderWidth = 1.0f;
}
if (col < 2)
{
cell.RightBorderColor = Color.Black;
cell.RightBorderWidth = 1.0f;
}
// width of the first column
if (col == 0)
{
cell.Width = 220;
}
// cell content
var paragraph = new RtfParagraph();
if (row == 0)
{
paragraph.Font = headerFont;
paragraph.Add(new RtfString(text));
}
else
{
if (col == 0)
{
// item
paragraph.Font = tableFont;
paragraph.Add(new RtfString(text));
}
}
// alignment
if (col == 0)
{
paragraph.Alignment = RtfHorizontalAlignment.Left;
}
else
{
paragraph.Alignment = RtfHorizontalAlignment.Center;
}
// add to content
cell.Content.Add(paragraph);
}
}
Pictures give your simple text documents a complete professional look. C1Word library not only makes you add text to your word documents, but it also provides the C1Word.AddPicture method that can add any image as well as set it's alignment in the document.
//Add Picture
Image img;
img = new Bitmap(<Filename>);
c1Word.AddPicture(img, RtfHorizontalAlignment.Left);
Graphics help you to enhance information and add interest to your documents by providing visual elements in the form of different shapes. With C1Word, you can add the following shapes to your documents:
You can also fill these shapes using C1Word.FillRectangle, C1Word.FillEllipse etc. methods. Additionally, you can add C1Word.ShapeFillOpacity and C1Word.ShapeRotation effects on your shapes as per your needs. Following is simple code to add a Line and Rectangle shape to a Word document -
//Add Graphics (simple rect
RectangleF rc = new RectangleF(250, 100, 200, 200);
Bitmap image = new Bitmap(GetManifestResource("picture.jpg"));
c1Word.DrawImage(image, rc);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
rc = new RectangleF(250, 100, 150, 20);
c1Word.DrawLine(Pens.Orange, 200, 190, 400, 190);
rc = new RectangleF(150, 150, 190, 80);
using (Pen pen = new Pen(Brushes.Transparent, 5.0f))
{
c1Word.DrawRectangle(pen, rc);
}
c1Word.FillRectangle(Color.Green, rc);
c1Word.ShapeFillOpacity(50);
c1Word.ShapeRotation(25);
You can also add a rectangle around a Table row using following code -
// Create Word Document
var doc = new C1WordDocument();
// initialization
var text = string.Empty;
var fontName = "Calibri";
var headerFont = new Font(fontName, 12, FontStyle.Bold);
var tableFont = new Font(fontName, 12, FontStyle.Regular);
var contextFont = new Font(fontName, 10, FontStyle.Regular);
// table data
var list = new List<string[]>();
list.Add(new string[] { "Service", "Standard support", "Platinum support" });
list.Add(new string[] { "24 Hour FastTrack Response *", "0", "1" });
list.Add(new string[] { "Industry Leading Phone Support", "1", "1" });
list.Add(new string[] { "Access to hotfix builds", "1", "1" });
list.Add(new string[] { "ComponentOne Studio Forums", "1", "1" });
list.Add(new string[] { "Access to 3 major releases per year", "1", "1" });
list.Add(new string[] { "Online Support", "1", "1" });
list.Add(new string[] { "Email project for Analysis", "1", "1" });
// add table
doc.LineBreak();
int rows = list.Count;
int cols = 3;
RtfTable table = new RtfTable(rows, cols);
doc.Add(table);
for (int row = 0; row < rows; row++)
{
var data = list[row];
if (row == 1)
{
// platinum background
var r = table.Rows[row];
r.BackFilling = Color.LightYellow;
r.SetRectBorder(RtfBorderStyle.Single, Color.Red, 3.0f);
}
for (int col = 0; col < cols; col++)
{
// call object and data of the cell
var cell = table.Rows[row].Cells[col];
text = data[col];
// cell padding
var padding = 7;
cell.LeftPadding = padding;
cell.RightPadding = padding;
cell.TopPadding = padding;
cell.BottomPadding = padding;
// cell border
cell.SetRectBorder(RtfBorderStyle.Single, (row == 1) ? Color.Red : Color.Black, (row == 1) ? 3.0f : 1.0f);
// platinum attributes
if(row == 1)
{
if (col > 0)
{
cell.LeftBorderColor = Color.Black;
cell.LeftBorderWidth = 1.0f;
}
if (col < 2)
{
cell.RightBorderColor = Color.Black;
cell.RightBorderWidth = 1.0f;
}
cell.BackFilling = Color.LightYellow;
}
// width of the first column
if (col == 0)
{
cell.Width = 220;
}
// cell content
var paragraph = new RtfParagraph();
if (row == 0)
{
paragraph.Font = headerFont;
paragraph.Add(new RtfString(text));
}
else
{
if (col == 0)
{
// item
paragraph.Font = tableFont;
paragraph.Add(new RtfString(text));
}
else
{
// check
int value;
if (int.TryParse(text, out value) && value == 1)
{
paragraph.Add(new RtfPicture(img));
}
}
}
// alignment
if (col == 0)
{
paragraph.Alignment = RtfHorizontalAlignment.Left;
}
else
{
paragraph.Alignment = RtfHorizontalAlignment.Center;
}
// add to content
cell.Content.Add(paragraph);
}
}
When adding images to Word documents, it's best to use Microsoft's own imaging format – EMF and WMF. The library exclusively includes C1Word.DrawMetafile method that takes in metafile images (.wmf, .emf) and adds it to your documents.
//Add Metafile
Image img;
img = Metafile.FromFile(<Filename>);
c1Word.DrawMetafile((Metafile)img);
Please note that all documents in the snapshots above have been created using C1Word and opened in Microsoft Word © 2007. To review C1Word features in detail and see their implementation, please refer to C1Word sample here. What do you think about C1Word library? Please submit your comments below. Thanks!