When using ExecuteWithRegions can you hide TableStart/TableEnd Tags when there are no rows in the Table?

Last post 01-19-2011, 6:44 AM by alexey.noskov. 14 replies.
Sort Posts: Previous Next
  •  11-18-2005, 12:08 AM 35952

    When using ExecuteWithRegions can you hide TableStart/TableEnd Tags when there are no rows in the Table?

    We are using ExecutewithRegions with several tables.  One of the Tables has no rows and when the Document is produced it displays the TableStart/TableEnd tags when I would like them to be hidden.

    How can I hide TableStart and TableEnd Tags when there are no rows in the DataTable?

    Thank you.

     
  •  11-18-2005, 2:55 AM 35961 in reply to 35952

    Re: When using ExecuteWithRegions can you hide TableStart/TableEnd Tags when there are no rows in the Table?

    I've created a document that contains this:

    «TableStart:MyTable»

    «MyField»

    «TableEnd:MyTable»

    And executed this code:

    [Test]

    public void TestEmptyTable()

    {

    Document doc = TestUtil.Open(@"MailMerge\TestEmptyTable.doc");

    DataTable table = new DataTable("MyTable");

    table.Columns.Add("MyField");

    doc.MailMerge.ExecuteWithRegions(table);

    TestUtil.Save(doc, @"MailMerge\TestEmptyTable Out.doc");

    Assert.AreEqual("\x000c", doc.GetText());

    }

    Basically MailMerge deletes TableStart and TableEnd tags and everything in between them if the table does not have any rows and everyting is used properly.

     


    Roman Korchagin
    Aspose.Words Team Leader
    Aspose Auckland Team
     
  •  11-18-2005, 11:36 PM 36039 in reply to 35961

    Re: When using ExecuteWithRegions can you hide TableStart/TableEnd Tags when there are no rows in the Table?

    Our situation is a little more involved that your test:

    We use a DataSet, not a DataTable for the merge because our document has both regions that grow and ones that do not.

    Our DataSet contains two tables -dtMain and dtRegion. dtRegion handles the GrowRegions of the document.

    So in our document we have

    «TableStart:Main» «MyMainMergeFields» «TableEnd:Main»

    «TableStart:Region» «MyRegionMergeFields» «TableEnd:Region»

    dtMain will always have one row. dtRegion may not contain any rows.

    We also save the document to a memory stream.

    So our code goes something like:

    doc.MailMerge.ExecuteWithRegions(DataSetWith2Tables);

    MemoryStream stream = new MemoryStream();

    doc.Save(stream, SaveFormat.FormatDocument);

    If the growregion table, dtRegion of our dataset contains no rows, the merge fails  at doc.Save(stream, SaveFormat.FormatDocument) with the common "Object reference not set to an instance of an object" error.

    I have tried merging with only one datatable from our dataset - the dtMain using:

    doc.MailMerge.ExecuteWithRegions(dtMain) and I also tried doc.MailMerge.Execute(dtMain)

    In either case, the merge executes but still leaves behind the TableStart and TableEnd tags.

    I want to try and hide or delete the tags myself but cannot seem to find the syntax to do that.

    I have tried DocumentBuilder.MoveToMergeField(FieldName) and DocumentBuilder does not recognize TableStart as valid mergefields.

    Can you show me how to delete or hide these tags?

    Thanks

     
  •  11-22-2005, 12:17 AM 36104 in reply to 36039

    Re: When using ExecuteWithRegions can you hide TableStart/TableEnd Tags when there are no rows in the Table?

    Please attach your document because it all works fine for me.

    If you use MailMerge.Execute then you don't need TableStart and TableEnd merge fields in the document. In fact this form of mail merge does not use merge regions and does not touch those fields. Maybe you are getting these field left out because you use this normal mail merge.

    It does not matter how many tables you have in your data set. As long as your Table.Name matches the name of the table in TableStart and TableEnd fields, then there will be a match between the merge region and table found and the mail merge will execute and remove the fields from the final document.

    I've modified my document:

    «TableStart:TableA»«FieldA»«TableEnd:TableA»

    «TableStart:TableB»«FieldB»«TableEnd:TableB»

    I've modified my code:

    [Test]

    public void TestEmptyTable()

    {

    Document doc = TestUtil.Open(@"MailMerge\TestEmptyTable.doc");

    DataSet ds = new DataSet();

    DataTable tableA = new DataTable("TableA");

    tableA.Columns.Add("FieldA");

    tableA.Rows.Add(new object[] {"aaa"});

    ds.Tables.Add(tableA);

    DataTable tableB = new DataTable("TableB");

    tableB.Columns.Add("FieldB");

    ds.Tables.Add(tableB);

    doc.MailMerge.ExecuteWithRegions(ds);

    //Customer complained saving to memory stream throws. Does not throw for me.

    MemoryStream s = new MemoryStream();

    doc.Save(s, SaveFormat.FormatDocument);

    TestUtil.Save(doc, @"MailMerge\TestEmptyTable Out.doc");

    Assert.AreEqual("aaa\x000c", doc.GetText());

    }


    Roman Korchagin
    Aspose.Words Team Leader
    Aspose Auckland Team
     
  •  08-31-2010, 8:19 AM 256357 in reply to 36104

    TableStart/TableEnd Tags are not hidden when no rows in the Table .NET

    Hi all,

    I am facing up an issue right now similar to that one : http://www.aspose.com/community/forums/36104/when-using-executewithregions-can-you-hide-tablestart-tableend-tags-when-there-are-no-rows-in-the-ta/showthread.aspx#36104
     
    I have a word template document with fews MailRegions and for each of these MailRegions, nested Regions.

    My Aspose Version is 9.1.0.0 (File Version), 2010,5,14,0 (Product Version).

    Here is a part of my template

     

    «TableStart:MainParentField»

    MyTiTLE

    «TableStart:FirstChildLevel»

    «LibelleTypeProduit»

     

     

    Qty

    %

    Price

     

    «LibelleArticle»

    Qty

    %

    Price

    «TableStart:SecondChildLevel» 1. «UnitLabel»

    «Quantity»

    «Percent»

    «PriceHT»«TableEnd:SecondChildLevel»

    TOTAL HT

    (TOTAL TTC)

    «TotalHT»
    (
    «TotalTTC» )

     

    «TableEnd:FirstChildLevel»

    «TableEnd:MainParentField»


    I can have more than one SecondChildLevel (more than one FirstChildLevel too).

    Here is how I create a document from this template :

    Dim doc As Aspose.Words.Document = New Aspose.Words.Document(Doctemplate)
    doc.MailMerge.ExecuteWithRegions(dataSource)
    doc.Save(outputFileName, saveFormat) 'Format is WordDocument


    dataSource is a DataSet.

    Do You have any idea of how I can hide MainParentField when no data (rows) is returned ?

    I tried to use an empty datasource, but, it does not work.

    I tried also to check my dataset (getXml() methods), and I got that : <NewDataSet />

    I also tried that :
    <NewDataSet>
    <MainParentField>
      <FictiveFieldUseNoWhere />
    </MaintParentField>
    </NewDataSet>


    but not working too..

    Any idea ?

    Thanks

     
  •  08-31-2010, 8:35 AM 256361 in reply to 256357

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Hi

     

    Thanks for your request. I think in this case you can try using RemoveEmptyRegions class provided by Adam in this thread:

    http://www.aspose.com/community/forums/permalink/241691/241795/showthread.aspx#241795

     

    Hope this helps.

     

    Also we will consider adding an option like RemoveEmptyParagraphs, but for regions. Your request has been linked to the appropriate issue. You will be notified as soon as it is supported.

     

    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  08-31-2010, 8:51 AM 256363 in reply to 256361

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Hi Andrey!

    Thank you for your answer. When I tried to access your link, I got a denied access :(. I am logged in!
     
  •  08-31-2010, 8:57 AM 256366 in reply to 256363

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Where could i find this class ? Which namespace ?
     
  •  08-31-2010, 9:01 AM 256369 in reply to 256366

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    that's ok, I could find it!
     
  •  08-31-2010, 9:01 AM 256370 in reply to 256363

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Attachment: Present (inaccessible)

    Hi

     

    Sorry it is private thread. Please see the attached RemoveEmptyRegions class.

     

    // Open the template document

    Document doc = new Document(@"in.doc");

     

    // Merge data

    doc.MailMerge.ExecuteWithRegions(Dataset);

     

    // Remove any remaining unused regions from the document

    doc.Accept(new RemoveEmptyRegions());

     

    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  08-31-2010, 10:31 AM 256387 in reply to 256370

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table .NET

    Hi again Andrey,

    I am working using VB .NET.

    I tried to convert the class as below and it is not working :( (it seems running without stopping).

    Public Class RemoveEmptyRegion
            Inherits DocumentVisitor

            Private CurrentTable As Table

            Private StartCell As Cell
            Private EndCell As Cell
            Private ParentRow As Row

            Private Const StartRegion As String = "tablestart"
            Private Const EndRegion As String = "tableend"

            Private Depth As Integer = 0
            Private IsInsideTable As Boolean = False
            Private IsEndOfRegion As Boolean = False

            Public Overrides Function VisitFieldStart(ByVal fieldStart As Aspose.Words.Fields.FieldStart) As VisitorAction

                If (GetFieldCode(fieldStart).ToLower().Contains(StartRegion)) Then

                    Depth = Depth + 1
                    If (IsInsideTable) Then
                        StartCell = CType(fieldStart.ParentParagraph.ParentNode, Cell)
                        ParentRow = StartCell.ParentRow
                    End If

                    fieldStart.Remove()

                ElseIf (GetFieldCode(fieldStart).ToLower().Contains(EndRegion)) Then

                    IsEndOfRegion = True

                    If (IsInsideTable) Then
                        EndCell = CType(fieldStart.ParentParagraph.ParentNode, Cell)
                    End If

                    fieldStart.Remove()

                    Depth = Depth - 1
                    Else
                        If (Depth > 0) Then
                            fieldStart.Remove()
                        End If
                End If

                Return VisitorAction.Continue

            End Function

            Public Overrides Function VisitFieldSeparator(ByVal fieldSeparator As Aspose.Words.Fields.FieldSeparator) As VisitorAction

                If (Depth > 0 Or IsEndOfRegion) Then
                    fieldSeparator.Remove()
                End If
                Return VisitorAction.Continue

            End Function

            Public Overrides Function VisitFieldEnd(ByVal fieldEnd As Aspose.Words.Fields.FieldEnd) As VisitorAction

                If (Depth > 0 And IsInsideTable And Not IsEndOfRegion) Then

                    Dim CurrentCell As Cell = CType(fieldEnd.ParentParagraph.ParentNode, Cell)
                    fieldEnd.Remove()

                    Dim cells As ArrayList = IsColumnEmpty(ParentRow.ParentTable, ParentRow.Cells.IndexOf(CurrentCell))

                    If cells IsNot Nothing Then
                        For Each cell As Cell In cells
                            cell.Remove()
                        Next
                    End If

                ElseIf IsEndOfRegion Then

                    If (fieldEnd.PreviousPreOrder(fieldEnd.Document) IsNot Nothing) Then

                        Dim prevNode As Node = fieldEnd.PreviousPreOrder(fieldEnd.Document)

                        If (prevNode.NodeType = NodeType.Run) Then

                            Dim run As Run = CType(prevNode, Run)
                            Dim paragraphText As String = run.ParentParagraph.ToTxt().Trim()

                            If (String.IsNullOrEmpty(paragraphText)) Then
                                run.ParentParagraph.Remove()
                            End If
                        End If
                    End If

                    fieldEnd.Remove()

                    If (IsInsideTable And IsEndOfRegion) Then

                        Dim cells As ArrayList = IsColumnEmpty(ParentRow.ParentTable, ParentRow.Cells.IndexOf(StartCell))

                        If (cells IsNot Nothing) Then

                            For Each cell As Cell In cells
                                cell.Remove()
                            Next
                        End If

                        cells = IsColumnEmpty(ParentRow.ParentTable, ParentRow.Cells.IndexOf(EndCell))

                        If cells IsNot Nothing Then
                            For Each cell As Cell In cells

                                cell.Remove()
                            Next
                        End If

                        Dim isRowEmpty As Boolean = True

                        For Each cell As Cell In ParentRow.Cells
                            For Each p As Paragraph In cell.Paragraphs

                                If (p.HasChildNodes) Then
                                    isRowEmpty = False
                                End If
                            Next
                        Next

                        If isRowEmpty Then
                            ParentRow.Remove()
                        End If
                    End If
                    IsEndOfRegion = False

                ElseIf Depth > 0 Then
                    fieldEnd.Remove()
                End If

                Return VisitorAction.Continue

            End Function

            Public Overrides Function VisitRun(ByVal run As Run) As VisitorAction

                If (Depth > 0 Or IsEndOfRegion) Then
                    run.Remove()
                End If

                Return VisitorAction.Continue

            End Function

            Public Overrides Function VisitTableStart(ByVal table As Table) As VisitorAction

                If (Depth > 0) Then
                    table.Remove()
                Else
                    IsInsideTable = True
                    CurrentTable = table
                End If

                Return VisitorAction.Continue

            End Function

            Public Overrides Function VisitTableEnd(ByVal table As Table) As VisitorAction

                IsInsideTable = False
                Return VisitorAction.Continue

            End Function

            Public Overrides Function VisitParagraphEnd(ByVal paragraph As Paragraph) As VisitorAction

                If (Depth > 0 Or IsEndOfRegion) Then
                    If (Not paragraph.HasChildNodes) Then
                        paragraph.Remove()
                    End If
                End If

                Return VisitorAction.Continue

            End Function

            Private Shared Function IsColumnEmpty(ByVal table As Table, ByVal ColumnId As Integer) As ArrayList

                Dim isColumnEmptyValue As Boolean = True
                Dim nodes As ArrayList = New ArrayList()

                For Each row As Row In table
                    Dim CurrentCell As Cell = row.Cells(ColumnId)

                    If (CurrentCell IsNot Nothing) Then
                        nodes.Add(CurrentCell)
                        For Each p As Paragraph In CurrentCell.Paragraphs
                            If (p.HasChildNodes) Then
                                isColumnEmptyValue = False
                            End If
                        Next
                    End If
                Next

                If Not isColumnEmptyValue Then
                    Return Nothing
                Else
                    Return nodes
                End If

            End Function

            ''' <summary>
            ''' Not Sure For This Bloc ....
            ''' </summary>
            ''' <param name="fieldStart"></param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Private Shared Function GetFieldCode(ByVal fieldStart As Aspose.Words.Fields.FieldStart) As String

                Dim builder As StringBuilder = New StringBuilder()

                Dim node As Node = fieldStart

                While Not node.Equals(node.NextPreOrder(node.Document))
                    If node IsNot Nothing And node.NodeType <> NodeType.FieldSeparator And node.NodeType <> NodeType.FieldEnd Then
                        ' Use text only of Run nodes to avoid duplication.
                        If (node.NodeType = NodeType.Run) Then
                            builder.Append(node.GetText())
                        End If
                    End If
                End While

                'For node As Node = CType(fieldStart, Node) To node.NextPreOrder(node.Document)

                '    If node IsNot Nothing And node.NodeType <> NodeType.FieldSeparator And node.NodeType <> NodeType.FieldEnd Then
                '        ' Use text only of Run nodes to avoid duplication.
                '    If (node.NodeType == NodeType.Run) Then
                '            builder.Append(node.GetText())
                '        End If
                '    End If

                'Next

                Return builder.ToString()
            End Function


        End Class
     
  •  08-31-2010, 2:04 PM 256444 in reply to 256387

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Attachment: Present (inaccessible)

    Hi

     

    Thanks for your inquiry. Please see the attached file.

    If you need more assistance, I will be glad to help you.

     

    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-01-2010, 2:49 AM 256501 in reply to 256444

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Hi Andrey!

    I tried, and it works! Thanks You so much! (You have now both C# and Vb version).

    Is it possible to mark as resolved this issue ?

    David
     
  •  09-01-2010, 3:15 AM 256504 in reply to 256501

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    Hi David,

    We will mark this issue as resolved sometime in the future when we implement this behaviour automatically within the actual ExecuteWithRegions function. In the mean time it's great that this manual implementation is working well for you.

    If you have any further inquires, please feel free to ask.

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  01-19-2011, 6:44 AM 279568 in reply to 256504

    Re: TableStart/TableEnd Tags are not hidden when no rows in the Table

    The issues you have found earlier (filed as 17666) have been fixed in this update.     


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
View as RSS news feed in XML