Pages

Thursday, May 28, 2015

Open and close PDF reader from code

Here is an example of how you can open Adobe PDF Reader (or another PDF reader) with a particular PDF document, and close the reader again.
static void pager_FindWindow(Args _args)
{
    Filename    pdfFileName = 'MyDocumentFile.pdf';
    FilePath    pdfFilePath = @'C:\XYZ\'; 
    str         adobeExe;
    
    System.Diagnostics.Process              process;
    System.Diagnostics.ProcessStartInfo     processStartInfo;

    // Let Windows figure out the standard program and location for the PDF reader
    adobeExe = WinAPI::findExecutable(pdfFilePath + pdfFileName); 
    
    // Start the reader process
    new InteropPermission(InteropKind::ClrInterop).assert();

    process = new System.Diagnostics.Process();

    processStartInfo = new System.Diagnostics.ProcessStartInfo();
    processStartInfo.set_FileName(adobeExe);
    processStartInfo.set_Arguments(pdfFilePath + pdfFileName);

    process.set_StartInfo(processStartInfo);

    process.Start();
    

    // Wait 5 secs. before closing the window
    sleep (5000);
    
    // Close the window
    process.Kill();
}

No comments: