PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Alternating shading of row migraDoc page break
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3700
Page 1 of 1

Author:  OVS [ Thu Nov 30, 2017 1:46 pm ]
Post subject:  Alternating shading of row migraDoc page break

Hi,

I have the same issue as described here:

viewtopic.php?f=2&t=2970&view=next

As I wanted to adapt the solution to my code I noticed that the KeepWithNext does not exist.
Can someone explain how to use the KeepWithNext which is mentioned in the link?



For explanation:

I will get a list<sting> from an other class.
Every 10th entry a new row will be written.

There is a header row which should be repeated when a page break occurs. So the HeadingFormat = true. This is not shown in the code below.


Code:
            for (int i = 0; i < (Input.Count()-28)/10 ; i++)
            {
                DRow = DataTable.AddRow();
                DRow.Borders.Top.Visible = false;
                DRow.Borders.Bottom.Visible = true;
                DRow.Borders.Left.Visible = false;
                DRow.Borders.Right.Visible = false;
                DRow.Borders.Color = Colors.DarkGray;
                DRow.Height = "0.75cm";

                cell = DRow.Cells[0];
                int Iconsize = 18;
                imgIcon.Height = (Iconsize * 0.75).ToString() + "pt";

                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[1];
                cell.AddParagraph(Input[29+(i*10)]);
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[2];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[3];
                cell.AddParagraph(Input[30+(i*10)]);
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[4];
                ...
                ...
                ...

                if (HighlightRowFlag == true)
                {
                    DRow.Shading.Color = Colors.LightGray;
                }
                HighlightRowFlag = !HighlightRowFlag;
            }



The code is working perfectly fine except for the alternating shading of rows.

Author:  Thomas Hoevel [ Thu Nov 30, 2017 2:07 pm ]
Post subject:  Re: Alternating shading of row migraDoc page break

Hi!
OVS wrote:
As I wanted to adapt the solution to my code I noticed that the KeepWithNext does not exist.
Can someone explain how to use the KeepWithNext which is mentioned in the link?
The property is KeepWith, not KeepWithNext.

OVS wrote:
The code is working perfectly fine except for the alternating shading of rows.
Seeing just a code snippet, no MDDDL, no PDF, I cannot say where the problem is.
Shading works when used properly.

Author:  OVS [ Thu Nov 30, 2017 2:52 pm ]
Post subject:  Re: Alternating shading of row migraDoc page break

I assumed that it is KeepWith, but when I set it to 1 the first row will be repeated after the pagebreak and the shading will be incorrect too.

Eg:

Columheader Columname
1st row: Data1
2nd row: Data2
3rd row: Data3
.
.
.
pagebreak
Columheader Columname
1st row: Data1
2nd row: Data n
3rd row: Data n+1

Code:
            Document document = CreateDocument(Input);
            MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");

            MigraDoc.Rendering.PdfDocumentRenderer renderer = new MigraDoc.Rendering.PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            renderer.Document = document;

            renderer.RenderDocument();

            var pdfSharpDoc = renderer.PdfDocument;


            for (int pageCounter = 0; pageCounter < pdfSharpDoc.PageCount; pageCounter++)
            {
                pdfSharpDoc.Outlines.Add("Page " + (pageCounter +1) ,pdfSharpDoc.Pages[pageCounter]);
            }

            pdfSharpDoc.Save(filename);


Edit: If I set my DRow.HeadingFormat = false. I don't get the Data1 repeated and each page will start with an even number and there for has the correct shading. However the header is missing.

Author:  Thomas Hoevel [ Thu Nov 30, 2017 4:09 pm ]
Post subject:  Re: Alternating shading of row migraDoc page break

OVS wrote:
I assumed that it is KeepWith, but when I set it to 1 the first row will be repeated after the pagebreak and the shading will be incorrect too.
Do not set KeepWith for the header row. Do not set it for all rows. Set it for odd rows only to make sure you get an even count of rows on each page.
You can use "KeepWith=5" to keep blocks of 6 rows together. Or "KeepWith=9" to have blocks of 10 rows.

Author:  OVS [ Mon Dec 04, 2017 7:23 am ]
Post subject:  Re: Alternating shading of row migraDoc page break

I tried it over the weekend but with no success, either it does nothing or it throws me an exception saying that it has no instance assigned to it.

My complete code:

Code:
            Table DataTable = new Table();

            DataTable.Borders.Left.Visible = false;
            DataTable.Borders.Right.Visible = false;
            DataTable.Format.Font.Name = "Arial";
            DataTable.Format.Font.Size = 8;

            float IconColumnWidth;

            Column DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_1_Column;

            IconColumnWidth = DCol.Width;

            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_2_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_3_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_4_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_5_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_6_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_7_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_8_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_9_Column;
            DCol.Format.Font.Bold = true;

            DCol = DataTable.AddColumn();
            DCol.Width = sectionWidth * Factor_10_Column;
            DCol.Format.Font.Bold = true;

            Row DRow = DataTable.AddRow();


            cell = DRow.Cells[0];
            cell.AddParagraph("1Col");
            cell.MergeRight = 1;


            cell = DRow.Cells[2];
            cell.AddParagraph("2Col");

            cell = DRow.Cells[3];
            cell.AddParagraph("3Col");

            cell = DRow.Cells[4];
            cell.AddParagraph("4Col");

            cell = DRow.Cells[5];
            cell.AddParagraph("5Col");

            cell = DRow.Cells[6];
            cell.AddParagraph("6Col");

            cell = DRow.Cells[7];
            cell.AddParagraph("7Col");

            cell = DRow.Cells[8];
            cell.AddParagraph("8Col");
            cell.MergeRight = 1;

            //cell = DRow.Cells[9];
            //cell.AddParagraph("9Col");


            DRow.Shading.Color = Colors.LightGray;
            DRow.Borders.Left.Visible = false;
            DRow.Borders.Right.Visible = false;
            DRow.Borders.Width = 1;
            DRow.Borders.Color = Colors.Black;

            //xxx
            DRow.HeadingFormat = true;
            bool HighlightRowFlag = false;


            #region Fill table with entries

            for (int i = 0; i < (Input.Count()-28)/10 ; i++)
            {

                DRow = DataTable.AddRow();
                DRow.Borders.Top.Visible = false;
                DRow.Borders.Bottom.Visible = true;
                DRow.Borders.Left.Visible = false;
                DRow.Borders.Right.Visible = false;
                DRow.Borders.Color = Colors.DarkGray;
                DRow.Height = "0.75cm";

                cell = DRow.Cells[0];
                //MigraDoc.DocumentObjectModel.Shapes.Image imgLogo = new MigraDoc.DocumentObjectModel.Shapes.Image(@PicturePath);
                //img.Width = sectionWidth * 0.037;
                int Iconsize = 18;
                imgIcon.Height = (Iconsize * 0.75).ToString() + "pt";

                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[1];
                //cell.Add(imgIcon.Clone());
                cell.AddParagraph(Input[29+(i*10)]);
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell.Format.Font.Bold = false;

                cell = DRow.Cells[2];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[3];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[4];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[5];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[6];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[7];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[8];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;

                cell = DRow.Cells[9];
                cell.Add(imgIcon.Clone());
                cell.VerticalAlignment = VerticalAlignment.Center;


                if (HighlightRowFlag == true)
                {
                    DRow.Shading.Color = Colors.LightGray;
                    DRow.KeepWith = 1;

                }
                else
                {
                    DRow.KeepWith = 0;
                }

                HighlightRowFlag = !HighlightRowFlag;



If I change the last part of the code to
Code:
                if (HighlightRowFlag == true)
                {
                    DRow.Shading.Color = Colors.LightGray;
                    //DRow.KeepWith = 1;

                }
                else
                {
                    DRow.KeepWith = 1;
                }


I'll get a null reference. 'Object not set to an instance of an object.'

Author:  Thomas Hoevel [ Mon Dec 04, 2017 9:15 am ]
Post subject:  Re: Alternating shading of row migraDoc page break

OVS wrote:
My complete code:
You call it "complete code", but it's not a complete VS solution I can use to start right away.

Do not set "KeepWith = 1" for the last row of the table. Only use this when you know there will be a following row.

Exceptions come with a stack trace that indicates where things went wrong.

Author:  OVS [ Mon Dec 04, 2017 10:17 am ]
Post subject:  Re: Alternating shading of row migraDoc page break

Thomas Hoevel wrote:
OVS wrote:
Do not set "KeepWith = 1" for the last row of the table. Only use this when you know there will be a following row.


This was the hint I needed. It is working fine now.

Thank you very much!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/