using LoginPI.Engine.ScriptBase; using System.IO; public class pdfPrinting : ScriptBase { private void Execute() { /* -Disclaimer: This workload is provided as-is and might need further configuration and customization to work successfully in each unique environment. For further Professional Services-based customization please consult with the Login VSI Support and Services team. Please refer to the Help Center section "Application Customization" for further self-help information regarding workload crafting and implementation. -Run the workload against the "target" using Script Editor to ensure it will work before uploading it and testing with it -Workload version and changelist: --V1.0 | original -Leave Application running compatibility: true Metadata: -Utilizing Windows 10 Professional x86; winver 1909; OS Build 18363.476 -ScriptingToolSet ver. 3.6.23 -Scripting date/author: V1.0: Dec 20 2019 Read me; items to note; caveats: -This script was designed to: -Open a document in an application -Send the keyboard shortcut to open the Print dialog -Select the Microsoft Print to PDF printer -Type the output path -Save the file -Open and close the subsequent file in a PDF reader application -Please read and set the variables section -Find and use the path to the target application which will also open the target file, such as: Example: "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "c:\MyPdf.pdf" **Some of the code blocks will need to be customized** 1) The interaction with the printer selection/print prepare dialog box, within the section "This will select the Microsoft Print to PDF printer" 2) The interaction with the save file/output popup window, within the section "This will find the save file/output popup window" */ string OutputFileFolder = @"c:\temp\"; // This is for creating the path in which the pdf printed file will be saved, if it doesn't exist. This is so the target application won't produce an error message such as "The specified directory doesn't exist" when trying to save the file; example "c:\temp\" string ProcessNameTaskkill = "winword*"; // To ensure the target application isn't running before the START(); metafunction, put the process name of the target application here; example: "winword*" int GlobalTimeoutInSeconds = 60; // Define how long it should take maximum for operations to complete, such as opening an Winword document, in seconds. This script will fail, as designed, if this value is surpassed; example: 60 int WaitSeconds = 2; // Define here how many seconds to wait in between functions; example: 5. This will allow for the target application's UI to catch up int CharactersPerMinuteToType = 999; // Define how many characters per minute the typing function will type; example; 180 string OutputFilePath = @"c:\temp\mypdfoutput.pdf"; // Put the path to save the file at; example: "c:\temp\mypdfoutput.pdf". Please note this will also delete this file path first in this script, if it exists. A UNC/network path would also work here string OutputFileName = "*mypdfoutput*"; // Put the filename of the outputted pdf file here. string InitialWindowTitleName = "*mydoc*"; // Put the window title name that will be present once the source file has been opened within it; example: "*mysheetinput.docx*" string PDFPrintWindowTitleName = "*Save Print Output As*"; // Put the name of the target application's Window title here; example: "*Print*" string KeystrokesToClearOutFilenameField = "{CTRL+a}{BACK}"; // Put the keystrokes combination to select all text and clear out a field; example "{CTRL+a}{BACK}" -- which will do ctrl+a -> backspace string KeystrokesToOpenPrintDialog = "{CTRL+p}"; // Put the keystrokes combination to open the print dialog page; example "{CTRL+p}" -- which will do ctrl+p string PDFReaderAppPaneXPath = "Pane:SUMATRA_PDF_CANVAS"; // Put the xPath for the document pane of the PDF Reader application here -- this should only be present once the pdf print output file has loaded in the PDF reader application; example - in Sumatra PDF reader the xPath is "Pane:SUMATRA_PDF_CANVAS" // Use the Application XRay tool to determine this. Read up more about the Application XRay tool's usage, if need be, in Login PI's documentation string InitialDocReaderAppPaneXPath = "Pane:_WwF/Pane:_WwB/Document:_WwG"; // Put the xPath parameter with the xPath of a Control that will be present once the initial application has loaded the "target" doccument; example - in winword the xPath for the main document pane might be "Pane:_WwF/Pane:_WwB/Document:_WwG" // Use the Application XRay tool to determine this. Read up more about the Application XRay tool's usage, if need be, in Login PI's documentation // Ensuring the target application isn't already running ShellExecute("cmd /c taskkill /f /im " + ProcessNameTaskkill,waitForProcessEnd:true,timeout:GlobalTimeoutInSeconds); // These are file system prepare tasks if(File.Exists(OutputFilePath))// This will delete OutputFilePath variable path, if it exists { File.Delete(OutputFilePath); } System.IO.Directory.CreateDirectory(OutputFileFolder); // This will create the path in the OutputFileFolder variable, if it doesn't exist // Opening the doc and verifying it opens; encapsulated by a custom timer StartTimer(name:"OpenDocumentToLanding"); START(mainWindowTitle:InitialWindowTitleName,timeout:GlobalTimeoutInSeconds); MainWindow.FindControlWithXPath(xPath : InitialDocReaderAppPaneXPath,timeout:GlobalTimeoutInSeconds); // Replace the xPath parameter with the xPath of a Control that will be present once the application has loaded StopTimer(name:"OpenDocumentToLanding"); Wait(WaitSeconds); // The following will open the print dialog Type(KeystrokesToOpenPrintDialog,cpm:CharactersPerMinuteToType); // This will select the Microsoft Print to PDF printer Wait(WaitSeconds); MainWindow.FindControl(className : "ComboBox:NetUIDropdownAnchor", title : "Which Printer",timeout:GlobalTimeoutInSeconds).Click(); Wait(WaitSeconds); MainWindow.FindControl(className : "ListItem:NetUIGalleryButton", title : "Microsoft Print to PDF",timeout:GlobalTimeoutInSeconds).Click(); MainWindow.FindControl(className : "*utton", title : "Print",timeout:GlobalTimeoutInSeconds).Click(); // This will find the save file/output popup window, encapsulated by a custom timer, click on the path field, clear it out, and type in the output file path StartTimer(name:"SaveFileWindowAppear"); var SaveFileWindow = FindWindow(title : PDFPrintWindowTitleName,timeout:GlobalTimeoutInSeconds); StopTimer(name:"SaveFileWindowAppear"); SaveFileWindow.Focus(); Wait(WaitSeconds); var FileNamePath = SaveFileWindow.FindControl(className : "Edit:Edit", title : "File name:",timeout:GlobalTimeoutInSeconds); FileNamePath.Click(); Type(KeystrokesToClearOutFilenameField,cpm:CharactersPerMinuteToType); Type(OutputFilePath,cpm:CharactersPerMinuteToType); var SaveButton = MainWindow.FindControl(className : "Button:Button", title : "&Save",timeout:GlobalTimeoutInSeconds); SaveButton.Click(); // This will wait for the target file to be present; saved. This is encapsulated by a custom timer. StartTimer(name:"TimeToPDFFilePrintOutput"); while (!System.IO.File.Exists(OutputFilePath)){ System.Threading.Thread.Sleep(1); } STOP(); // This will close out of the application. The application shouldn't close until the pdf print operation has been completed. StopTimer(name:"TimeToPDFFilePrintOutput"); // The following will open the outputted .pdf file in the "target" pdf reader, encapsulated by a custom timer Wait(WaitSeconds); StartTimer(name:"OpenPDFFile"); ShellExecute(OutputFilePath,waitForProcessEnd:false); var PDFReaderApp = FindWindow(title : OutputFileName,timeout:GlobalTimeoutInSeconds); PDFReaderApp.FindControlWithXPath(xPath : PDFReaderAppPaneXPath,timeout:GlobalTimeoutInSeconds); // Replace the xPath parameter with the xPath of a Control that will be present once the application has loaded StopTimer(name:"OpenPDFFile"); // This will close out of the app PDFReaderApp.Focus(); Wait(WaitSeconds); KeyDown(KeyCode.ALT); Type("{F4}"); KeyUp(KeyCode.ALT); Wait(WaitSeconds); } }