Printing a document with UWP

Apryse's UWP PDF library supports printing. The library handles all the hard work, you simply have to tell it to register and what options you would like to be available.

  1. First, we have to add a PDFPrintManager. Declare another member variable in MainPage.xaml.cs next to MyToolManager.The PDFPrintManager is a singleton, so let's grab it at the bottom of MainPage's constructor, under OpenButton.Click.

C#

1pdftron.PDF.PDFViewCtrl MyPDFViewCtrl;
2pdftron.PDF.Tools.ToolManager MyToolManager;
3pdftron.PDF.PDFPrintManager MyPrintManager;

MyPrintManager = pdftron.PDF.PDFPrintManager.GetInstance();

sh

12. Now, we need to add localized string options. Because the printer options are handled by the library, it needs to provide the option of localization. Inside the Lib folder there is a folder called strings that contains the string resources for American English (en-US).
2
3Right click on PDFNetSample, select **Add** -> **New Folder**. Name this folder strings. The right click on the strings folder, again select **Add** -> **New Folder**. Name this folder en-US. From the downloaded package, go to the folder Lib -> strings -> en-US and drag the file Printing.resw into the en-US folder you created in the Sample project.
4
5![localizedstrings](./img/localizedstrings.bmp)
6Adding localized strings to the project for Printing.
7
8Once done, your solution explorer should show look as follows.
9
10![solutionwithlocalizedstrings](./img/solutionwithlocalizedstrings.bmp)
11Solution explorer with localized strings.
12
13Also, we need to tell the PDFPrintManager where to find this resource. In the constructor for MainPage, we can add the following two lines of code below where the PDFPrintManager is initialized.
14
15```csharp
16
17Windows.ApplicationModel.Resources.ResourceLoader loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView("Printing");
18MyPrintManager.SetResourceLoader(loader);
  1. Now that the PDFPrintManager has the resources to display the correct options, we can go on and add some options to it. Let's add the 2 standard options of orientation and paper size. We can also add the user option for page range. This can be done by adding the following lines at the bottom of MainPage's Constructor.

C#

1MyPrintManager.AddStandardPrintOption(Windows.Graphics.Printing.StandardPrintTaskOptions.MediaSize);
2 MyPrintManager.AddStandardPrintOption(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
3
4 MyPrintManager.AddUserOptionPageRange();
  1. Finally, we need to register for printing. Normally, this would be done in a Page's OnNavigatedTo and OnNavigatedFrom functions, but in this case, we want the title of the print page to match the document, also, we only want printing to be available when a document is open. So, instead we register for printing each time we open a document (and unregister when we close one). Inside OpenButton_Click, inside the if statement (file != null) we will first unregister printing and then register for it at the end with the new document.Edit the code as follows:

C#

1if (file != null)
2{
3 MyPrintManager.UnRegisterForPrintingContract();
4
5 Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
6 pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(stream);
7 MyPDFViewCtrl.SetDoc(doc);
8
9 MyPrintManager.RegisterForPrintingContract(doc, doc.GetDocInfo().GetTitle());
10}

Note that calling PDFPrintManager.UnRegisterForPrintingContract() is safe because it will not unregister if it is not already registered.

The next step is to add a print button to the sample. Until we have opened a document, we want it to be disabled. Let's add it next to the Open button as follows:

XML

1<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
2 <Button x:Name="OpenButton">Open</Button>
3 <Button x:Name="PrintButton" IsEnabled="False">Print</Button>
4</StackPanel>

We need to add a click handler, similar to the Open button. So, just below where we register for the open button click, let's register for the print button's click event. And let's put the handler under OpenButton_Click.

C#

1public MainPage()
2{
3 ...
4 PrintButton.Click += PrintButton_Click;
5 ...
6}
7
8async void OpenButton_Click(object sender, RoutedEventArgs e)
9{
10 ...
11}
12
13void PrintButton_Click(object sender, RoutedEventArgs e)
14{
15 throw new NotImplementedException();
16}

We need to add a click handler, similar to the Open button. So, just below where we register for the open button click, let's register for the print button's click event and let's put the handler under OpenButton_Click.

C#

1async void PrintButton_Click(object sender, RoutedEventArgs e)
2 {
3 // only print if we're wide enough.
4 if (Window.Current.Bounds.Width >= 500)
5 {
6 await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
7 }
8 }

We also need to enable the print button when a document is opened, so let's enable it when we Register for the printing contract and disable it when we unregister. In OpenButton_click, edit it as follows:

C#

1if (file != null)
2{
3 MyPrintManager.UnRegisterForPrintingContract();
4 PrintButton.IsEnabled = false;
5
6 Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
7 pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(stream);
8 MyPDFViewCtrl.SetDoc(doc);
9
10 MyPrintManager.RegisterForPrintingContract(doc, doc.GetDocInfo().GetTitle());
11 PrintButton.IsEnabled = true;
12}

At this point, printing should be added to the sample.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales