Elango T

Elango T

  • NA
  • 48
  • 8.1k

Printing XPS Document

Jan 25 2018 1:33 AM
Dear All,
 
This is my first question in C# Corner. Please apologize for any inconvenience.
 
Iam using WPF and XPS to print content from a FlowDocument.
 
XAML : 
<FlowDocumentScrollViewer Width="1000">
<FlowDocument x:Name="FD">
<Paragraph>
<Image Source="http://www.wpf-tutorial.com/images/logo.png" Width="90" Height="90" Margin="0,0,30,0" />
<Label Content="Testing Label"></Label>
<TextBox Name="txtHeading" Width="150"></TextBox>
<Run FontSize="120">WPF</Run>
</Paragraph>
<Paragraph>
WPF, which stands for
<Bold>Windows Presentation Foundation</Bold> ,
is Microsoft's latest approach to a GUI framework, used with the .NET framework.
Some advantages include:
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
 
 C# Code to Print :
 
if (File.Exists("printPreview.xps"))
{
File.Delete("printPreview.xps");
}
var xpsDocument = new XpsDocument("printPreview.xps", FileAccess.ReadWrite);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)FD).DocumentPaginator);
//Document = xpsDocument.GetFixedDocumentSequence();
// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
// Display the dialog. This returns true if the user presses the Print button.
Nullable<Boolean> print = pDialog.ShowDialog();
if (print == true)
{
FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print job");
xpsDocument.Close();
}
 It works fine expect the Final output is by splitting the Page into 2 Columns. Need help in making the page layout in single coulmn.