locked
How to automate Print to PDF RRS feed

  • Question

  • Hi

    I have a folder with n number of outlook message files with extension ".msg". The content of these files should be converted to PDF without altering its format using Microsoft Print to PDF option and the output files should be saved to a folder with .pdf extension automatically.

    Any help on this?

    Note: I have already tried using below script and it prints only blank pages in PDF.

    function ConvertTo-PDF {
    	param(
    		$TextDocumentPath
    	)
    	
    	Add-Type -AssemblyName System.Drawing
    	$doc = New-Object System.Drawing.Printing.PrintDocument
    	$doc.DocumentName = $TextDocumentPath
    	$doc.PrinterSettings = new-Object System.Drawing.Printing.PrinterSettings
    	$doc.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
    	$doc.PrinterSettings.PrintToFile = $true
    	$file=[io.fileinfo]$TextDocumentPath
    	$pdf= [io.path]::Combine($file.DirectoryName, $file.BaseName) + '.pdf'
    	$doc.PrinterSettings.PrintFileName = $pdf
    	$doc.Print()
    	$doc.Dispose()
    }

    Thanks.


    • Edited by Nty89 Friday, August 30, 2019 9:31 AM
    Friday, August 30, 2019 9:28 AM

All replies

  • You cannot use that code to print anything. You must programmatically draw the graphics that you want in the new document.

    To convert a document to a pdf use MS Word or a third party converter.


    \_(ツ)_/

    Friday, August 30, 2019 6:25 PM
  • A .msg file is not a simple collection of text.

    https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxmsg/b046868c-9fbf-41ae-9ffb-8de2bd4eec82


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, August 30, 2019 7:36 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, September 6, 2019 11:37 AM