In my app, I have printing working through dependency service in iOS and Android. I found a class here that implemented printing on UWP. However, it only prints one page.
Here is my content page defining the webview:
` public class GroceryListWebView : ContentPage
{
public interface IBaseUrl { string Get(); }
public Command PrintServiceCommand { get; set; }
private WebView _browser;
private string _htmlSource;
public GroceryListWebView(string fileName)
{
BindingContext = this;
var browser = new WebView();
PrintServiceCommand = new Command(Print);
//Build webview from html
var htmlSource = new HtmlWebViewSource
{
Html = File.ReadAllText(fileName)
};
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
browser.Source = htmlSource;
_htmlSource = File.ReadAllText(fileName);
_browser = browser;
Content = browser;
//Build menu for the different platforms
switch (Device.RuntimePlatform)
{
case Device.Android:
{
var printToolbarItem = new ToolbarItem()
{
IconImageSource = "print.png",
Order = ToolbarItemOrder.Secondary,
Priority = 0,
Text = "Print"
};
printToolbarItem.SetBinding(MenuItem.CommandProperty, "PrintServiceCommand");
ToolbarItems.Insert(0,printToolbarItem);
var shareToolbarItem = new ToolbarItem()
{
IconImageSource = "sharing.png",
Order = ToolbarItemOrder.Secondary,
Priority = 1,
Text = "Sharing"
};
shareToolbarItem.SetBinding(MenuItem.CommandProperty, "ShareListCommand");
ToolbarItems.Insert(1, shareToolbarItem);
break;
}
case Device.iOS:
{
var printToolbarItem = new ToolbarItem()
{
IconImageSource = "print.png",
Order = ToolbarItemOrder.Primary,
Priority = 0,
Text = "Print"
};
printToolbarItem.SetBinding(MenuItem.CommandProperty, "PrintServiceCommand");
ToolbarItems.Insert(0, printToolbarItem);
var shareToolbarItem = new ToolbarItem()
{
IconImageSource = "sharing.png",
Order = ToolbarItemOrder.Primary,
Priority = 1,
Text = "Sharing"
};
shareToolbarItem.SetBinding(MenuItem.CommandProperty, "ShareListCommand");
ToolbarItems.Insert(1, shareToolbarItem);
break;
}
case Device.UWP:
{
var printToolbarItem = new ToolbarItem()
{
IconImageSource = "print.png",
Order = ToolbarItemOrder.Primary,
Priority = 0,
Text = "Print"
};
printToolbarItem.SetBinding(MenuItem.CommandProperty, "PrintServiceCommand");
ToolbarItems.Insert(0, printToolbarItem);
var shareToolbarItem = new ToolbarItem()
{
IconImageSource = "sharing.png",
Order = ToolbarItemOrder.Primary,
Priority = 1,
Text = "Sharing"
};
shareToolbarItem.SetBinding(MenuItem.CommandProperty, "ShareListCommand");
ToolbarItems.Insert(1, shareToolbarItem);
break;
}
}
}
public void Print()
{
var printService = DependencyService.Get<IPrintService>();
printService.Print(_browser, _htmlSource);
}
}`
Here is the class I am using for my dependency service:
`[assembly: Dependency(typeof(PrintUwp))]
namespace GroceryList.UWP
{
public class PrintUwp : IPrintService
{
PrintManager printmgr = PrintManager.GetForCurrentView();
PrintDocument PrintDoc;
PrintDocument printDoc;
PrintTask Task;
private Windows.UI.Xaml.Controls.WebView ViewToPrint = new Windows.UI.Xaml.Controls.WebView();
public PrintUwp()
{
printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
}
public async void Print(WebView viewToPrint, string htmlSource)
{
ViewToPrint.NavigateToString(htmlSource);
if (PrintDoc != null)
{
printDoc.GetPreviewPage -= PrintDoc_GetPreviewPage;
printDoc.Paginate -= PrintDoc_Paginate;
printDoc.AddPages -= PrintDoc_AddPages;
}
printDoc = new PrintDocument();
try
{
printDoc.GetPreviewPage += PrintDoc_GetPreviewPage;
printDoc.Paginate += PrintDoc_Paginate;
printDoc.AddPages += PrintDoc_AddPages;
var showprint = await PrintManager.ShowPrintUIAsync();
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
PrintDoc = null;
GC.Collect();
}
private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
var deff = args.Request.GetDeferral();
Task = args.Request.CreatePrintTask("Grocery List", OnPrintTaskSourceRequested);
deff.Complete();
}
async void OnPrintTaskSourceRequested(PrintTaskSourceRequestedArgs args)
{
var def = args.GetDeferral();
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
args.SetSource(printDoc.DocumentSource);
});
def.Complete();
}
private void PrintDoc_AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(ViewToPrint);
printDoc.AddPagesComplete();
}
private void PrintDoc_Paginate(object sender, PaginateEventArgs e)
{
PrintTaskOptions opt = Task.Options;
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void PrintDoc_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
printDoc.SetPreviewPage(e.PageNumber, ViewToPrint);
}
}
}`
I would appreciate any ideas on how to get this to print more than one page from UWP.
John