title: Programmatically provide a filepath as input file for “Microsoft Print to PDF” printer
body: <h1>Desired result</h1>
  
  <p>I want to print a file to a new PDF using the  Windows 10 printer "Microsoft Print to PDF" which is installed by default.</p>
  
  <p>When you select this printer as your default printer and use your context menu on a file and select <em>Print</em>, it only asks for a save directory and name. After that, it immediately converts to PDF and saves the file. </p>
  
  <p>As long as MS Office is installed, this works for Word, Excel, PowerPoint file types. But also for common image types and normal text files.</p>
  
  <p>I'd like to automate this by providing a default path.</p>
  
  <h1>What I already tried</h1>
  
  <p>Stackoverflow already has <a href="https://stackoverflow.com/questions/31896952/how-to-programmatically-print-to-pdf-file-without-prompting-for-filename-in-c-sh">this related question</a>, but it does not address my specific problem and is rather incomplete and not working.</p>
  
  <p>But I came up with this C# console program which uses the PDF printer to produce a new PDF on my desktop with "Hello World" as string</p>
  
  <pre><code>namespace PrintToPdf_Win10
  {
      using System;
      using System.Drawing;
      using System.Drawing.Printing;
  
      class Program
      {
          public static void Main(string[] args)
          {
              PrintDocument printDoc = new PrintDocument
              {
                  PrinterSettings = new PrinterSettings
                  {
                      PrinterName = "Microsoft Print to PDF",
                      PrintToFile = true,
                      PrintFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/test.pdf"
                  }
              };
  
              printDoc.PrintPage += printDoc_PrintPage;
              printDoc.Print();
              Console.ReadKey(true);
          }
  
          static void printDoc_PrintPage(object sender, PrintPageEventArgs e)
          {
              e.Graphics.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, 50, 50);
          }
      }
  }
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>How do I set the content of - let's say a Word file - as input for my <code>printDoc</code> object?</p>
  
  <p>Is there a generic way to set <code>printDoc</code> by providing only a filePath to my file I want to print from? Or do I have to create a custom function for each possible filetype families like:</p>
  
  <ul>
  <li>Office filetypes (<code>doc, docx, xls, xlsx, xlsm, ppt, pptx</code> etc.)</li>
  <li>Image filetypes (<code>png, bmp, jpg</code>)</li>
  <li>text files (<code>txt, rtf, ini</code>)</li>
  </ul>