Reputation: 58
Im trying to print visual (or anything related) with pagination from a WPF element. I'm using a MVVM Pattern development.
This is my visual layout. Where the user can scroll to view the pages.
<ScrollViewer>
<StackPanel x:Name="Wrapper">
<StackPanel x:Name="PageOne" />
<StackPanel x:Name="PageTwo" />
</StackPanel>
</ScrollViewer>
The visual is passed via a Command Binding on a button.
<Button Command="{Binding PrintCommand}" CommandParameter="{BindingElementName=Wrapper}"
The visual is passed to the print Method.
PrintDialog newDialog = new PrintDialog();
newDialog.PrintVisual(MyVisualName, "Printing is Fun!");
I would like to paginate the two pages (and more), and also scale the visual to the paper, Whilst holding true to MVVM style.
Thanks.
Upvotes: 0
Views: 1076
Reputation: 58
In the end i used Flow Inline Flowdocument.
<FlowDocumentScrollViewer>
<FlowDocument x:Name="EntirePage">
<Section>
<BlockUIContainer>
</BlockUIContainer>
</Section>
</FlowDocument>
</FlowDocumentScrollViewer>
Every control within the BlockUIContainer is printed. (A Lot of page size and margin fiddling is required to get pagination working perfectly) Section/ BlockUI does pagination somewhat automatically - so for me page one was one BlockUi and Page two was another - Comment/Ask for more info
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding PrintCommand}" CommandParameter="{Binding ElementName=EntirePage}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
I passed the FlowDocument via a command Parameter.
printCommand = new RelayCommand(p => PreparePrint((FlowDocument)p));
(pageVisual being the flow document passed via the command param)
Then between a few of methods i get to;
IDocumentPaginatorSource idocument = pageVisual as IDocumentPaginatorSource;
printDialog.PrintDocument(idocument.DocumentPaginator, "Printing Machine : " + Machine.Serial);
If you are confused and need help (much like i was) then don't hesitate to comment/ask questions.
Upvotes: 2