// TARGET:winword.exe // START_IN: 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 Microsoft Windows 11 Business; winver 1909; OS Build 10.0.26100 -ScriptEditor ver. 5.14.6. -Scripting date/author: V1.1: Mar 14 2025 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:\Users\Its me\AppData\Local\SumatraPDF\SumatraPDF.exe" "c:\mypdfoutput.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" */ // 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 OutputFileFolder = @"c:\temp\"; // To ensure the target application isn't running before the START(); metafunction, // put the process name of the target application here; example: "winword*" string ProcessNameTaskkill = "winword*"; // 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 GlobalTimeoutInSeconds = 60; // 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 WaitSeconds = 2; // Define how many characters per minute the typing function will type; example; 180 int CharactersPerMinuteToType = 999; // 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 OutputFilePath = @"c:\temp\mypdfoutput.pdf"; // Put the filename of the outputted pdf file here. string OutputFileName = "*mypdfoutput*"; // Put the window title name that will be present once the source file has been opened within it; example: "*mysheetinput.docx*" string InitialWindowTitleName = "Word"; // Put the name of the target application's Window title here; example: "*Print*" string PDFPrintWindowTitleName = "*Save Print Output As*"; // Put the keystrokes combination to select all text and clear out a field; // example "{CTRL+a}{BACK}" -- which will do ctrl+a -> backspace string KeystrokesToClearOutFilenameField = "{CTRL+a}{BACK}"; // Put the keystrokes combination to open the print dialog page; // example "{CTRL+p}" -- which will do ctrl+p string KeystrokesToOpenPrintDialog = "{CTRL+p}"; // 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"; // for Acrobat Reader, the string is "Pane:AVL_AVView[1]/Pane:AVL_AVView/Pane:AVL_AVView" // string PDFReaderAppPaneXPath = "Pane:SUMATRA_PDF_CANVAS"; string PDFReaderAppPaneXPath = "Pane:AVL_AVView[1]/Pane:AVL_AVView/Pane:AVL_AVView"; // 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:_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); Wait(WaitSeconds); // If Word opens to the document type selection page, uncomment the following line // to click on "Blank Document". If Word opens to a normal blank document, // leave the next line commented out. //MainWindow.FindControlWithXPath(xPath : "Pane:FullpageUIHost/Pane:NetUIFullpageUIWindow/Pane:NetUIScrollViewer[1]/Group:NetUIScrollViewer/Group:NetUISlabContainer/List:NetUIListView/ListItem:NetUIListViewItem",timeout:GlobalTimeoutInSeconds).Click(); // Click the item // Replace the xPath parameter with the xPath of a Control that will be present once the application has loaded MainWindow.FindControlWithXPath(xPath : InitialDocReaderAppPaneXPath,timeout:GlobalTimeoutInSeconds).Click(); 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 : "Button:NetUIStickyButton", title : "Open",timeout:GlobalTimeoutInSeconds).Click(); MainWindow.FindControl(title : "Microsoft Print to PDF",timeout:GlobalTimeoutInSeconds).Click(); Wait(WaitSeconds); MainWindow.FindControl(className : "Button:NetUISimpleButton", 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); // Wait for the Sumatra PDF window to appear var PDFReaderApp = FindWindow(title: OutputFileName, timeout: GlobalTimeoutInSeconds); // Wait for the Sumatra PDF window to become responsive // Replace the xPath parameter with the xPath of a Control that will be present once the application has loaded PDFReaderApp.FindControlWithXPath(xPath: PDFReaderAppPaneXPath, timeout: GlobalTimeoutInSeconds); StopTimer(name:"OpenPDFFile"); // This will close out of the app PDFReaderApp.Focus(); Wait(WaitSeconds); KeyDown(KeyCode.ALT); Type("{F4}"); KeyUp(KeyCode.ALT); Wait(WaitSeconds); } }