user1734463
user1734463

Reputation: 91

How to print a pdf file from windows store application

I am creating a windows 8 app in c# and xaml. How can i print a pdf file on my system from this app without launching the pdf reader of windows.

Upvotes: 3

Views: 2844

Answers (3)

Peter
Peter

Reputation: 1687

It is possible to achieve this without any third party library if you want to print it with a device.

This isn't working to create or write a ".pdf"[Pdf File). To make this happen without another library you could maybe try to write images directly into a pdf, but i can only reference here the Pdf Documentation.

First of all you have to render your pdf. You can do this with Windows.Data.Pdf. You can take a look at an example here. The libary will handle the Pdf and you get some BitmapImages rendered. This library can read, but not write ".pdf"

Now you have to print your Images(rendered pdf pages). For this you take the Windows.Graphics.Printing.PrintManager. I would not recommend the msdn example in this case. So have a first look at Windows Store Apps and XAML Based Printing.

And you will find another printing example with MVVM here: Printing from MVVM XAML Windows 8 Store apps.

If you just follow the examples you will achieve to print a pdf on paper some trouble.

How to add custom settings for your printer is explained at How to add custom settings to the print preview UI (XAML)!

Upvotes: 2

yms
yms

Reputation: 10418

If a commercial library is an option, you could try Amyuni PDF Creator for WinRT.

From the documentation, the code for printing a PDF file in C# using this library would look like this:

// Printing on a XAML Windows Store Application
// The field AmyuniPDFCreator.PDFCreator m_pdfCreator; contains an instance of the PDFCreator control
TypedEventHandler<PrintManager, PrintTaskRequestedEventArgs> m_printTaskRequestedEventToken;
// Registering the Application View for printing
void RegisterForPrinting()
{
    // Request a PrintManager instance
    PrintManager printMan = PrintManager.GetForCurrentView();
    // Add a handler for printing events.
    m_printTaskRequestedEventToken = printMan.PrintTaskRequested += m_pdfCreator.Document.PrintTaskRequestedEventHandler;
}
// Unregistering the Application View for printing
void UnregisterForPrinting()
{
    // Remove the handler for printing events.
    PrintManager printMan = PrintManager.GetForCurrentView();
    printMan.PrintTaskRequested -= m_printTaskRequestedEventToken;
}

More details about the library can be found here.

Disclaimer: I currently work as a developer of the library

Upvotes: 2

SWalters
SWalters

Reputation: 3694

Windows 8 doesn't have an API to do this, so you'll have to acquire one elsewhere - something that's capable of properly rendering a PDF for you, and that's going to mean a full-blown PDF API with all the bells and whistles (I'm not aware of any of these for Windows 8 that only supports printing).

If only PDF Sharp had a WinRT version, I'd recommend it in a heartbeat... unfortunately it doesn't (yet). Only ones I know of that have an API for WinRT are Foxit and Siberix Report Writer.

Upvotes: 1

Related Questions