view and print pdf in .net maui

shadeveloper 40 Reputation points
2024-06-14T05:38:19.0766667+00:00

Hello, I would need to be able to view and print a pdf that is hosted on a server. The url of the pdf will be sent to the app.

To view it I am using a Webview with the following prefix as Source:

pdfview.Source = "https://docs.google.com/viewer?url=..."

This webview shows me a print icon, but it does nothing.

How could I activate it?

Is there another way in .net maui to print a pdf?

Thank you

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,616 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,202 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,046 Reputation points Microsoft Vendor
    2024-06-25T09:46:15.3933333+00:00

    Hello,

    Currently MAUI does not have the print API to print PDF or HTML documents, you need to tap into Android native library and expose this feature for your App.

    You could refer to Printing HTML documents and Printing custom documents to get details about how to print HTML/PDF documents by native way.

    Then, you could invoke this Android native print function in your MAUI application. You could refer to Invoke platform code.

    Please refer to How to print PDF/HTML in MAUI? - Microsoft Q&A

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. 感恩的❤ 155 Reputation points
    2024-06-14T06:20:28.3933333+00:00

    Let's suppose you're now using PDF4NETView, so:

     // Load the PDF file.
                PDFFile file = PDFFile.Open("..\\..\\..\\..\\..\\SupportFiles\\MulticolumnTextAndImages.pdf");
                
                // Create a default printer settings to print on the default printer.
                PrintQueue printQueue = new LocalPrintServer().DefaultPrintQueue;
                PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(printQueue);
                Random rand = new Random();
                pdfPrintSettings.PageScaling = PageScaling.MultiplePagesPerSheetProportional;
                printQueue.DefaultPrintTicket.Collation = Collation.Collated;
                //printQueue.DefaultPrintTicket.CopyCount = 3;
                pdfPrintSettings.Rows = 2;
                pdfPrintSettings.Columns = 1;
                pdfPrintSettings.AutoRotate = false;
                // Print the PDF file.
                file.Print(pdfPrintSettings);
                file.Dispose();
    

    For more, please read the full example here: https://github.com/o2solutions/pdfview4net/blob/main/WPF/.NET%20Framework/PrintPDF/CS/Program.cs