Form Print Support

Hi,
We need to print some controls in our winform [ textbox contents, richtextbox contents etc which are arranged in a table / one or more tables]. The contents which should be printed depends on the bussiness requirements.We are presently doing this by creating wordML and open the printable data in a winword [ finding out all the tables and their positions, traversing through all the tables and controls, check if this is printable and find out the position of the control in the table/cell ( top/left etc) and creating corresponding wordML]. All the controls will be placed inside table/cell. One cell will have only one control and no cells would be empty. Is there any possible way to do this task using aspose words? Final out put can be shown as a doc, pdf or aspose words viewer.
Please let me know if you are looking for more clarification on this.
Thanks in advance,
Ali

Hi
Thanks for your inquiry. It is not quite clear for me what you need to achieve. If you need just insert content from winforms controls (like TextBox and RichTextBox) into the word document then you can easily achieve this using DocumentBuilder.
You can learn how to insert RTF string into the document using Aspose.Words here
https://forum.aspose.com/t/109707
Please provide me more specific information about your needs and I will try to help you.
Best regards.

Hi,
Thanks for the reply. We looked into DocumentBuilder, could you please explain how to insert tables to a document, then fix the position of the text in each cell by specifying whether it is aligned top-left / top-right etc.
Thanks and Regards,
Ali

Hi
Thanks for your inquiry. You can use Alignment to specify position of text inside cell. For example see the following code:

// Create new document
Document doc = new Document();
// Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify default width of cell
builder.CellFormat.Width = 100;
// Spacify height
builder.RowFormat.Height = 100;
// Generate table.
builder.InsertCell();
// Text will be at the top of cell
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
// Text wil be in the left corner
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
// insert text
builder.Write("test");
// Repaet this procedure
builder.InsertCell();
// Text will be at the top of cell
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Bottom;
// Text wil be in the left corner
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
// insert text
builder.Write("test");
// End row
builder.EndRow();
// End table
builder.EndTable();
// save document
doc.Save(@"Test219\out.doc");

Hope this helps.
Best regards.

Hi thanks for the help,
I looked into the article “Inserting RTF preformatted text into a doc” and it actually replace a text in the document by another document which contains rtf text. But I could find, if I use this, it will replace all the matching text which i dont need to be replaced. for eg: suppose the doc contains a table of four cells , first and third cell contains same text or some part is same, lets say first and thrid cell contains text “hello world”, the format of the two cell texts are different, say one is bold and other is italic and we have to replace the text in the first cell only [with bold “hello world”] , it will replace the text in third cell too.
Also, when i replaced the specified string in a doc with a rtf string it works, but for my requirement, what I tried to do is that,
create a form with tablelayout of having two rows and two columns, place richtextboxes in all cells, in a button click call the mothod BuildDocument(), traverse through all the cells, create the docuement using rtf text in the richtextboxes. But the original formatted text is not saved in the final word doc
Please see the below code.

int currentColCount = 0;
int currentRowCount = 0;
private void BuildDocument()
{
    // Create new document
    Document doc = new Document();
    // Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(doc);
    // traverse through table cells
    for (int rowCount = 0; rowCount < tbl1.RowCount; rowCount++)
    {
        for (int colCount = 0; colCount < tbl1.ColumnCount; colCount++)
        {
            // Specify default width of cell
            builder.CellFormat.Width = 100;
            // Spacify height
            builder.RowFormat.Height = 100;
            // specify border style
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            // Generate table.
            builder.InsertCell();
            // Text will be at the top of cell
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
            // Text wil be in the left corner
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            currentRowCount = rowCount;
            currentColCount = colCount;
            // insert text 
            RichTextBox rtfControl = (RichTextBox)tbl1.GetControlFromPosition(colCount, rowCount);
            builder.Write(rtfControl.Text);
            // Create regex
            Regex regex = new Regex(rtfControl.Text);
            // Replace placeholder with rtf using ReplaceEvaluator
            doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluatorForm), false);
        }
        // End row
        builder.EndRow();
        // End table
        builder.EndTable();
    }
    doc.Save(@"C:\Documents and Settings\abdul.ali\Desktop\DocumentBuild.doc");
}
private ReplaceAction ReplaceEvaluatorForm(object sender, ReplaceEvaluatorArgs e)
{
    // Get MatchNode
    Paragraph parentParagraph = e.MatchNode.ParentNode as Paragraph;
    RichTextBox rtfControl = (RichTextBox)tbl1.GetControlFromPosition(currentColCount, currentRowCount);
    string rtfString = rtfControl.Rtf;
    rtfControl = null;
    // Convert string to byte array
    byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
    // Sreate stream
    MemoryStream rtfStream = new MemoryStream(rtfBytes);
    Document rtfDoc = new Document(rtfStream);
    rtfStream.Close();
    // Insert rtd document into destination document
    InsertDocument(parentParagraph, rtfDoc);
    rtfDoc = null;
    // Return Replace action (remove palaceholder)
    return ReplaceAction.Replace;
}

Please advice to get the correct formatted data on the final Doc. Is this because of improper closing of rows and tables?
Regards,
Ali

int currentColCount = 0;
int currentRowCount = 0;
[Test]
public void Test222()
{
    // Create new document
    Document doc = new Document();
    // Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(doc);
    // traverse through table cells
    for (int rowCount = 0; rowCount < 3; rowCount++)
    {
        for (int colCount = 0; colCount < 2; colCount++)
        {
            // Specify default width of cell
            builder.CellFormat.Width = 100;
            // Spacify height
            builder.RowFormat.Height = 100;
            // specify border style
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            // Generate table.
            builder.InsertCell();
            // Text will be at the top of cell
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
            // Text wil be in the left corner
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            currentRowCount = rowCount;
            currentColCount = colCount;
            // insert text 
            // HERE YOU SHOULD READ RTF FROM YOUR RICHTEXTBOX
            string rtfString = File.ReadAllText(@"Test222\in.rtf");
            // Convert string to byte array
            byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
            // Sreate stream
            MemoryStream rtfStream = new MemoryStream(rtfBytes);
            Document rtfDoc = new Document(rtfStream);
            rtfStream.Close();
            // Insert rtd document into destination document
            InsertDocument(builder.CurrentParagraph, rtfDoc);
            rtfDoc = null;
            builder.CurrentParagraph.Remove();
        }
        // End row
        builder.EndRow();
    }
    // End table
    builder.EndTable();
    doc.Save(@"Test222\out.doc");
}

Hope this helps.
Best regards.