4

I'm working on a UWP application, written in c#, and I need to convert to pdf: - office documents (doc, docx, xls, xlsx, ppt, pptx) - images - Web pages

The application has to work in x86, x64, ARM, ARM64 architectures.

I know there are third party converter libraries on the market but only a few of them work in an ARM64 context or in UWP.

My idea is to use (in Windows 10) the "Microsoft PDF printer" that allows the users to save most of the file formats to PDF.

I found many posts asking for the same question but none of them really contains a helpful answer.

The code I found and tested is the following:

PrintDocument doc = new PrintDocument()
{
//DocumentName = safeDir + fileName,
    PrinterSettings = new PrinterSettings()
    {
        // set the printer to 'Microsoft Print to PDF'
        PrinterName = "Microsoft Print to PDF",

        // tell the object this document will print to file
        PrintToFile = true,


        // set the filename to whatever you like (full path)
        PrintFileName = safeDir + fileName,
    }
};
doc.Print();

The above code generates a valid but empty pdf. How should I set the original file content?

For example, if I have a Word file named myreport.docx, should I convert it to byte array and set it in somewhere?

Thank you in advance.

Enzo Failla
  • 84
  • 1
  • 5
  • With the upcoming WinUI you will be able to run the UI used on UWP on the Win32 App Model. This will make easier to do all kind of PDF manipulations. Check out #MSIgnite, Save the date: https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RE3TlM8 – Tony Oct 25 '19 at 21:35

2 Answers2

1

When using the PrintDocument class, you should read the file you are trying to print into a filestream as in the example here: https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?view=netframework-4.8

I personally prefer to use the Microsoft.Office.Interop libraries for Word and Excel to print. With these you just have to load the document, set the printer to "Adobe PDF", and save the document.

g1b
  • 11
  • 2
  • Is it a win32 class? – lindexi Oct 27 '19 at 23:41
  • The example provided prints only plain text - printing a PDF is an almost entirely different problem. Plain test uses DrawString(), but what can be used for PDF, if it's DrawImage(), how to convert the contents of your file to an Image for this to work etc – PandaWood Nov 30 '21 at 03:24
0

In UWP we use PrintDocument to print UIElement. And this is official tutorial. And this is code sample.

How to Print Web Pages

We need get WebViewBrush first before print. Please refer this case reply to get web content. Then view above code sample to print the WebViewBrush to pdf. And I have replied the similar case that you could refer.

How to print file

UWP PrintDocument just support print UIElement, so we need display the file content with uwp control then print. For detail steps please refer official code sample scenario 4.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • 1
    Thank you for your suggestions. I tested it and it works. However, what we really need is a "silent" conversion. Meaning that the user should just click on the "import button" without any other interaction. It seems it's not possible to print the document avoiding the ShowPrintUIAsync requester. Can you confirm that? – Enzo Failla Oct 29 '19 at 09:33
  • Yep, `ShowPrintUIAsync` is necessary in uwp. I have seen this requirement many times. But currently, we have no such api in uwp platform, so please feel free post your requirement in UserVoice. And you could also launch win32 app with `FullTrustProcessLauncher` then print file silent. – Nico Zhu Oct 29 '19 at 09:42