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

Keep paragraph and table together
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2756
Page 1 of 1

Author:  evb [ Tue Mar 04, 2014 10:48 am ]
Post subject:  Keep paragraph and table together

Hi,

This question is asked already several times in this forum, but for most of them there is no answer or the proposed answers don't work or not in my case.
(viewtopic.php?f=2&t=1073, viewtopic.php?f=2&t=265&start=0 (at the end), viewtopic.php?f=2&t=518 (also at the end))

I'm using Migradoc version 1.32.4334.0, normally the latest version from NuGet.

I have a table with a title. This title is not the header of the table!
The title is putted in a paragraph and the table is inserted after the paragraph.

When page breaking occurs, I want to keep the title and the table together. If the table is long, it can break itself over several pages. That is not the problem, the table headers are repeated each page.
But the first time the table is printed, I want to keep the title and table together to avoid that the title of de table is on page X and the table itself is on page X+1.

I tried the options KeepTogether and KeepWithNext of the paragraph, but they only work if another paragraph is following, not a table.

So, what are my options here?

Attachments:
File comment: Example of title and table not on one page
migradoc title and table not on one page.png
migradoc title and table not on one page.png [ 30.86 KiB | Viewed 11951 times ]

Author:  Thomas Hoevel [ Tue Mar 04, 2014 10:59 am ]
Post subject:  Re: Keep paragraph and table together

Hi!
evb wrote:
I tried the options KeepTogether and KeepWithNext of the paragraph, but they only work if another paragraph is following, not a table.
That's the problem: it doesn't work currently as I think it should.

evb wrote:
So, what are my options here?
The simple solution: add a page break before the header.

Or use a hack: add the header, call "docRenderer.PrepareDocument();" and store the page count. Add the table with e.g. 3 or 4 rows and call "docRenderer.PrepareDocument();" again. If the page count has changed, add page break before the header. Finally add the remaining table rows.

Author:  evb [ Tue Mar 04, 2014 1:25 pm ]
Post subject:  Re: Keep paragraph and table together

How to add a pagebreak before the title after checking the pagecount in the table?

Code:
            // if I add here a pagebreak then it works, but this a hardcoded always set addpagebreak
            // sec.AddPageBreak();
            Paragraph para = sec.AddParagraph();
            para.AddFormattedText(CoreResources.Strings.ListGlobalCriteria, TextFormat.Bold | TextFormat.Italic).Style = "Label";
            para.Format.SpaceAfter = 2;

            Table tableCriteria = sec.AddTable();
            tableCriteria.AddColumn(50);
            tableCriteria.AddColumn(50);
            Row rowCriteria = tableCriteria.AddRow();
            rowCriteria.HeadingFormat = true;
            ...
            // hack to keep together paragraph and table
            DocumentRenderer docRenderer = new DocumentRenderer(document);
            docRenderer.PrepareDocument();
            int pageCountAfterHeader = docRenderer.FormattedDocument.PageCount;


            rowCriteria = tableCriteria.AddRow();
            ....
            rowCriteria = tableCriteria.AddRow();
            ....
            rowCriteria = tableCriteria.AddRow();
            ....
           
            docRenderer.PrepareDocument();
            int pageCountAfterRow = docRenderer.FormattedDocument.PageCount;
            if (pageCountAfterRow > pageCountAfterHeader)
                        sec.AddPageBreak();   ???? but how can I put here a page break before my paragraph title?

Author:  Thomas Hoevel [ Tue Mar 04, 2014 3:14 pm ]
Post subject:  Re: Keep paragraph and table together

Untested code (there may be typos, but I hope you get the idea):
Code:
int idx = section.Elements.IndexOf(para);
section.Elements.InsertObject(idx, new PageBreak());

Author:  evb [ Wed Mar 05, 2014 7:43 am ]
Post subject:  Re: Keep paragraph and table together

Ok, it is working.

Code:
int idx = sec.Elements.IndexOf(para);
sec.Elements.InsertObject(idx, new PageBreak());


Thanks

Author:  Thomas Hoevel [ Wed Mar 05, 2014 12:05 pm ]
Post subject:  Re: Keep paragraph and table together

Sorry, I forgot Elements. :oops:
Glad to see you figured it out.

Author:  D_F_G [ Tue Jan 13, 2015 3:36 pm ]
Post subject:  Re: Keep paragraph and table together

Hello,

I am having the same problem.
Is there a bugfix for the problem by now? So is there an easier (or less hacky) way to keep a paragraph and a table together?

Author:  Thomas Hoevel [ Tue Jan 13, 2015 3:44 pm ]
Post subject:  Re: Keep paragraph and table together

The bug was fixed with MigraDoc 1.50 beta.
See also:
viewtopic.php?f=1&t=3017

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