jles
posted this
26 March 2018
Hi Rico,
Docentric Toolkit consists of two major parts/frameworks:
- Reporting (template based document generation)
- Document Object Model APIs
It seems like you are primarily using reporting. Note that you cannot directly mix reporting and DOM APIs. What you can do is you can use DOM APIs to create a sub document on the fly programmatically using DOM APIs and use it as a sub document in your main generating document. Below is a snippet of how to create a new document and create a template from scratch:
public static void CreateTable()
{
// Create a new Document.
Document document = Document.New();
// Table
Table table = new Table();
document.Body.Elements.Add(table); // Add to the section.
table.TableFormat.Borders = new TableBorders();
table.TableFormat.CellSpacing = 0;// Length.FromCentimeters(0.1);
table.Columns.AddRange
(
new TableColumn(Length.FromCentimeters(5)),
new TableColumn(Length.FromCentimeters(2)),
new TableColumn(Length.FromCentimeters(3))
);
// Row 1
{
TableRow tr1 = new TableRow();
table.Elements.Add(tr1);
// Cell 1
TableCell tc1 = new TableCell() { ColumnSpan = 2 };
tr1.Elements.Add(tc1);
tc1.Elements.AddRange
(
new Paragraph
(
new Run("This is Table Cell 1,1!!!")
)
);
// Cell 3
TableCell tc3 = new TableCell() { RowSpan = 2 };
tc3.TableCellFormat.Borders = new TableCellBorders() { Top = new Border(BorderStyle.Single, Colors.Yellow, Length.FromCentimeters(0.15)) };
tr1.Elements.Add(tc3);
tc3.Elements.AddRange
(
new Paragraph
(
new Run("This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Tablej Cellj 1,3"),
new Run("\n\nThis is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Table Cell 1,3 This is Tablej Cellj 1,3")
)
);
}
// Row 2
{
TableRow tr2 = new TableRow();
table.Elements.Add(tr2);
// Cell 1
TableCell tc1 = new TableCell() { RowSpan = 2 };
tc1.TableCellFormat.Borders = new TableCellBorders() { Top = new Border(BorderStyle.Single, Colors.LightBlue, Length.FromCentimeters(0.3)), Bottom = new Border(BorderStyle.Single, Colors.LightGreen, Length.FromCentimeters(0.5)) };
tr2.Elements.Add(tc1);
tc1.Elements.AddRange
(
new Paragraph
(
new Run("This is Table Cell 2,1")
)
);
// Cell 2
TableCell tc2 = new TableCell();
tr2.Elements.Add(tc2);
tc2.TableCellFormat.Borders = new TableCellBorders() { Left = new Border(BorderStyle.Single, Colors.Green, Length.FromCentimeters(0.2)) };
tc2.Elements.AddRange
(
new Paragraph
(
new Run("This is Table Cell 2,2")
)
);
}
// Row 3
{
TableRow tr3 = new TableRow();
table.Elements.Add(tr3);
// Cell 2
TableCell tc2 = new TableCell() { ColumnSpan = 2 };
tr3.Elements.Add(tc2);
tc2.Elements.AddRange
(
new Paragraph
(
new Run("This is Table Cell 3,2 This is Table Cell 3,2 This is Table Cell 3,2 This is Table Cell 3,2 This is Table Cell 3,2 This is Table Cell 3,2 This is Table Cellj 3,2")
)
);
}
// Row 4
{
TableRow tr4 = new TableRow();
table.Elements.Add(tr4);
// Cell 4,1
TableCell tc1 = new TableCell() { ColumnSpan = 3 };
tc1.TableCellFormat.Borders = new TableCellBorders() { Bottom = new Border(BorderStyle.Single, Colors.Goldenrod, Length.FromCentimeters(0.2)) };
tr4.Elements.Add(tc1);
tc1.Elements.AddRange
(
new Paragraph
(
new Run("This is Table Cell 4,1")
)
);
}
// Paragraph (there always needs to be at least an empty paragraph at the end of a document)
Paragraph p2 = new Paragraph
(
new Run("Table") { CharacterFormat = new CharacterFormat() { FontSize = 18, Foreground = Colors.Green, Bold = true } }
);
document.Body.Elements.Add(p2);
// Save the document.
document.Save(@"CreateTable.docx");
}
Let me know if this helped.
Best regards,
Jure Leskovec