Reputation: 767
I'm trying to make a small label printer with c# 2010 using the flowdocument from wpf.
I generate the labels like this:
FlowDocument doc = new FlowDocument();
Paragraph p = new Paragraph();
foreach (Labels label in labels)
{
p.Inlines.Add(label.name+"\n");
p.Inlines.Add(label.age + "\n");
p.Inlines.Add(label.price + "\n");
p.Inlines.Add( "\n");
doc.Blocks.Add(p);
}
It´s working fine But when there are more labels generated then fit on one page, the labels get split. So for example that name is on Page1 and age, price are on Page2.
Now I asked you for a possibility to prevent the labels for being split.
Upvotes: 2
Views: 1367
Reputation: 1876
Yes. You can specify that a 'Paragraph' should not be split across pages or columns by setting its 'KeepTogether' property to True (default is False). The only caveat is if there is not enough room for the 'Paragraph' to exist on one page then the 'KeepTogether' property will be ignored for the sake of displaying the content. For this to work in your example, each label will need to be its own paragraph like this:
FlowDocument doc = new FlowDocument();
foreach (Labels label in labels)
{
Paragraph p = new Paragraph();
p.KeepTogether = true;
p.Inlines.Add(label.name + "\n");
p.Inlines.Add(label.age + "\n");
p.Inlines.Add(label.price + "\n");
doc.Blocks.Add(p);
}
The last newline you were adding is no longer necessary as there is naturally a space between paragraphs. You may even consider removing the last newline character on the 'price' line.
Upvotes: 6