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

KeepTogether not working in Paragraph?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2893
Page 1 of 1

Author:  briankitt [ Mon Aug 11, 2014 3:34 am ]
Post subject:  KeepTogether not working in Paragraph?

I have a single paragraph.
The text has '\n' characters in it.
I do a few different 'AddFormattedText' as well as 'AddTab'
I have 'Paragraph.Format.KeepTogether=true' set.

But the paragraph still wraps over page breaks. I want this paragraph to remain together. Is it the '\n' that is causing it to go over a page break? If so, what's the best way to do a new line?

Author:  Thomas Hoevel [ Mon Aug 11, 2014 8:17 am ]
Post subject:  Re: KeepTogether not working in Paragraph?

briankitt wrote:
Is it the '\n' that is causing it to go over a page break?
I don't know.
Can we have an SSCCE?

Author:  briankitt [ Tue Aug 12, 2014 2:52 am ]
Post subject:  Re: KeepTogether not working in Paragraph?

Sorry, my code was really complicated, and I needed to set up a dummy sample. My production version is an 8 line invoice, in which the 8 lines must be kept together. You can see with this sample, that this will break on line 5, but it should roll to the next page since it can't keep them together.

Code:
            Document myDocument = new Document();
            myDocument.Info.Title = "Title";
            myDocument.Info.Subject = "Subject";
            myDocument.Info.Author = "Brian";
            myDocument.DefaultPageSetup.BottomMargin = Unit.FromCentimeter(1);
            myDocument.DefaultPageSetup.RightMargin = Unit.FromCentimeter(1);
            myDocument.DefaultPageSetup.LeftMargin = Unit.FromCentimeter(1);

            // Add Styles
            Style myStyle = myDocument.Styles["Normal"];
            myStyle.Font.Name = "Times New Roman";

            myStyle = myDocument.Styles[StyleNames.Header];

            myStyle = myDocument.Styles[StyleNames.Footer];

            myStyle = myDocument.Styles.AddStyle("InvoiceDetail", "Normal");
            myStyle.Font.Size = 9;
            myStyle.ParagraphFormat.AddTabStop(".5cm", TabAlignment.Left);
            myStyle.ParagraphFormat.AddTabStop("17cm", TabAlignment.Right, TabLeader.Dots);

            myStyle = myDocument.Styles.AddStyle("InvoiceBold", "Normal");
            myStyle.Font.Size = 9;
            myStyle.Font.Bold = true;
            myStyle.ParagraphFormat.AddTabStop(".5cm", TabAlignment.Left);
            myStyle.ParagraphFormat.AddTabStop("17cm", TabAlignment.Right, TabLeader.Dots);


            myDocument.AddSection();
            Paragraph myP = myDocument.LastSection.AddParagraph();
            myP.Format.KeepTogether = true;

            for (int x = 0; x < 20; x++)
            {
                myP.AddTab();
                myP.AddFormattedText("Line 1\nLine 2\nLine 3\nLine 4\n", "InvoiceBold");
                myP.AddTab();
                myP.AddFormattedText("Line 5\nLine 6\nLine 7\nLine 8\n", "InvoiceDetail");
            }

            string reportPath = ConfigurationManager.AppSettings["TempDir"] + Guid.NewGuid().ToString() + ".pdf";
            PdfDocumentRenderer myRender = new PdfDocumentRenderer(true);
            myRender.Document = myDocument;
            myRender.RenderDocument();
            myRender.Save(reportPath);

Author:  Thomas Hoevel [ Tue Aug 12, 2014 8:08 am ]
Post subject:  KeepTogether working as intended if used properly

briankitt wrote:
myDocument.DefaultPageSetup.BottomMargin = Unit.FromCentimeter(1);
This is bad practice. You should create a Clone() of DefaultPageSetup and modify that.

briankitt wrote:
string reportPath = ConfigurationManager.AppSettings["TempDir"] + Guid.NewGuid().ToString() + ".pdf";
This does not compile. A true SSCCE is a ZIP file with a solution that compiles right away.


KeepTogether is working as intended.
You have a single paragraph that is so long that it requires three pages. You tell MigraDoc to keep it together on a single page. This is IMPOSSIBLE.
KeepTogether will only work properly if all paragraphs are small enough to fit on a single page.


Move
Code:
Paragraph myP = myDocument.LastSection.AddParagraph();
myP.Format.KeepTogether = true;
into the for loop and page breaks will always occur between "Line 8" and "Line 1".

Author:  briankitt [ Tue Aug 12, 2014 2:41 pm ]
Post subject:  Re: KeepTogether not working in Paragraph?

Oh, crap. Sorry. That sample I put together is wrong. In my actual production code, the paragraph is inside of the loop. Sorry about that. I'm going to work on this sample and make it more representative of my production code. My actual code is 100s of lines of code, most of it not related to PDF, so you don't want to look at that.

Dang.

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