0

So i need to print a label from a windows 8 app,(c# Xaml), and all of the samples i've found are overcomplicated for my needs. My content is a seperate page, containing a Text block and an image, which are filled when the page loads, all i need it to do is print that page. Is there a simplified method for printing ( ie, no using RichTextBox and Overflows etc) for a single simple page. For the interested, this is the page i need to print:

<Page
x:Class="Storeageapp.OutputforLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Storeageapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="300" Height="200">
<Grid x:Name="printableArea">

    <Grid.RowDefinitions>

        <RowDefinition Height="3*"/>
        <RowDefinition Height="3*"/>

    </Grid.RowDefinitions>


    <StackPanel x:Name="header" Grid.Row="0" Grid.ColumnSpan="2" Height="60"  Visibility="Collapsed">

    </StackPanel>

    <TextBlock x:Name="UID" Text="Hello World" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" />

    <Image Source="" x:Name="scenarioImage"  HorizontalAlignment="Center" Grid.Row="1" Grid.Column="0" Margin="10"/>

    <StackPanel x:Name="footer"  Grid.Row="4" Grid.Column="0" VerticalAlignment="Top" Visibility="Collapsed">

    </StackPanel>
</Grid>
</Page>

I'd be happy if there was some way to print it as an image even but i can't figure that out. Thanks. edit: This is in a windows store app sorry, not WPF.

1 Answer 1

2

You can perform basic printing in WPF using the PrintDialog Class. From the example in the linked page:

private void InvokePrint(object sender, RoutedEventArgs e)
{
    // 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)
    {
        XpsDocument xpsDocument = new XpsDocument("C:\\FixedDocumentSequence.xps", FileAccess.ReadWrite);
        FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
        pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print job");
    }
}

However, it is possible to print with even less code than that:

PrintDialog printDialog = new PrintDialog();
document.ColumnWidth = printDialog.PrintableAreaWidth;
if (printDialog.ShowDialog() == true) printDialog.PrintDocument(
    ((IDocumentPaginatorSource)document).DocumentPaginator, "Flow Document Print Job");
2
  • Sorry for the ambiguity, i'm using a Windows Store app, not WPF. Commented Jul 14, 2015 at 13:10
  • @Sheridan. Good example - thank you for sharing. It helped me a lot in my WPF project. +1
    – nam
    Commented Jun 2, 2021 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.