Skip to main content

Printing and sharing

Print and Share live in the toolbar's More Options menu and are also available as Print(), PrintAsync(), Share(), ShareAsync(), PrintCommand and ShareCommand. Both include the current edits.

Platform support

PlatformPrintShare
WindowsStandard print dialog, vector output.Windows share sheet. Falls back to a save-a-copy picker if the share UI is unavailable.
macOSSystem print panel with preview.System share picker, anchored to the toolbar button.
iOSSystem print controller.System share sheet, as a popover on iPad.
AndroidSystem print framework.Share chooser. No manifest changes are needed.
LinuxNot built in.Not built in.
BrowserNot built in.Not built in.

The menu entries are hidden when the platform has no service and the app does not handle the request. CanPrint and CanShare report whether printing or sharing is possible right now. Hide the entries explicitly with IsPrintVisible, IsShareVisible or IsMoreOptionsVisible.

await Viewer.PrintAsync();
await Viewer.ShareAsync();

PrintAsync accepts a PrintOptions (Title, PageRange, Copies, Color, Duplex, Orientation, Scaling) and ShareAsync a ShareOptions (Title, Text, Subject). Both are in the Avalonia.Controls.Pdf.Services namespace. The viewer fills in Owner and Anchor on both.

Handle the request in your app

Handle PrintRequested or ShareRequested to take over. Set Handled to true before the first await and the built-in service is skipped. GetDocumentBytesAsync() returns the PDF with its edits. This is also how you add printing on Linux and in the browser.

Viewer.PrintRequested += async (_, e) =>
{
e.Handled = true;
var pdf = await e.GetDocumentBytesAsync();
await myPrintPipeline.PrintAsync(pdf, e.Options.Title);
};

Replace the platform service

To replace the platform implementation, assign your own IPrintService or IShareService to PrintService or ShareService. IPrintService has PrintAsync(IPdfDocument, PrintOptions?, CancellationToken) and ShowPrintPreviewAsync(IPdfDocument, PrintOptions?). IShareService has ShareAsync(IPdfDocument, ShareOptions?, CancellationToken) and ShareFileAsync(string filePath, ShareOptions?, CancellationToken). Leave a property unset to use the built-in service, or set it to null to remove the feature.

Viewer.PrintService = new MyPrintService();
Viewer.ShareService = null; // no Share entry

See also