user2122032
user2122032

Reputation: 81

How to reference a resource?

How would I reference the code below into c# to print. I used the resource dictionary because I don't want a window to show when I am printing but to print directly from a button.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<DockPanel Name="dockpanel" Width="auto"    LastChildFill="True" x:Key="Maindock">
        <Grid DockPanel.Dock="top" Width="340" >
</DockPanel>

Here is the print code:

//System.Printing
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities =                 
                printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);

//get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth,
                   capabilities.PageImageableArea.ExtentHeight);

// update the layout of the visual to the printer page size.
Print.Measure(sz);
Print.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth,
              capabilities.PageImageableArea.OriginHeight), sz));

//now print the visual to printer to fit on the one page.
//printDlg.PageRangeSelection(printQty);
//now print the visual to printer to fit on the one page.
String printerName = "Brother DCP-7045N Printer";

System.Printing.PrintQueue queue = new System.Printing.LocalPrintServer()
                                               .GetPrintQueueprinterName);
printDlg.PrintQueue = queue;

printDlg.PrintVisual(Print, "");

Upvotes: 2

Views: 568

Answers (1)

Peter Holmes
Peter Holmes

Reputation: 652

If the resource that you want to print is part of your application resources, i.e. added to the App.xaml file directly, as shown below, or via merged dictionaries, then you should be able to just new up a visual element and set the content. Here I am using this.FindResource() to get an instance of the resource to set as the content.

Note: You don't have to display the newed-up Page in order to print it.

Application Resource

<Application x:Class="PrintTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Grid x:Key="PrintTestResource">
            <TextBlock FontSize="50" HorizontalAlignment="Center" VerticalAlignment="Center">Hello World</TextBlock>
        </Grid>
    </Application.Resources>
</Application>

Print Code

public void Print()
{
    var printDialog = new PrintDialog();
    if (printDialog.ShowDialog().Value)
    {
        var printCapabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
        var printSize = new Size(printCapabilities.PageImageableArea.ExtentWidth, printCapabilities.PageImageableArea.ExtentHeight);

        var printPage = new Page();
        printPage.Content = this.FindResource("PrintTestResource");
        printPage.Measure(printSize);
        printPage.Arrange(new Rect(new Point(printCapabilities.PageImageableArea.OriginWidth, printCapabilities.PageImageableArea.OriginHeight), printSize));

        printDialog.PrintVisual(printPage, String.Empty);
    }
}

Upvotes: 1

Related Questions