Majkl
Member
Hello experts,
I need help creating a document, I've spent a few days on this and still can't find a solution.
I have certain values exported to a .txt file, which I then load the data into the "DataGridView" in WF, where I edit this data and then insert it into the created .docx template.
I have a problem with the formatting and that is that I have the name of the section and I will create a table under it where there are values and I want the name of the section and the associated table to start on each page, but I get inserted "----end of page-- -" to the next page and it is completely empty, and the section and its data continue until the next page.
You don't know how I should treat it so that "-----end of page----" ends on this page? When I have 36 rows in the table, it is in order, but when there are 37, the table is on one page, but the next one is already empty and continues to the third page.
(I apologize for my English)
Thanks for your advice.
I need help creating a document, I've spent a few days on this and still can't find a solution.
I have certain values exported to a .txt file, which I then load the data into the "DataGridView" in WF, where I edit this data and then insert it into the created .docx template.
I have a problem with the formatting and that is that I have the name of the section and I will create a table under it where there are values and I want the name of the section and the associated table to start on each page, but I get inserted "----end of page-- -" to the next page and it is completely empty, and the section and its data continue until the next page.
You don't know how I should treat it so that "-----end of page----" ends on this page? When I have 36 rows in the table, it is in order, but when there are 37, the table is on one page, but the next one is already empty and continues to the third page.
(I apologize for my English)
Thanks for your advice.
C#:
foreach (var sectionName in uniqueSectionNames)
{
// Adding a new page
body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));
// Add section title + make it bold + remove the space after the title paragraph, I put 10 and there is a smaller space
body.Append(new Paragraph(new ParagraphProperties(new SpacingBetweenLines() { After = "10", Line = "240", LineRule = LineSpacingRuleValues.Auto } ),
new Run(new RunProperties(new Bold()),new Text($"Úsek: {sectionName}"))));
// Create a new table
var table = new Table();
// Setting table margins
var tableProperties = new TableProperties(
new TableBorders(
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 10 },
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 10},
new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 10 }
),
new TableWidth { Type = TableWidthUnitValues.Pct, Width = "85%" } // Nastavení šířky tabulky na 85%
);
table.AppendChild(tableProperties);
// Add data from the DataGridView to the table
foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
if (!dataGridViewRow.IsNewRow && !IsRowEmpty(dataGridViewRow))
{
var currentSectionName = dataGridViewRow.Cells[0].Value?.ToString() ?? string.Empty;
// If the section name matches the current name, we add a row to the table
if (currentSectionName == sectionName)
{
var row = new TableRow();
for (int i = 1; i < dataGridViewRow.Cells.Count; i++)
{
var cellData = dataGridViewRow.Cells[i].Value?.ToString() ?? string.Empty;
var cell = new TableCell(new Paragraph(new Run(new Text(cellData))));
var paragraphProperties = cell.GetFirstChild<Paragraph>().GetFirstChild<ParagraphProperties>();
if (paragraphProperties == null)
{
paragraphProperties = new ParagraphProperties();
cell.GetFirstChild<Paragraph>().InsertBefore(paragraphProperties, cell.GetFirstChild<Paragraph>().First());
}
var spacingBetweenLines = paragraphProperties.GetFirstChild<SpacingBetweenLines>();
if (spacingBetweenLines == null)
{
spacingBetweenLines = new SpacingBetweenLines() { After = "25", Line = "240", LineRule = LineSpacingRuleValues.Auto };
paragraphProperties.Append(spacingBetweenLines);
}
else
{
spacingBetweenLines.After = "25";
}
row.Append(cell);
}
table.Append(row);
}
}
}
// Adding a table to the document body
body.Append(table);
}
}