Prior to aspose.total purchase - questions about aspose.words

I have some questions about Aspose.Words.

Our current mechanism of replacing tags inside tables is presented below:

  1. Copy whole source table
  2. Replace common tags for whole table
  3. Copy second row for each data source records
  4. Replace tag for every data source record

I try to get similar functionality from Aspose, but I have some problems.

  1. Can I add a row to the existing table? I have a table in Word
    document and want to add a set of rows to it. I cannot find any way to do it
    I have writen:
public class AsposeWordgood
{
    protected Document document;
    protected DocumentBuilder documentBuilder;

    public AsposeWord()
    {
        // evaluation licence 
        string licenseFile = "\\Aspose.Word.lic";
        if (File.Exists(licenseFile))
        {
            // This shows how to license Aspose.Word, if you don't specify a license, 
            // Aspose.Word works in evaluation mode. 
            License license = new License();
            license.SetLicense(licenseFile);
        }
    }

    private void replaceTabelarTags()
    {
        // this tag is inside the Word table 
        string tag = "#CASE_PRODUCT_GROUP_NAME#";
        Regex regex = new Regex(tag);

        document.Range.Replace(regex, new
        ReplaceEvaluator(replaceTabelarTagsCase), false);

    }
    private ReplaceAction replaceTabelarTagsCase(object sender,
    ReplaceEvaluatorArgs e)
    {
        ReplaceAction ra = ReplaceAction.Replace;

        e.Replacement = "Test replace by ASPOSE";
        Table table = (Table)
        e.MatchNode.ParentNode.ParentNode.ParentNode.ParentNode;

        Row asposeRow = new Row(asposeDocument);

        Cell asposeCell0 = new Cell(asposeDocument);

        asposeCell0.CellFormat.Width = 150;

        asposeRow.Cells.Add(asposeCell0);

        Cell asposeCell1 = new Cell(asposeDocument);

        asposeCell1.CellFormat.Width = 150;

        asposeRow.Cells.Add(asposeCell1);

        table.Rows.Add(asposeRow);

        return ra;
    }
  1. I also try to copy whole table which contains given tag. The result
    document contains only source table
private ReplaceAction replaceTabelarTagsCase(object sender, ReplaceEvaluatorArgs e)
{
    ReplaceAction ra = ReplaceAction.Replace;

    e.Replacement = "Test replace by ASPOSE";
    Table table = (Table)
    e.MatchNode.ParentNode.ParentNode.ParentNode.ParentNode;

    document.FirstSection.Body.AppendChild(table);
}
  1. Can I copy a whole cell format? I try to copy table with DocumentBuilder methods: StartTable(), InsertCell(), EndRow(), EndTable() but the created table has different styles (cell width, row height, borders and cell text format). I cannot do this by:
documentBuilder.InsertCell(); 
documentBuilder.CellFormat = sourceTableCell.CellFormat; 

because builder.CellFormat is read only.

I set some properties by:

documentBuilder.CellFormat.Width = sourceTableCell.CellFormat.Width; 
documentBuilder.CellFormat.Borders.LineWidth = sourceTableCell.CellFormat.Borders.LineWidth; 
documentBuilder.CellFormat.Borders.LineStyle = sourceTableCell.CellFormat.Borders.LineStyle; 

but I can’t examine if I copy all source parameters.

  1. I try to change our tags (#TAG_NAME#) with merge fields and use
    MailMerge to replace tags with value, but if I use:
documentBuilder.InsertField("MERGEFIELD " + tagName + " \\* MERGEFORMAT", ""); 

the merge field is not shown in result Word document. I only insert the
merge field and not use a MailMerge() method.
Should the merge field be displayed in result document? I don’t know if
the merge field is added to Word document but invisible or the merge
field is not added.

This message was posted using Email2Forum by Merit.

Hi
Thanks for your interest in Aspose.Words. I think that you can achieve this using aspose.Words.

  1. You can add new row to existing table. I think that you should clone last row and add this row to table. For example you can use the following code.
// Open document
Document doc = new Document(@"Test101\in.doc");
// Get table from document
Table myTable = doc.FirstSection.Body.Tables[0];
// Add new row
myTable.Rows.Add(myTable.LastRow.Clone(true));
// Save document
doc.Save(@"Test101\out.doc");
  1. Here you should use Clone function too. See the following code.
// Open document
Document doc = new Document(@"Test101\in.doc");
// Get table from document
Table myTable = doc.FirstSection.Body.Tables[0];
// Copy table
doc.FirstSection.Body.AppendChild(myTable.Clone(true));
// Save document
doc.Save(@"Test101\out.doc");
  1. Just clone the source cell.
  2. This occurs because you didn’t specify field value. Try using the following code.
builder.InsertField("MERGEFIELD myField", "«myField»");

Please ask if you would like to know anything else. I will be happy to help you.
Best regards.

Hi
Thanks for your reply - was very useful.

Could you clarify points 2 & 3 for me?

  1. Can I insert the ‘cloned’ table at any other place than the end of document? I want to insert table directly above or below the source table.

  2. Could you give me a code example? I create the table with documentBuilder.StartTable() and documentBuilder.EndTable(). Which method could I use to insert ‘cloned’ row or cell?

Best Regards
Krzysztof Araszkiewicz
Software Developer

Chrome CRM, Gdynia

Hi
Thanks for your request.
2. Here is code example that shows how you can insert the cloned table at any location in the document.

Document doc = new Document(@"Test033\in.doc");
// Get table from document
Table myTable = doc.FirstSection.Body.Tables[0];
// Insert cloned table directly below the source table
myTable.ParentNode.InsertAfter(myTable.Clone(true), myTable);
// Insert cloned table directly above the source table
myTable.ParentNode.InsertBefore(myTable.Clone(true), myTable);
// Also you can use DocumentBuilde.MoveTo___ methods to move cursor to any location in the document
// For example the following snippet insert cloned table at the begining of document
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();
builder.CurrentParagraph.ParentNode.InsertBefore(myTable.Clone(true), builder.CurrentParagraph);
// Save output document
doc.Save(@"Test033\out.doc");
  1. Please see the following code snippet.
// Start new table
Table myNewTable = builder.StartTable();
// Insert cloned row into the table
myNewTable.Rows.Add(myTable.Rows[0].Clone(true));
// End table
builder.EndTable();

I hope this could help you.
Best regards.

Hi

I suppose that I have all needed information about Aspose.Words to improve my work.

Thank you for your answers.

Best Regards
Krzysztof Araszkiewicz
Software Developer

Chrome CRM, Gdynia