Creates a new list by copying the specified list and adding it to the collection of lists in the document.
[Visual Basic]Public Function AddCopy( _
ByVal
srcList As
List _
) As
List Parameters
- srcList
- The source list to copy from.
Return Value
The newly created list.
Remarks
The source list can be from any document. If the source list belongs to a different document, a copy of the list is created and added to the current document.
If the source list is a reference to or a definition of a list style, the newly created list is not related to the original list style.
Example
Enumerates through all lists defined in one document and creates a sample of those lists in another document.
[C#]
public void PrintOutAllLists()
{
// You can use any of your documents to try this little program out.
Document srcDoc = new Document(MyDir + "Lists.PrintOutAllLists.doc");
// This will be the sample document we product.
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
foreach (List srcList in srcDoc.Lists)
{
// This copies the list formatting from the source into the destination document.
List dstList = dstDoc.Lists.AddCopy(srcList);
AddListSample(builder, dstList);
}
dstDoc.Save(MyDir + "Lists.PrintOutAllLists Out.doc");
}
private static void AddListSample(DocumentBuilder builder, List list)
{
builder.Writeln("Sample formatting of list with ListId:" + list.ListId);
builder.ListFormat.List = list;
for (int i = 0; i < list.ListLevels.Count; i++)
{
builder.ListFormat.ListLevelNumber = i;
builder.Writeln("Level " + i);
}
builder.ListFormat.RemoveNumbers();
builder.Writeln();
}[Visual Basic]
Public Sub PrintOutAllLists()
' You can use any of your documents to try this little program out.
Dim srcDoc As Document = New Document(MyDir & "Lists.PrintOutAllLists.doc")
' This will be the sample document we product.
Dim dstDoc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(dstDoc)
For Each srcList As List In srcDoc.Lists
' This copies the list formatting from the source into the destination document.
Dim dstList As List = dstDoc.Lists.AddCopy(srcList)
AddListSample(builder, dstList)
Next srcList
dstDoc.Save(MyDir & "Lists.PrintOutAllLists Out.doc")
End Sub
Private Shared Sub AddListSample(ByVal builder As DocumentBuilder, ByVal list As List)
builder.Writeln("Sample formatting of list with ListId:" & list.ListId)
builder.ListFormat.List = list
Dim i As Integer = 0
Do While i < list.ListLevels.Count
builder.ListFormat.ListLevelNumber = i
builder.Writeln("Level " & i)
i += 1
Loop
builder.ListFormat.RemoveNumbers()
builder.Writeln()
End SubShows how to restart numbering in a list by copying a list.
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a list based on a template.
List list1 = doc.Lists.Add(ListTemplate.NumberArabicParenthesis);
// Modify the formatting of the list.
list1.ListLevels[0].Font.Color = Color.Red;
list1.ListLevels[0].Alignment = ListLevelAlignment.Right;
builder.Writeln("List 1 starts below:");
// Use the first list in the document for a while.
builder.ListFormat.List = list1;
builder.Writeln("Item 1");
builder.Writeln("Item 2");
builder.ListFormat.RemoveNumbers();
// Now I want to reuse the first list, but need to restart numbering.
// This should be done by creating a copy of the original list formatting.
List list2 = doc.Lists.AddCopy(list1);
// We can modify the new list in any way. Including setting new start number.
list2.ListLevels[0].StartAt = 10;
// Use the second list in the document.
builder.Writeln("List 2 starts below:");
builder.ListFormat.List = list2;
builder.Writeln("Item 1");
builder.Writeln("Item 2");
builder.ListFormat.RemoveNumbers();
builder.Document.Save(MyDir + "Lists.RestartNumberingUsingListCopy Out.doc");[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
' Create a list based on a template.
Dim list1 As List = doc.Lists.Add(ListTemplate.NumberArabicParenthesis)
' Modify the formatting of the list.
list1.ListLevels(0).Font.Color = Color.Red
list1.ListLevels(0).Alignment = ListLevelAlignment.Right
builder.Writeln("List 1 starts below:")
' Use the first list in the document for a while.
builder.ListFormat.List = list1
builder.Writeln("Item 1")
builder.Writeln("Item 2")
builder.ListFormat.RemoveNumbers()
' Now I want to reuse the first list, but need to restart numbering.
' This should be done by creating a copy of the original list formatting.
Dim list2 As List = doc.Lists.AddCopy(list1)
' We can modify the new list in any way. Including setting new start number.
list2.ListLevels(0).StartAt = 10
' Use the second list in the document.
builder.Writeln("List 2 starts below:")
builder.ListFormat.List = list2
builder.Writeln("Item 1")
builder.Writeln("Item 2")
builder.ListFormat.RemoveNumbers()
builder.Document.Save(MyDir & "Lists.RestartNumberingUsingListCopy Out.doc")See Also
ListCollection Class | Aspose.Words.Lists Namespace