Cloning is one of the common word that is being used not only in real world but in the world of programming too. Cloning means to generate an identical copy or replica of something. In programming, cloning means to create an identical copy of an instance.
Cloning in Aspose.Pdf
Aspose.Pdf allows developers to make clones of Table objects but in a controlled manner. Aspose.Pdf offers two different methods in Table class for cloning purposes. These two methods are Clone and CompleteClone . The Clone and CompleteClone methods in Table class allow you to copy a table.
The Clone method clones a new Table from original Table but only table format information (that is table structure) is copied.
Using CompleteClone method, both format information (that is table structure) and all data (rows) in the Table are copied.
Note: The Row and Cell classes also have Clone and CompleteClone methods. That means, we cannot only clone a Table but its parts (rows and cells) too.
The following example shows how to read a Table from XML template, clone it and then add data into the new table.
Example:
[RepeatExample.xml]
<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section>
<Table ID="table1" ColumnWidths="50 50 50">
<DefaultCellBorder>
<All LineWidth="0.1"></All>
</DefaultCellBorder>
<Row>
<Cell>
<Text>
<Segment>col1</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>col2</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>col3</Segment>
</Text>
</Cell>
</Row>
<Row>
<Cell>
<Text>
<Segment>item1</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>item2</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>item3</Segment>
</Text>
</Cell>
</Row>
</Table>
</Section>
</Pdf>
[C#]
//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//Call BindXML method to read information from RepeatExample.xml document. Second
//parameter is for Xsl file. In our case, we don't need it so leave it as null.
pdf1.BindXML("RepeatExample.xml",null);
//Obtain the first section from the Pdf
Section sec1 = pdf1.Sections[0];
//Obtain a table named "table1" from the Pdf
Table table1 = sec1.Paragraphs["table1"] as Table;
//Declare a table instance
Table curTable;
//Create an array of string containing three values, "item1", "item2" and "item3"
string[] items = new string[]{"item1","item2","item3"};
//Run a for loop for 5 times
for(int i = 0; i < 5 ; i++)
{
//Copy the table structure of "table1" by calling its Clone method
curTable = table1.Clone() as Table;
//Set the top margin of the cloned table to 10
curTable.Margin.Top = 10;
//Copy a row (with structure and data) of "table1" by calling its
//CompleteClone method. And then add that row to the cloned table
curTable.Rows.Add(table1.Rows[0].CompleteClone() as Row);
//Import an array of string "items" into the table
curTable.ImportArray(items,1,0,false);
//Add the cloned table to the paragraphs collection of the section
sec1.Paragraphs.Add(curTable);
}
//Save the Pdf as example.pdf document
pdf1.Save("example.pdf");
[VB.NET]
'Instantiate a Pdf object by calling its empty constructor
Dim pdf1 As Pdf = New Pdf()
'Call BindXML method to read information from RepeatExample.xml document. Second
'parameter is for Xsl file. In our case, we don't need it so leave it as null.
pdf1.BindXML("RepeatExample.xml", Nothing)
'Obtain the first section from the Pdf
Dim sec1 As Section = pdf1.Sections(0)
'Obtain a table named "table1" from the Pdf
Dim table1 As Table = sec1.Paragraphs("table1")
'Declare a table instance
Dim curTable As Table
'Create an array of string containing three values, "item1", "item2" and "item3"
Dim items() As String = New String() {"item1", "item2", "item3"}
'Declare an Integer variable
Dim i As Integer
'Run a for loop for 5 times
For i = 0 To 5 - 1 Step 1
'Copy the table structure of "table1" by calling its Clone method
curTable = table1.Clone()
'Set the top margin of the cloned table to 10
curTable.Margin.Top = 10
'Copy a row (with structure and data) of "table1" by calling its
'CompleteClone method. And then add that row to the cloned table
curTable.Rows.Add(table1.Rows(0).CompleteClone())
'Import an array of string "items" into the table
curTable.ImportArray(items, 1, CType(0, Byte), False)
'Add the cloned table to the paragraphs collection of the section
sec1.Paragraphs.Add(curTable)
Next
'Save the Pdf as example.pdf document
pdf1.Save("example.pdf")