Creating/formatting a .docx document

Majkl

Member
Joined
Dec 8, 2023
Messages
5
Location
Czech Republic
Programming Experience
3-5
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.
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);
    }
}
 

Attachments

  • konec.png
    konec.png
    13.1 KB · Views: 15
What happens if you insert the page break after the table instead of before every table? (e.g move lines 4-5 after line 57)

This isn't really a C# or .NET Framework question but rather a question about Word and how it does its formatting.
 
What happens if you insert the page break after the table instead of before every table? (e.g move lines 4-5 after line 57)

This isn't really a C# or .NET Framework question but rather a question about Word and how it does its formatting.

I tried that too and it creates a file with several thousand clean pages.
Thanks for the heads up and moving the thread.
 
Solved: this statement " body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));" I put it on line 68 and it already displayed the sections as I needed, it's strange that I tried it for almost all positions and missed this one :)
Now I would still need, if the table is on two or three pages, so that the name of the section with which it started is on each page.
Thanks for your advice
 

Latest posts

Back
Top Bottom