Create a Document Programmatically
Reports, letters and confirmations are often built by the application rather than typed by a user. With ServerTextControl, your code writes a document the same way a user would in the editor, from formatted headings and body text to tables, images and headers, and then saves it or hands it to the editor. The steps below build a short report in a button handler, save it as DOCX and show it in the TextControl of your form.
Prerequisites
- The application from Create Your First Windows Forms Application, with a
TextControlnamedtextControl1onForm1. - A
ButtonnamedcreateButtonon the form. Double-click it in the designer to create itsClickhandler. - An image named logo.png in the project, with its Copy to Output Directory property set to Copy always, so that it is next to the application when it runs.
How Code Builds a Document
A ServerTextControl holds a document without showing it. Like a user in the editor, your code works at the current input position: Selection sets the formatting and inserts text there, and new tables and images are added there as well. The input position moves on with each insertion, so the document grows from top to bottom in the order of your code.
ServerTextControl in the Overview explains the component.
Build the Document
Add the following code to Form1.cs:
using System;
using System.Windows.Forms;
using TXTextControl;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void createButton_Click(object sender, EventArgs e)
{
byte[] document;
using (ServerTextControl tx = new ServerTextControl())
{
// Initialize the new instance before using it
tx.Create();
// Heading: Arial, 24 pt, bold, centered
tx.Selection.FontName = "Arial";
tx.Selection.FontSize = 480; // 480 twips = 24 pt
tx.Selection.Bold = true;
tx.Selection.ParagraphFormat = new ParagraphFormat()
{
Alignment = TXTextControl.HorizontalAlignment.Center
};
tx.Selection.Text = "Quarterly Report\r\n";
// Body text: 12 pt, regular, left-aligned
tx.Selection.FontSize = 240; // 240 twips = 12 pt
tx.Selection.Bold = false;
tx.Selection.ParagraphFormat = new ParagraphFormat()
{
Alignment = TXTextControl.HorizontalAlignment.Left
};
tx.Selection.Text = "The following table lists the results of the quarter.\r\n";
// Table with 4 rows, 3 columns and the ID 10
tx.Tables.Add(4, 3, 10);
Table table = tx.Tables.GetItem(10);
TableRow headerRow = table.Rows.GetItem(1);
headerRow.IsHeader = true;
headerRow.CellFormat = new TableCellFormat()
{
BackColor = System.Drawing.Color.LightGray
};
foreach (TableCell cell in table.Cells)
{
cell.Text = "Row " + cell.Row.ToString() + ", Column " + cell.Column.ToString();
}
// A paragraph and an image below the table
tx.Selection.Text = "Prepared by the sales department.\r\n";
TXTextControl.Image logo = new TXTextControl.Image("logo.png", 4); // 4 = PNG
tx.Images.Add(logo, -1); // -1 = current input position
// Header on every page
tx.HeadersAndFooters.Add(HeaderFooterType.Header);
HeaderFooter header = tx.HeadersAndFooters.GetItem(HeaderFooterType.Header);
header.Selection.Text = "Sample Company Inc.";
// Save as DOCX, and as a byte array for the editor
tx.Save("report.docx", StreamType.WordprocessingML);
tx.Save(out document, BinaryStreamType.InternalUnicodeFormat);
}
// Show the document in the editor
textControl1.Load(document, BinaryStreamType.InternalUnicodeFormat);
}
}
Keep the namespace that Visual Studio created around the class.
What the Code Does
- Create. Create initializes the
ServerTextControl. It must be called before any other member. Theusingstatement disposes the instance when the document is done. - Text and formatting. FontName, FontSize and Bold set the character formatting, and ParagraphFormat with its Alignment the paragraph formatting of the text that Selection.Text inserts next. Font sizes are given in twips, 20 twips per point.
\r\nends a paragraph. - Qualified names.
HorizontalAlignmentandImageexist in Windows Forms and in TX Text Control. In a form, writeTXTextControl.HorizontalAlignmentandTXTextControl.Image. - The table. Tables.Add inserts a table at the input position with an ID of your choice, and
Tables.GetItemwith that ID returns it. Rows are numbered from 1. IsHeader makes the first row repeat at the top of each page, and CellFormat gives it a background color. TableCell.Text fills each cell. After the table is added, the input position is below it. - The image. The Image constructor takes the file name and the format, where 4 is PNG. Images.Add with -1 inserts it at the input position, like a character in the text.
- The header. HeadersAndFooters.Add adds a header to the document, and the Selection of the HeaderFooter inserts its text.
- Saving. Save writes report.docx to the output folder, and a second time to a byte array in the TX Text Control format. TextControl.Load shows that byte array in the editor.
Run the Application
Press F5 and click createButton. The editor shows the report: the centered heading, the text, the table with its gray header row, the closing line and the image. In page view, the header "Sample Company Inc." appears at the top of the page. The file report.docx in the output folder contains the same document.
Build the Document in the Editor Instead
The same calls work on the TextControl itself, where the user sees the document grow:
textControl1.Selection.FontSize = 480;
textControl1.Selection.Text = "Quarterly Report\r\n";
textControl1.Tables.Add(4, 3, 10);
textControl1.HeadersAndFooters.Add(HeaderFooterType.Header);
A ServerTextControl keeps the editor free while the document is built and can run where no form exists. Headers and footers appear in the editor only in page view.
Next Steps
- Save as DOCX and PDF: save and convert the document in other formats.
- Print a Document: print the document with or without the editor.
- Creating Documents: text, tables, images, sections and fields in code in detail.
- Create a Template: fill documents with data instead of building them in code.