title: Using Microsoft Print To PDF in VB.NET resulting in 0 KB "PDF"
body: <p>I am experiencing some problems when trying to print to pdf.
  Maybe, some of you cracks could have a look to my code snippet ?</p>
  
  <pre><code>[...]
  
  Private tmpPDFName As String
  Private printPDF As Printing.PrintDocument
  
  ''' &lt;summary&gt;
  ''' Delegate Method "finishPrintingPDF" should be called when finishing Printing 
  ''' &lt;/summary&gt;
  ''' &lt;param name="sender"&gt;&lt;/param&gt;
  ''' &lt;param name="e"&gt;&lt;/param&gt;
  ''' &lt;remarks&gt;&lt;/remarks&gt;
  Private Sub finishPrintingPDF(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
  
      Dim targetDir As String
  
      Try
          targetDir = "C:\Finished\"
          If Not System.IO.Directory.Exists(targetDir) Then System.IO.Directory.CreateDirectory(targetDir)
      Catch ex As Exception
          Throw New Exception(ex.Message)
      End Try
  
      System.IO.File.Copy(tmpPDFName, targetDir &amp; "123.pdf")
      Application.DoEvents()
      System.IO.File.Delete(tmpPDFName)
  
  End Sub
  
  
  ''' &lt;summary&gt;
  ''' Using "Microsoft Print To PDF" Printer to print to PDF
  ''' &lt;/summary&gt;
  ''' &lt;remarks&gt;&lt;/remarks&gt;
  Public Overloads Sub PrintDocumentToPDF()
  
      Dim tempUserDir As String : tempUserDir = String.Empty
      tempUserDir = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData &amp; "\Temp"
  
      If Not System.IO.Directory.Exists(tempUserDir) Then
          System.IO.Directory.CreateDirectory(tempUserDir)
      End If
  
      tmpPDFName = tempUserDir &amp; "\123.pdf"
  
      If System.IO.File.Exists(tmpPDFName) Then System.IO.File.Delete(tmpPDFName)
  
      printPDF = New Printing.PrintDocument
      printPDF.DocumentName = "Test"
      printPDF.PrintController = New Printing.StandardPrintController()
      ' CLSDomePrinting performing the printing (no error there, so I ignore this class)
      AddHandler printPDF.PrintPage, AddressOf CLSDoSomePrinting
      ' When finished Printing, the temporary file shall be copied to its final place 
      ' The only problem is: There is no really guarentee, it works
      ' Sometimes PDF is created as expected, but sometimes PDF has a size of 0 Bytes and is empty...
      ' Is there a proofed way to print to pdf using "Microsoft Print to PDF" ?
      AddHandler printPDF.EndPrint, AddressOf finishPrintingPDF
  
      Dim prnSettings As New Printing.PrinterSettings
      With prnSettings
          .Copies = 1
          .PrinterName = "Microsoft Print To PDF"
          .PrintFileName = tmpPDFName
          .PrintToFile = True
      End With
  
      printPDF.PrinterSettings = prnSettings
      ' So far, so good
      printPDF.Print()
  
  End Sub
  </code></pre>
  
  <p>So, this would be my approach. I dont see a reason, why my code snippet should not work. Most, it results in a file with 0 Bytes.</p>
  
  <p>I am working on virtual machine (Hyper V) in Windows 10 enviroment.</p>
  
  <p>Thank you, in advance
  Stephan</p>