PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 31, 2024 11:37 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Aug 11, 2014 3:34 am 
Offline

Joined: Wed Mar 06, 2013 3:53 pm
Posts: 22
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 11, 2014 8:17 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
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?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 12, 2014 2:52 am 
Offline

Joined: Wed Mar 06, 2013 3:53 pm
Posts: 22
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);


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 12, 2014 8:08 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
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".

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 12, 2014 2:41 pm 
Offline

Joined: Wed Mar 06, 2013 3:53 pm
Posts: 22
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group