Some test text!

Adding printing

PDFTron SDK for UWP 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.

    pdftron.PDF.PDFViewCtrl MyPDFViewCtrl;
    pdftron.PDF.Tools.ToolManager MyToolManager;
    pdftron.PDF.PDFPrintManager MyPrintManager;

    The PDFPrintManager is a singleton, so let's grab it at the bottom of MainPage's constructor, under OpenButton.Click.

    MyPrintManager = pdftron.PDF.PDFPrintManager.GetInstance();
  2. 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).

    Right 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.

    Figure 5: Adding localized strings to the project for Printing.

    Once done, your solution explorer should show look as follows.

    Figure 6: Solution explorer with localized strings.

    Also, 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.

    Windows.ApplicationModel.Resources.ResourceLoader loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView("Printing");
    MyPrintManager.SetResourceLoader(loader);
  3. 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.

    MyPrintManager.AddStandardPrintOption(Windows.Graphics.Printing.StandardPrintTaskOptions.MediaSize);
    MyPrintManager.AddStandardPrintOption(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
    
    MyPrintManager.AddUserOptionPageRange();
  4. 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:

    if (file != null)
    {
       MyPrintManager.UnRegisterForPrintingContract();
    
       Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
       pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(stream);
       MyPDFViewCtrl.SetDoc(doc);
    
       MyPrintManager.RegisterForPrintingContract(doc, doc.GetDocInfo().GetTitle());
    }

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:

<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
    <Button x:Name="OpenButton">Open</Button> 
    <Button x:Name="PrintButton" IsEnabled="False">Print</Button> 
</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.

public MainPage()
{
    ...
    PrintButton.Click += PrintButton_Click;
    ...
}

async void OpenButton_Click(object sender, RoutedEventArgs e)
{
    ...
}

void PrintButton_Click(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}

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.

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

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:

if (file != null)
{
    MyPrintManager.UnRegisterForPrintingContract();
    PrintButton.IsEnabled = false;

    Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
    pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(stream);
    MyPDFViewCtrl.SetDoc(doc);

    MyPrintManager.RegisterForPrintingContract(doc, doc.GetDocInfo().GetTitle());
    PrintButton.IsEnabled = true;
}

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