1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package convwatch; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import convwatch.FileHelper; 27cdf0e10cSrcweir import java.io.File; 28cdf0e10cSrcweir import java.io.FileWriter; 29cdf0e10cSrcweir import java.io.RandomAccessFile; 30cdf0e10cSrcweir import convwatch.GraphicalTestArguments; 31cdf0e10cSrcweir import helper.ProcessHandler; 32cdf0e10cSrcweir import java.util.ArrayList; 33cdf0e10cSrcweir import helper.OSHelper; 34cdf0e10cSrcweir 35cdf0e10cSrcweir /** 36cdf0e10cSrcweir * This object gives all functionallity to print msoffice documents. 37cdf0e10cSrcweir * It also offers functions to check what type of document it is. 38cdf0e10cSrcweir * It handles *.doc as word documents and use word to print 39cdf0e10cSrcweir * *.xls as excel 40cdf0e10cSrcweir * *.ppt as powerpoint 41cdf0e10cSrcweir */ 42cdf0e10cSrcweir 43cdf0e10cSrcweir class ProcessHelper 44cdf0e10cSrcweir { 45cdf0e10cSrcweir ArrayList m_aArray; 46cdf0e10cSrcweir } 47cdf0e10cSrcweir 48cdf0e10cSrcweir public class MSOfficePrint 49cdf0e10cSrcweir { 50cdf0e10cSrcweir private String m_sPrinterName; // within Windows the tools need a printer name; 51cdf0e10cSrcweir setPrinterName(String _s)52cdf0e10cSrcweir public void setPrinterName(String _s) {m_sPrinterName = _s;} 53cdf0e10cSrcweir 54cdf0e10cSrcweir // ----------------------------------------------------------------------------- isWordDocument(String _sSuffix)55cdf0e10cSrcweir static boolean isWordDocument(String _sSuffix) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir if (_sSuffix.toLowerCase().endsWith(".doc") || 58cdf0e10cSrcweir _sSuffix.toLowerCase().endsWith(".rtf") || 59cdf0e10cSrcweir _sSuffix.toLowerCase().endsWith(".dot")) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir return true; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir return false; 64cdf0e10cSrcweir } 65cdf0e10cSrcweir isExcelDocument(String _sSuffix)66cdf0e10cSrcweir static boolean isExcelDocument(String _sSuffix) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir // xlt templates 69cdf0e10cSrcweir // xlw 70cdf0e10cSrcweir // xla addin 71cdf0e10cSrcweir if (_sSuffix.toLowerCase().endsWith(".xls")) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir return true; 74cdf0e10cSrcweir } 75cdf0e10cSrcweir /* temporal insertion by SUS 76cdf0e10cSrcweir if (_sSuffix.endsWith(".xml")) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir return true; 79cdf0e10cSrcweir }*/ 80cdf0e10cSrcweir return false; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir isPowerPointDocument(String _sSuffix)83cdf0e10cSrcweir static boolean isPowerPointDocument(String _sSuffix) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir if (_sSuffix.toLowerCase().endsWith(".pps") || 86cdf0e10cSrcweir _sSuffix.toLowerCase().endsWith(".ppt")) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir return true; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir return false; 91cdf0e10cSrcweir } 92cdf0e10cSrcweir 93cdf0e10cSrcweir /** 94cdf0e10cSrcweir * returns true, if the given filename has a MS Office suffix. 95cdf0e10cSrcweir */ isMSOfficeDocumentFormat(String _sFile)96cdf0e10cSrcweir public static boolean isMSOfficeDocumentFormat(String _sFile) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir String sDocumentSuffix = FileHelper.getSuffix(_sFile); 99cdf0e10cSrcweir if (isWordDocument(sDocumentSuffix)) return true; 100cdf0e10cSrcweir if (isExcelDocument(sDocumentSuffix)) return true; 101cdf0e10cSrcweir if (isPowerPointDocument(sDocumentSuffix)) return true; 102cdf0e10cSrcweir // if suffix is xml, return also true, but we can't decide if word or excel 103cdf0e10cSrcweir if (sDocumentSuffix.toLowerCase().endsWith(".xml")) return true; 104cdf0e10cSrcweir return false; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir storeToFileWithMSOffice( GraphicalTestArguments _aGTA, String _sInputFile, String _sOutputFile)107cdf0e10cSrcweir public void storeToFileWithMSOffice( GraphicalTestArguments _aGTA, 108cdf0e10cSrcweir String _sInputFile, 109cdf0e10cSrcweir String _sOutputFile) throws ConvWatchCancelException, java.io.IOException 110cdf0e10cSrcweir { 111cdf0e10cSrcweir String sDocumentSuffix = FileHelper.getSuffix(_sInputFile); 112cdf0e10cSrcweir String sFilterName = _aGTA.getExportFilterName(); 113cdf0e10cSrcweir ArrayList aStartCommand = new ArrayList(); 114cdf0e10cSrcweir if (isWordDocument(sDocumentSuffix)) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir aStartCommand = createWordStoreHelper(); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir else if (isExcelDocument(sDocumentSuffix)) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir aStartCommand = createExcelStoreHelper(); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir else if (isPowerPointDocument(sDocumentSuffix)) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir } 125cdf0e10cSrcweir else if (sDocumentSuffix.toLowerCase().equals(".xml")) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable. 128cdf0e10cSrcweir if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel")) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir aStartCommand = createExcelStoreHelper(); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir // else 133cdf0e10cSrcweir // { 134cdf0e10cSrcweir // } 135cdf0e10cSrcweir } 136cdf0e10cSrcweir else 137cdf0e10cSrcweir { 138cdf0e10cSrcweir GlobalLogWriter.get().println("No Microsoft Office document format found."); 139cdf0e10cSrcweir // TODO: use a better Exception!!! 140cdf0e10cSrcweir throw new ConvWatchCancelException/*WrongSuffixException*/("No MS office document format found."); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir if (aStartCommand != null) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir if (sFilterName == null) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir // TODO: hardcoded FilterName in perl script 147cdf0e10cSrcweir sFilterName = ""; // xlXMLSpreadsheet"; 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir // String sCommand = sStartCommand + " " + 151cdf0e10cSrcweir // _sInputFile + " " + 152cdf0e10cSrcweir // StringHelper.doubleQuote(sFilterName) + " " + 153cdf0e10cSrcweir // _sOutputFile; 154cdf0e10cSrcweir 155cdf0e10cSrcweir aStartCommand.add(_sInputFile); 156cdf0e10cSrcweir aStartCommand.add(sFilterName); 157cdf0e10cSrcweir aStartCommand.add(_sOutputFile); 158cdf0e10cSrcweir realStartCommand(aStartCommand); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir // ----------------------------------------------------------------------------- 163cdf0e10cSrcweir /** 164cdf0e10cSrcweir * print the given file (_sInputFile) to the file name (_sPrintFile) 165cdf0e10cSrcweir */ printToFileWithMSOffice( GraphicalTestArguments _aGTA, String _sInputFile, String _sPrintFilename)166cdf0e10cSrcweir public void printToFileWithMSOffice( GraphicalTestArguments _aGTA, 167cdf0e10cSrcweir String _sInputFile, 168cdf0e10cSrcweir String _sPrintFilename) throws ConvWatchCancelException, java.io.IOException 169cdf0e10cSrcweir { 170cdf0e10cSrcweir String sDocumentSuffix = FileHelper.getSuffix(_sInputFile); 171cdf0e10cSrcweir 172cdf0e10cSrcweir setPrinterName(_aGTA.getPrinterName()); 173cdf0e10cSrcweir 174cdf0e10cSrcweir ArrayList aStartCommand = new ArrayList(); 175cdf0e10cSrcweir if (isWordDocument(sDocumentSuffix)) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir aStartCommand = createWordPrintHelper(); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir else if (isExcelDocument(sDocumentSuffix)) 180cdf0e10cSrcweir { 181cdf0e10cSrcweir aStartCommand = createExcelPrintHelper(); 182cdf0e10cSrcweir } 183cdf0e10cSrcweir else if (isPowerPointDocument(sDocumentSuffix)) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir aStartCommand = createPowerPointPrintHelper(); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir else if (sDocumentSuffix.toLowerCase().equals(".xml")) 188cdf0e10cSrcweir { 189cdf0e10cSrcweir // TODO: Open XML File and check if we need excel or word 190cdf0e10cSrcweir String sOfficeType = getOfficeType(_sInputFile); 191cdf0e10cSrcweir 192cdf0e10cSrcweir // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable. 193cdf0e10cSrcweir // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel")) 194cdf0e10cSrcweir if (sOfficeType.equals("excel")) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir aStartCommand = createExcelPrintHelper(); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir else if (sOfficeType.equals("word")) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir aStartCommand = createWordPrintHelper(); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir else 203cdf0e10cSrcweir { 204cdf0e10cSrcweir return; 205cdf0e10cSrcweir } 206cdf0e10cSrcweir } 207cdf0e10cSrcweir else 208cdf0e10cSrcweir { 209cdf0e10cSrcweir GlobalLogWriter.get().println("No Microsoft Office document format found."); 210cdf0e10cSrcweir // TODO: use a better Exception!!! 211cdf0e10cSrcweir throw new ConvWatchCancelException/*WrongSuffixException*/("No Mircosoft Office document format found."); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir 214cdf0e10cSrcweir if (aStartCommand.isEmpty() == false) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir String sPrinterName = m_sPrinterName; 217cdf0e10cSrcweir if (sPrinterName == null) 218cdf0e10cSrcweir { 219cdf0e10cSrcweir sPrinterName = ""; 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222cdf0e10cSrcweir // String sCommand = sStartCommand + " " + 223cdf0e10cSrcweir // _sInputFile + " " + 224cdf0e10cSrcweir // StringHelper.doubleQuote(m_sPrinterName) + " " + 225cdf0e10cSrcweir // _sPrintFilename; 226cdf0e10cSrcweir aStartCommand.add(_sInputFile); 227cdf0e10cSrcweir aStartCommand.add(m_sPrinterName); 228cdf0e10cSrcweir aStartCommand.add(_sPrintFilename); 229cdf0e10cSrcweir 230cdf0e10cSrcweir realStartCommand(aStartCommand); 231cdf0e10cSrcweir } 232cdf0e10cSrcweir String sUserDir = System.getProperty("user.home"); 233cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 234cdf0e10cSrcweir if (! sUserDir.endsWith(fs)) 235cdf0e10cSrcweir { 236cdf0e10cSrcweir sUserDir = sUserDir + fs; 237cdf0e10cSrcweir } 238cdf0e10cSrcweir _aGTA.getPerformance().readWordValuesFromFile(sUserDir + "msofficeloadtimes.txt"); 239cdf0e10cSrcweir OfficePrint.createInfoFile(_sPrintFilename, _aGTA, "msoffice"); 240cdf0e10cSrcweir TimeHelper.waitInSeconds(2, "Give Microsoft Office some time to print."); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir realStartCommand(ArrayList _aStartCommand)243cdf0e10cSrcweir public void realStartCommand(ArrayList _aStartCommand) throws ConvWatchCancelException 244cdf0e10cSrcweir { 245cdf0e10cSrcweir if (_aStartCommand.isEmpty()) 246cdf0e10cSrcweir { 247cdf0e10cSrcweir throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is empty."); 248cdf0e10cSrcweir } 249cdf0e10cSrcweir 250cdf0e10cSrcweir try 251cdf0e10cSrcweir { 252cdf0e10cSrcweir // Convert the StartCommand ArrayList to a String List 253cdf0e10cSrcweir int nValues = _aStartCommand.size(); 254cdf0e10cSrcweir String[] aList = new String[nValues]; 255cdf0e10cSrcweir for (int i=0;i<nValues;i++) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir String aStr = (String) _aStartCommand.get(i); 258cdf0e10cSrcweir if (aStr == null) 259cdf0e10cSrcweir { 260cdf0e10cSrcweir aStr = ""; 261cdf0e10cSrcweir } 262cdf0e10cSrcweir if (aStr.length() == 0) 263cdf0e10cSrcweir { 264cdf0e10cSrcweir aStr = "\"\""; 265cdf0e10cSrcweir } 266cdf0e10cSrcweir aList[i] = new String(aStr); 267cdf0e10cSrcweir } 268cdf0e10cSrcweir 269cdf0e10cSrcweir // This is really the latest point where we can check if we are running within windows environment 270cdf0e10cSrcweir if (! OSHelper.isWindows()) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir // TODO: use a better Exception!!! 273cdf0e10cSrcweir throw new ConvWatchCancelException/*WrongEnvironmentException*/("We doesn't work within windows environment."); 274cdf0e10cSrcweir } 275cdf0e10cSrcweir 276cdf0e10cSrcweir 277cdf0e10cSrcweir ProcessHandler aHandler = new ProcessHandler(aList); 278cdf0e10cSrcweir boolean bBackValue = aHandler.executeSynchronously(); 279cdf0e10cSrcweir } 280cdf0e10cSrcweir catch (IndexOutOfBoundsException e) 281cdf0e10cSrcweir { 282cdf0e10cSrcweir throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is too short."); 283cdf0e10cSrcweir } 284cdf0e10cSrcweir 285cdf0e10cSrcweir // return aHandler.getExitCode(); 286cdf0e10cSrcweir } 287cdf0e10cSrcweir 288cdf0e10cSrcweir createWordPrintHelper()289cdf0e10cSrcweir ArrayList createWordPrintHelper() throws java.io.IOException 290cdf0e10cSrcweir { 291cdf0e10cSrcweir // create a program in tmp file 292cdf0e10cSrcweir String sTmpPath = util.utils.getUsersTempDir(); 293cdf0e10cSrcweir String ls = System.getProperty("line.separator"); 294cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 295cdf0e10cSrcweir 296cdf0e10cSrcweir String sPrintViaWord = "printViaWord.pl"; 297cdf0e10cSrcweir 298cdf0e10cSrcweir ArrayList aList = searchLocalFile(sPrintViaWord); 299cdf0e10cSrcweir if (aList.isEmpty() == false) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir return aList; 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir String sName = sTmpPath + fs + sPrintViaWord; 305cdf0e10cSrcweir File aFile = new File(sName); 306cdf0e10cSrcweir FileWriter out = new FileWriter(aFile.toString()); 307cdf0e10cSrcweir 308cdf0e10cSrcweir 309cdf0e10cSrcweir out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls ); 310cdf0e10cSrcweir out.write( " if 0; " + ls ); 311cdf0e10cSrcweir out.write( "use strict; " + ls ); 312cdf0e10cSrcweir out.write( "use Time::HiRes; " + ls ); 313cdf0e10cSrcweir out.write( "if ( $^O ne \"MSWin32\") " + ls ); 314cdf0e10cSrcweir out.write( "{ " + ls ); 315cdf0e10cSrcweir out.write( " print 'Windows only.\\n'; " + ls ); 316cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 317cdf0e10cSrcweir out.write( " exit(1); " + ls ); 318cdf0e10cSrcweir out.write( "} " + ls ); 319cdf0e10cSrcweir out.write( " " + ls ); 320cdf0e10cSrcweir out.write( "use Win32::OLE; " + ls ); 321cdf0e10cSrcweir out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls ); 322cdf0e10cSrcweir out.write( " " + ls ); 323cdf0e10cSrcweir out.write( "# ------ usage ------ " + ls ); 324cdf0e10cSrcweir out.write( "sub print_usage() " + ls ); 325cdf0e10cSrcweir out.write( "{ " + ls ); 326cdf0e10cSrcweir out.write( " print STDERR \"Usage: word_print.pl <Word file> <name of printer> <output file> .\\n " + ls ); 327cdf0e10cSrcweir out.write( " Please use the same string for the name of the printer as you can find \\n " + ls ); 328cdf0e10cSrcweir out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls ); 329cdf0e10cSrcweir out.write( " The name could look like the the following line: \\n " + ls ); 330cdf0e10cSrcweir out.write( " Apple LaserWriter II NT v47.0 \\n " + ls ); 331cdf0e10cSrcweir out.write( " Sample command line: \\n " + ls ); 332cdf0e10cSrcweir out.write( " execl_print.pl c:\\book1.doc Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls ); 333cdf0e10cSrcweir out.write( "} " + ls ); 334cdf0e10cSrcweir out.write( " " + ls ); 335cdf0e10cSrcweir out.write( " " + ls ); 336cdf0e10cSrcweir out.write( "if ($#ARGV != 2) " + ls ); 337cdf0e10cSrcweir out.write( "{ " + ls ); 338cdf0e10cSrcweir out.write( " print 'Too less arguments.\\n'; " + ls ); 339cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 340cdf0e10cSrcweir out.write( " exit(1); " + ls ); 341cdf0e10cSrcweir out.write( "} " + ls ); 342cdf0e10cSrcweir out.write( " " + ls ); 343cdf0e10cSrcweir out.write( "my $startWordTime = Time::HiRes::time(); " + ls ); 344cdf0e10cSrcweir out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls ); 345cdf0e10cSrcweir out.write( "my $stopWordTime = Time::HiRes::time() - $startWordTime; " + ls ); 346cdf0e10cSrcweir out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls ); 347cdf0e10cSrcweir out.write( "# , ReadOnly => 1})" + ls ); 348cdf0e10cSrcweir out.write(ls); 349cdf0e10cSrcweir out.write( "my $startLoadWordTime = Time::HiRes::time(); " + ls ); 350cdf0e10cSrcweir out.write( "$Word->Documents->Open({Filename => $ARGV[0]}) " + ls ); 351cdf0e10cSrcweir out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls ); 352cdf0e10cSrcweir out.write( "my $stopLoadWordTime = Time::HiRes::time() - $startLoadWordTime; " + ls ); 353cdf0e10cSrcweir out.write(ls); 354cdf0e10cSrcweir out.write( "my $startPrintWordTime = Time::HiRes::time(); " + ls); 355cdf0e10cSrcweir out.write( "my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls ); 356cdf0e10cSrcweir out.write( "$Word->{ActivePrinter} = $ARGV[1]; " + ls ); 357cdf0e10cSrcweir out.write( "$Word->ActiveDocument->PrintOut({ " + ls ); 358cdf0e10cSrcweir out.write( " Background => 0, " + ls ); 359cdf0e10cSrcweir out.write( " Append => 0, " + ls ); 360cdf0e10cSrcweir out.write( " Range => wdPrintAllDocument, " + ls ); 361cdf0e10cSrcweir out.write( " Item => wdPrintDocumentContent, " + ls ); 362cdf0e10cSrcweir out.write( " Copies => 1, " + ls ); 363cdf0e10cSrcweir out.write( " PageType => wdPrintAllPages, " + ls ); 364cdf0e10cSrcweir out.write( " PrintToFile => 1, " + ls ); 365cdf0e10cSrcweir out.write( " OutputFileName => $ARGV[2] " + ls ); 366cdf0e10cSrcweir out.write( " }); " + ls ); 367cdf0e10cSrcweir out.write( "$Word->{ActivePrinter} = $oldActivePrinte; " + ls ); 368cdf0e10cSrcweir out.write( "my $stopPrintWordTime = Time::HiRes::time() - $startPrintWordTime;" + ls); 369cdf0e10cSrcweir 370cdf0e10cSrcweir out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls ); 371cdf0e10cSrcweir out.write( "my $sVersion = $Word->Application->Version();"+ls); 372cdf0e10cSrcweir out.write( "$Word->ActiveDocument->Close({SaveChanges => 0}); " + ls ); 373cdf0e10cSrcweir out.write( "$Word->Quit(); " + ls ); 374cdf0e10cSrcweir 375cdf0e10cSrcweir out.write( "local *FILE;" + ls); 376cdf0e10cSrcweir out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls); 377cdf0e10cSrcweir out.write( "{" + ls); 378cdf0e10cSrcweir out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls); 379cdf0e10cSrcweir out.write( " print FILE \"WordVersion=$sVersion\\n\";" + ls); 380cdf0e10cSrcweir out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls); 381cdf0e10cSrcweir out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls); 382cdf0e10cSrcweir out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls); 383cdf0e10cSrcweir out.write( " close(FILE);" + ls); 384cdf0e10cSrcweir out.write( "}" + ls); 385cdf0e10cSrcweir out.close(); 386cdf0e10cSrcweir 387cdf0e10cSrcweir aList.add("perl"); 388cdf0e10cSrcweir aList.add(sName); 389cdf0e10cSrcweir return aList; 390cdf0e10cSrcweir } 391cdf0e10cSrcweir 392cdf0e10cSrcweir // TODO: Maybe give a possibility to say where search the script from outside 393cdf0e10cSrcweir searchLocalFile(String _sScriptName)394cdf0e10cSrcweir ArrayList searchLocalFile(String _sScriptName) 395cdf0e10cSrcweir { 396cdf0e10cSrcweir String userdir = System.getProperty("user.dir"); 397cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 398cdf0e10cSrcweir 399cdf0e10cSrcweir ArrayList aList = new ArrayList(); 400cdf0e10cSrcweir File aPerlScript = new File(userdir + fs + _sScriptName); 401cdf0e10cSrcweir if (FileHelper.isDebugEnabled()) 402cdf0e10cSrcweir { 403cdf0e10cSrcweir GlobalLogWriter.get().println("Search for local existance of " + aPerlScript.getAbsolutePath()); 404cdf0e10cSrcweir } 405cdf0e10cSrcweir 406cdf0e10cSrcweir if (aPerlScript.exists()) 407cdf0e10cSrcweir { 408cdf0e10cSrcweir if (FileHelper.isDebugEnabled()) 409cdf0e10cSrcweir { 410cdf0e10cSrcweir GlobalLogWriter.get().println("OK, found it, use this instead the internal one."); 411cdf0e10cSrcweir } 412cdf0e10cSrcweir 413cdf0e10cSrcweir String sName = aPerlScript.getAbsolutePath(); 414cdf0e10cSrcweir // String sCommand = "perl " + sName; 415cdf0e10cSrcweir // System.out.println(sCommand); 416cdf0e10cSrcweir aList.add("perl"); 417cdf0e10cSrcweir aList.add(sName); 418cdf0e10cSrcweir return aList; 419cdf0e10cSrcweir } 420cdf0e10cSrcweir return aList; 421cdf0e10cSrcweir } 422cdf0e10cSrcweir createWordStoreHelper()423cdf0e10cSrcweir ArrayList createWordStoreHelper() throws java.io.IOException 424cdf0e10cSrcweir { 425cdf0e10cSrcweir // create a program in tmp file 426cdf0e10cSrcweir String sTmpPath = util.utils.getUsersTempDir(); 427cdf0e10cSrcweir String ls = System.getProperty("line.separator"); 428cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 429cdf0e10cSrcweir 430cdf0e10cSrcweir // ArrayList aList = new ArrayList(); 431cdf0e10cSrcweir String sSaveViaWord = "saveViaWord.pl"; 432cdf0e10cSrcweir 433cdf0e10cSrcweir ArrayList aList = searchLocalFile(sSaveViaWord); 434cdf0e10cSrcweir if (aList.isEmpty() == false) 435cdf0e10cSrcweir { 436cdf0e10cSrcweir return aList; 437cdf0e10cSrcweir } 438cdf0e10cSrcweir 439cdf0e10cSrcweir String sName = sTmpPath + fs + sSaveViaWord; 440cdf0e10cSrcweir if (FileHelper.isDebugEnabled()) 441cdf0e10cSrcweir { 442cdf0e10cSrcweir GlobalLogWriter.get().println("No local found, create a perl script: " + sName); 443cdf0e10cSrcweir } 444cdf0e10cSrcweir 445cdf0e10cSrcweir File aFile = new File(sName); 446cdf0e10cSrcweir FileWriter out = new FileWriter(aFile.toString()); 447cdf0e10cSrcweir 448cdf0e10cSrcweir out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls ); 449cdf0e10cSrcweir out.write( " if 0; " + ls ); 450cdf0e10cSrcweir out.write( "use strict; " + ls ); 451cdf0e10cSrcweir out.write( " " + ls ); 452cdf0e10cSrcweir out.write( "if ( $^O ne \"MSWin32\") " + ls ); 453cdf0e10cSrcweir out.write( "{ " + ls ); 454cdf0e10cSrcweir out.write( " print 'Windows only.\\n'; " + ls ); 455cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 456cdf0e10cSrcweir out.write( " exit(1); " + ls ); 457cdf0e10cSrcweir out.write( "} " + ls ); 458cdf0e10cSrcweir out.write( " " + ls ); 459cdf0e10cSrcweir out.write( "use Win32::OLE; " + ls ); 460cdf0e10cSrcweir out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls ); 461cdf0e10cSrcweir out.write( " " + ls ); 462cdf0e10cSrcweir out.write( "# ------ usage ------ " + ls ); 463cdf0e10cSrcweir out.write( "sub print_usage() " + ls ); 464cdf0e10cSrcweir out.write( "{ " + ls ); 465cdf0e10cSrcweir out.write( " print STDERR \"Usage: storeViaWord.pl <Word file> <output filer> <output file> \\n\" " + ls ); 466cdf0e10cSrcweir out.write( "} " + ls ); 467cdf0e10cSrcweir out.write( " " + ls ); 468cdf0e10cSrcweir out.write( " " + ls ); 469cdf0e10cSrcweir out.write( "if ($#ARGV != 2) " + ls ); 470cdf0e10cSrcweir out.write( "{ " + ls ); 471cdf0e10cSrcweir out.write( " print 'Too less arguments.\\n'; " + ls ); 472cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 473cdf0e10cSrcweir out.write( " exit(1); " + ls ); 474cdf0e10cSrcweir out.write( "} " + ls ); 475cdf0e10cSrcweir out.write( " " + ls ); 476cdf0e10cSrcweir out.write( " " + ls ); 477cdf0e10cSrcweir out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls ); 478cdf0e10cSrcweir out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls ); 479cdf0e10cSrcweir out.write( "my $Book = $Word->Documents->Open($ARGV[0]) " + ls ); 480cdf0e10cSrcweir out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls ); 481cdf0e10cSrcweir out.write( "# my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls ); 482cdf0e10cSrcweir out.write( "# $Word->{ActivePrinter} = $ARGV[1]; " + ls ); 483cdf0e10cSrcweir out.write( "# $Word->ActiveDocument->PrintOut({ " + ls ); 484cdf0e10cSrcweir out.write( "# Background => 0, " + ls ); 485cdf0e10cSrcweir out.write( "# Append => 0, " + ls ); 486cdf0e10cSrcweir out.write( "# Range => wdPrintAllDocument, " + ls ); 487cdf0e10cSrcweir out.write( "# Item => wdPrintDocumentContent, " + ls ); 488cdf0e10cSrcweir out.write( "# Copies => 1, " + ls ); 489cdf0e10cSrcweir out.write( "# PageType => wdPrintAllPages, " + ls ); 490cdf0e10cSrcweir out.write( "# PrintToFile => 1, " + ls ); 491cdf0e10cSrcweir out.write( "# OutputFileName => $ARGV[2] " + ls ); 492cdf0e10cSrcweir out.write( "# }); " + ls ); 493cdf0e10cSrcweir out.write( "# $Word->{ActivePrinter} = $oldActivePrinte; " + ls ); 494cdf0e10cSrcweir out.write( "$Book->savaAs($ARGV[2], $ARGV[1]); " + ls ); 495cdf0e10cSrcweir out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls ); 496cdf0e10cSrcweir out.write( "$Book->Close({SaveChanges => 0}); " + ls ); 497cdf0e10cSrcweir out.write( "$Word->Quit(); " + ls ); 498cdf0e10cSrcweir out.close(); 499cdf0e10cSrcweir 500cdf0e10cSrcweir aList.add("perl"); 501cdf0e10cSrcweir aList.add(sName); 502cdf0e10cSrcweir return aList; 503cdf0e10cSrcweir } 504cdf0e10cSrcweir 505cdf0e10cSrcweir createExcelPrintHelper()506cdf0e10cSrcweir ArrayList createExcelPrintHelper() throws java.io.IOException 507cdf0e10cSrcweir { 508cdf0e10cSrcweir // create a program in tmp file 509cdf0e10cSrcweir String sTmpPath = util.utils.getUsersTempDir(); 510cdf0e10cSrcweir String ls = System.getProperty("line.separator"); 511cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 512cdf0e10cSrcweir 513cdf0e10cSrcweir String sPrintViaExcel = "printViaExcel.pl"; 514cdf0e10cSrcweir 515cdf0e10cSrcweir ArrayList aList = searchLocalFile(sPrintViaExcel); 516cdf0e10cSrcweir if (aList.isEmpty() == false) 517cdf0e10cSrcweir { 518cdf0e10cSrcweir return aList; 519cdf0e10cSrcweir } 520cdf0e10cSrcweir String sName = sTmpPath + fs + sPrintViaExcel; 521cdf0e10cSrcweir if (FileHelper.isDebugEnabled()) 522cdf0e10cSrcweir { 523cdf0e10cSrcweir GlobalLogWriter.get().println("No local found, create a perl script: " + sName); 524cdf0e10cSrcweir } 525cdf0e10cSrcweir 526cdf0e10cSrcweir File aFile = new File(sName); 527cdf0e10cSrcweir FileWriter out = new FileWriter(aFile.toString()); 528cdf0e10cSrcweir 529cdf0e10cSrcweir out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls ); 530cdf0e10cSrcweir out.write( " if 0; " + ls ); 531cdf0e10cSrcweir out.write( "use strict; " + ls ); 532cdf0e10cSrcweir out.write( " " + ls ); 533cdf0e10cSrcweir out.write( "if ( $^O ne \"MSWin32\") " + ls ); 534cdf0e10cSrcweir out.write( "{ " + ls ); 535cdf0e10cSrcweir out.write( " print \"Windows only.\\n\"; " + ls ); 536cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 537cdf0e10cSrcweir out.write( " exit(1); " + ls ); 538cdf0e10cSrcweir out.write( "} " + ls ); 539cdf0e10cSrcweir out.write( " " + ls ); 540cdf0e10cSrcweir out.write( " " + ls ); 541cdf0e10cSrcweir out.write( "use Win32::OLE qw(in with); " + ls ); 542cdf0e10cSrcweir out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls ); 543cdf0e10cSrcweir out.write( " " + ls ); 544cdf0e10cSrcweir out.write( "# ------ usage ------ " + ls ); 545cdf0e10cSrcweir out.write( "sub print_usage() " + ls ); 546cdf0e10cSrcweir out.write( "{ " + ls ); 547cdf0e10cSrcweir out.write( " print STDERR \"Usage: printViaExcel.pl <Excel file> <name of printer> <output file> .\\n " + ls ); 548cdf0e10cSrcweir out.write( " Please use the same string for the name of the printer as you can find \\n " + ls ); 549cdf0e10cSrcweir out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls ); 550cdf0e10cSrcweir out.write( " The name could look like the the following line: \\n " + ls ); 551cdf0e10cSrcweir out.write( " Apple LaserWriter II NT v47.0 \\n " + ls ); 552cdf0e10cSrcweir out.write( " Sample command line: \\n " + ls ); 553cdf0e10cSrcweir out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls ); 554cdf0e10cSrcweir out.write( "} " + ls ); 555cdf0e10cSrcweir out.write( " " + ls ); 556cdf0e10cSrcweir out.write( " " + ls ); 557cdf0e10cSrcweir out.write( " " + ls ); 558cdf0e10cSrcweir out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls ); 559cdf0e10cSrcweir out.write( " " + ls ); 560cdf0e10cSrcweir out.write( " " + ls ); 561cdf0e10cSrcweir out.write( "if ($#ARGV != 2) " + ls ); 562cdf0e10cSrcweir out.write( "{ " + ls ); 563cdf0e10cSrcweir out.write( " print STDERR \"Too less arguments.\\n\"; " + ls ); 564cdf0e10cSrcweir out.write( " print STDERR \"ARGV[0] $ARGV[0]\\n\"; " + ls ); 565cdf0e10cSrcweir out.write( " print STDERR \"ARGV[1] $ARGV[1]\\n\"; " + ls ); 566cdf0e10cSrcweir out.write( " print STDERR \"ARGV[2] $ARGV[2]\\n\"; " + ls ); 567cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 568cdf0e10cSrcweir out.write( " exit(1); " + ls ); 569cdf0e10cSrcweir out.write( "} " + ls ); 570cdf0e10cSrcweir out.write( " " + ls ); 571cdf0e10cSrcweir out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls ); 572cdf0e10cSrcweir out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls ); 573cdf0e10cSrcweir out.write( " # application or open new " + ls ); 574cdf0e10cSrcweir out.write( " " + ls ); 575cdf0e10cSrcweir out.write( " " + ls ); 576cdf0e10cSrcweir out.write( " " + ls ); 577cdf0e10cSrcweir out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls ); 578cdf0e10cSrcweir out.write( " $Book->PrintOut({Copies => 1, " + ls ); 579cdf0e10cSrcweir out.write( " ActivePrinter => $ARGV[1], " + ls ); 580cdf0e10cSrcweir out.write( " PrToFileName => $ARGV[2], " + ls ); 581cdf0e10cSrcweir out.write( " Collate => 1 " + ls ); 582cdf0e10cSrcweir out.write( " }); " + ls ); 583cdf0e10cSrcweir out.write( "# Close worksheets without store changes" + ls ); 584cdf0e10cSrcweir out.write( "# $Book->Close({SaveChanges => 0}); " + ls ); 585cdf0e10cSrcweir out.write( "my $sVersion = $Excel->Application->Version();"+ls); 586cdf0e10cSrcweir out.write( "$Excel->Quit(); " + ls ); 587cdf0e10cSrcweir out.write( "local *FILE;" + ls); 588cdf0e10cSrcweir out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls); 589cdf0e10cSrcweir out.write( "{" + ls); 590cdf0e10cSrcweir out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls); 591cdf0e10cSrcweir out.write( " print FILE \"ExcelVersion=$sVersion\\n\";" + ls); 592cdf0e10cSrcweir // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls); 593cdf0e10cSrcweir // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls); 594cdf0e10cSrcweir // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls); 595cdf0e10cSrcweir out.write( " close(FILE);" + ls); 596cdf0e10cSrcweir out.write( "}" + ls); 597cdf0e10cSrcweir out.close(); 598cdf0e10cSrcweir 599cdf0e10cSrcweir aList.add("perl"); 600cdf0e10cSrcweir aList.add(sName); 601cdf0e10cSrcweir return aList; 602cdf0e10cSrcweir } 603cdf0e10cSrcweir createExcelStoreHelper()604cdf0e10cSrcweir ArrayList createExcelStoreHelper() throws java.io.IOException 605cdf0e10cSrcweir { 606cdf0e10cSrcweir // create a program in tmp file 607cdf0e10cSrcweir String sTmpPath = util.utils.getUsersTempDir(); 608cdf0e10cSrcweir String ls = System.getProperty("line.separator"); 609cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 610cdf0e10cSrcweir 611cdf0e10cSrcweir String sSaveViaExcel = "saveViaExcel.pl"; 612cdf0e10cSrcweir 613cdf0e10cSrcweir ArrayList aList = searchLocalFile(sSaveViaExcel); 614cdf0e10cSrcweir if (aList.isEmpty() == false) 615cdf0e10cSrcweir { 616cdf0e10cSrcweir return aList; 617cdf0e10cSrcweir } 618cdf0e10cSrcweir String sName = sTmpPath + fs + sSaveViaExcel; 619cdf0e10cSrcweir if (FileHelper.isDebugEnabled()) 620cdf0e10cSrcweir { 621cdf0e10cSrcweir GlobalLogWriter.get().println("No local found, create a script: " + sName); 622cdf0e10cSrcweir } 623cdf0e10cSrcweir 624cdf0e10cSrcweir File aFile = new File(sName); 625cdf0e10cSrcweir FileWriter out = new FileWriter(aFile.toString()); 626cdf0e10cSrcweir 627cdf0e10cSrcweir out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls ); 628cdf0e10cSrcweir out.write( " if 0; " + ls ); 629cdf0e10cSrcweir out.write( "use strict; " + ls ); 630cdf0e10cSrcweir out.write( "# This script is automatically created. " + ls ); 631cdf0e10cSrcweir out.write( " " + ls ); 632cdf0e10cSrcweir out.write( "use Win32::OLE qw(in with); " + ls ); 633cdf0e10cSrcweir out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls ); 634cdf0e10cSrcweir out.write( " " + ls ); 635cdf0e10cSrcweir out.write( "# ------ usage ------ " + ls ); 636cdf0e10cSrcweir out.write( "sub print_usage() " + ls ); 637cdf0e10cSrcweir out.write( "{ " + ls ); 638cdf0e10cSrcweir out.write( " print STDERR \"Usage: savaViaExcel.pl <Excel file> <filefilter> <output file> .\\n " + ls ); 639cdf0e10cSrcweir out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls ); 640cdf0e10cSrcweir out.write( "} " + ls ); 641cdf0e10cSrcweir out.write( " " + ls ); 642cdf0e10cSrcweir out.write( " " + ls ); 643cdf0e10cSrcweir out.write( " " + ls ); 644cdf0e10cSrcweir out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls ); 645cdf0e10cSrcweir out.write( " " + ls ); 646cdf0e10cSrcweir out.write( " " + ls ); 647cdf0e10cSrcweir out.write( "if ($#ARGV != 2) " + ls ); 648cdf0e10cSrcweir out.write( "{ " + ls ); 649cdf0e10cSrcweir out.write( " print \"Too less arguments.\\n\"; " + ls ); 650cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 651cdf0e10cSrcweir out.write( " exit(1); " + ls ); 652cdf0e10cSrcweir out.write( "} " + ls ); 653cdf0e10cSrcweir out.write( " " + ls ); 654cdf0e10cSrcweir out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls ); 655cdf0e10cSrcweir out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls ); 656cdf0e10cSrcweir out.write( " # application or open new " + ls ); 657cdf0e10cSrcweir out.write( "my $sFilterParameter = $ARGV[1]; " + ls ); 658cdf0e10cSrcweir out.write( "my $sFilterName = xlHTML; " + ls ); 659cdf0e10cSrcweir out.write( "if ($sFilterParameter eq 'xlXMLSpreadsheet') " + ls ); 660cdf0e10cSrcweir out.write( "{ " + ls ); 661cdf0e10cSrcweir out.write( " $sFilterName = xlXMLSpreadsheet; " + ls ); 662cdf0e10cSrcweir out.write( "} " + ls ); 663cdf0e10cSrcweir out.write( "elsif ($sFilterParameter eq 'xlHTML') " + ls ); 664cdf0e10cSrcweir out.write( "{ " + ls ); 665cdf0e10cSrcweir out.write( " $sFilterName = xlHTML; " + ls ); 666cdf0e10cSrcweir out.write( "} " + ls ); 667cdf0e10cSrcweir out.write( "else " + ls ); 668cdf0e10cSrcweir out.write( "{ " + ls ); 669cdf0e10cSrcweir out.write( " my $undefined; " + ls); 670cdf0e10cSrcweir out.write( " $sFilterName = $undefined; " + ls ); 671cdf0e10cSrcweir out.write( "} " + ls ); 672cdf0e10cSrcweir out.write( " " + ls ); 673cdf0e10cSrcweir out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls ); 674cdf0e10cSrcweir out.write( "$Excel->{DisplayAlerts} = 0; " + ls ); 675cdf0e10cSrcweir out.write( "$Book->saveAs($ARGV[2], " + ls ); 676cdf0e10cSrcweir out.write( " $sFilterName, " + ls ); 677cdf0e10cSrcweir out.write( " '', " + ls ); 678cdf0e10cSrcweir out.write( " '', " + ls ); 679cdf0e10cSrcweir out.write( " 0, " + ls ); 680cdf0e10cSrcweir out.write( " 0, " + ls ); 681cdf0e10cSrcweir out.write( " xlNoChange, " + ls ); 682cdf0e10cSrcweir out.write( " xlLocalSessionChanges, " + ls ); 683cdf0e10cSrcweir out.write( " 1); " + ls ); 684cdf0e10cSrcweir out.write( "# Close worksheets without store changes" + ls ); 685cdf0e10cSrcweir out.write( "# $Book->Close({SaveChanges => 0}); " + ls ); 686cdf0e10cSrcweir out.write( "$Excel->Quit(); " + ls ); 687cdf0e10cSrcweir out.close(); 688cdf0e10cSrcweir 689cdf0e10cSrcweir aList.add("perl"); 690cdf0e10cSrcweir aList.add(sName); 691cdf0e10cSrcweir return aList; 692cdf0e10cSrcweir } 693cdf0e10cSrcweir createPowerPointPrintHelper()694cdf0e10cSrcweir ArrayList createPowerPointPrintHelper() throws java.io.IOException 695cdf0e10cSrcweir { 696cdf0e10cSrcweir // create a program in tmp file 697cdf0e10cSrcweir String sTmpPath = util.utils.getUsersTempDir(); 698cdf0e10cSrcweir String ls = System.getProperty("line.separator"); 699cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 700cdf0e10cSrcweir 701cdf0e10cSrcweir String sPrintViaPowerPoint = "printViaPowerPoint.pl"; 702cdf0e10cSrcweir 703cdf0e10cSrcweir ArrayList aList = searchLocalFile(sPrintViaPowerPoint); 704cdf0e10cSrcweir if (aList.isEmpty() == false) 705cdf0e10cSrcweir { 706cdf0e10cSrcweir return aList; 707cdf0e10cSrcweir } 708cdf0e10cSrcweir String sName = sTmpPath + fs + sPrintViaPowerPoint; 709cdf0e10cSrcweir if (FileHelper.isDebugEnabled()) 710cdf0e10cSrcweir { 711cdf0e10cSrcweir GlobalLogWriter.get().println("No local found, create a script: " + sName); 712cdf0e10cSrcweir } 713cdf0e10cSrcweir 714cdf0e10cSrcweir File aFile = new File(sName); 715cdf0e10cSrcweir FileWriter out = new FileWriter(aFile.toString()); 716cdf0e10cSrcweir 717cdf0e10cSrcweir 718cdf0e10cSrcweir out.write( "eval 'exec perl -wS $0 $1 $2 ' " + ls ); 719cdf0e10cSrcweir out.write( " if 0; " + ls ); 720cdf0e10cSrcweir out.write( "use strict; " + ls ); 721cdf0e10cSrcweir out.write( " " + ls ); 722cdf0e10cSrcweir out.write( "if ( $^O ne \"MSWin32\") " + ls ); 723cdf0e10cSrcweir out.write( "{ " + ls ); 724cdf0e10cSrcweir out.write( " print \"Windows only.\\n\"; " + ls ); 725cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 726cdf0e10cSrcweir out.write( " exit(1); " + ls ); 727cdf0e10cSrcweir out.write( "} " + ls ); 728cdf0e10cSrcweir out.write( " " + ls ); 729cdf0e10cSrcweir out.write( " " + ls ); 730cdf0e10cSrcweir out.write( "use Win32::OLE qw(in with); " + ls ); 731cdf0e10cSrcweir out.write( "use Win32::OLE::Const 'Microsoft PowerPoint'; " + ls ); 732cdf0e10cSrcweir out.write( " " + ls ); 733cdf0e10cSrcweir out.write( "# ------ usage ------ " + ls ); 734cdf0e10cSrcweir out.write( "sub print_usage() " + ls ); 735cdf0e10cSrcweir out.write( "{ " + ls ); 736cdf0e10cSrcweir out.write( " print STDERR \"Usage: powerpoint_print.pl <PowerPoint file> <name of printer> <output file> .\\n " + ls ); 737cdf0e10cSrcweir out.write( " Please use the same string for the name of the printer as you can find \\n " + ls ); 738cdf0e10cSrcweir out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls ); 739cdf0e10cSrcweir out.write( " The name could look like the the following line: \\n " + ls ); 740cdf0e10cSrcweir out.write( " Apple LaserWriter II NT v47.0 \\n " + ls ); 741cdf0e10cSrcweir out.write( " Sample command line: \\n " + ls ); 742cdf0e10cSrcweir out.write( " powerpoint_print.pl c:\\book.ppt Apple LaserWriter II NT v47.0 c:\\output\\book.ps \\n\"; " + ls ); 743cdf0e10cSrcweir out.write( "} " + ls ); 744cdf0e10cSrcweir out.write( " " + ls ); 745cdf0e10cSrcweir out.write( " " + ls ); 746cdf0e10cSrcweir out.write( " " + ls ); 747cdf0e10cSrcweir out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls ); 748cdf0e10cSrcweir out.write( " " + ls ); 749cdf0e10cSrcweir out.write( " " + ls ); 750cdf0e10cSrcweir out.write( "if ($#ARGV < 2) " + ls ); 751cdf0e10cSrcweir out.write( "{ " + ls ); 752cdf0e10cSrcweir out.write( " print \"Too less arguments.\\n\"; " + ls ); 753cdf0e10cSrcweir out.write( " print_usage(); " + ls ); 754cdf0e10cSrcweir out.write( " exit(1); " + ls ); 755cdf0e10cSrcweir out.write( "} " + ls ); 756cdf0e10cSrcweir out.write( " " + ls ); 757cdf0e10cSrcweir out.write( "my $PowerPoint = Win32::OLE->GetActiveObject('PowerPoint.Application') " + ls ); 758cdf0e10cSrcweir out.write( " || Win32::OLE->new('PowerPoint.Application', 'Quit'); # get already active Excel " + ls ); 759cdf0e10cSrcweir out.write( " # application or open new " + ls ); 760cdf0e10cSrcweir out.write( " " + ls ); 761cdf0e10cSrcweir out.write( " " + ls ); 762cdf0e10cSrcweir out.write( " " + ls ); 763cdf0e10cSrcweir out.write( " $PowerPoint->{'Visible'} = 1; " + ls ); 764cdf0e10cSrcweir out.write( " my $Presentation = $PowerPoint->Presentations->Add; " + ls ); 765cdf0e10cSrcweir out.write( " my $Presentation = $PowerPoint->Presentations->Open( $ARGV[0] ); " + ls ); 766cdf0e10cSrcweir out.write( "# we can't change active printer in powerpoint " + ls ); 767cdf0e10cSrcweir out.write( "# $Presentation->PrintOptions->{ActivePrinter} = $ARGV[1]; " + ls ); 768cdf0e10cSrcweir out.write( " print \"Active printer is: \" . $Presentation->PrintOptions->{ActivePrinter} . \"\\n\"; " + ls ); 769cdf0e10cSrcweir out.write( " $Presentation->PrintOptions->{PrintInBackground} = 0; " + ls ); 770cdf0e10cSrcweir out.write( " # PrintColorType = 1 means print in color and PrintColorType = 2 means print in gray " + ls ); 771cdf0e10cSrcweir out.write( " $Presentation->PrintOptions->{PrintColorType} = 1; " + ls ); 772cdf0e10cSrcweir out.write( " " + ls ); 773cdf0e10cSrcweir out.write( " $Presentation->PrintOut({PrintToFile => $ARGV[2]}); " + ls ); 774cdf0e10cSrcweir out.write( " sleep 5; " + ls ); 775cdf0e10cSrcweir out.write( " print \"Presentation has been printed\\n\"; " + ls ); 776cdf0e10cSrcweir out.write( "my $sVersion = $Presentation->Application->Version();"+ls); 777cdf0e10cSrcweir out.write( " $PowerPoint->Quit(); " + ls ); 778cdf0e10cSrcweir 779cdf0e10cSrcweir out.write( "local *FILE;" + ls); 780cdf0e10cSrcweir out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls); 781cdf0e10cSrcweir out.write( "{" + ls); 782cdf0e10cSrcweir out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls); 783cdf0e10cSrcweir out.write( " print FILE \"PowerPointVersion=$sVersion\\n\";" + ls); 784cdf0e10cSrcweir // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls); 785cdf0e10cSrcweir // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls); 786cdf0e10cSrcweir // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls); 787cdf0e10cSrcweir out.write( " close(FILE);" + ls); 788cdf0e10cSrcweir out.write( "}" + ls); 789cdf0e10cSrcweir out.close(); 790cdf0e10cSrcweir 791cdf0e10cSrcweir aList.add("perl"); 792cdf0e10cSrcweir aList.add(sName); 793cdf0e10cSrcweir return aList; 794cdf0e10cSrcweir } 795cdf0e10cSrcweir 796cdf0e10cSrcweir /** 797cdf0e10cSrcweir @param _sFilename a name to a ms office xml file 798cdf0e10cSrcweir @return 'word' or 'excel' or '' if type not known 799cdf0e10cSrcweir */ getOfficeType(String _sFilename)800cdf0e10cSrcweir public String getOfficeType(String _sFilename) 801cdf0e10cSrcweir { 802cdf0e10cSrcweir File aFile = new File(_sFilename); 803cdf0e10cSrcweir if (! aFile.exists()) 804cdf0e10cSrcweir { 805cdf0e10cSrcweir GlobalLogWriter.get().println("couldn't find file " + _sFilename); 806cdf0e10cSrcweir return ""; 807cdf0e10cSrcweir } 808cdf0e10cSrcweir RandomAccessFile aReader = null; 809cdf0e10cSrcweir String sOfficeType = ""; 810cdf0e10cSrcweir try 811cdf0e10cSrcweir { 812cdf0e10cSrcweir aReader = new RandomAccessFile(aFile,"r"); 813cdf0e10cSrcweir String aLine = ""; 814cdf0e10cSrcweir while (aLine != null) 815cdf0e10cSrcweir { 816cdf0e10cSrcweir aLine = aReader.readLine(); 817cdf0e10cSrcweir if (aLine != null) 818cdf0e10cSrcweir { 819cdf0e10cSrcweir aLine = aLine.trim(); 820cdf0e10cSrcweir if ( (! (aLine.length() < 2) ) && 821cdf0e10cSrcweir (! aLine.startsWith("#")) && 822cdf0e10cSrcweir (! aLine.startsWith(";")) ) 823cdf0e10cSrcweir { 824cdf0e10cSrcweir int nIdx = aLine.indexOf("mso-application"); 825cdf0e10cSrcweir if (nIdx > 0) 826cdf0e10cSrcweir { 827cdf0e10cSrcweir if (aLine.indexOf("Word.Document") > 0) 828cdf0e10cSrcweir { 829cdf0e10cSrcweir sOfficeType = "word"; 830cdf0e10cSrcweir } 831cdf0e10cSrcweir else if (aLine.indexOf("Excel") > 0) 832cdf0e10cSrcweir { 833cdf0e10cSrcweir sOfficeType = "excel"; 834cdf0e10cSrcweir } 835cdf0e10cSrcweir else 836cdf0e10cSrcweir { 837cdf0e10cSrcweir GlobalLogWriter.get().println("Unknown/unsupported data file: " + aLine); 838cdf0e10cSrcweir } 839cdf0e10cSrcweir } 840cdf0e10cSrcweir } 841cdf0e10cSrcweir } 842cdf0e10cSrcweir } 843cdf0e10cSrcweir } 844cdf0e10cSrcweir catch (java.io.FileNotFoundException fne) 845cdf0e10cSrcweir { 846cdf0e10cSrcweir System.out.println("couldn't open file " + _sFilename); 847cdf0e10cSrcweir System.out.println("Message: " + fne.getMessage()); 848cdf0e10cSrcweir } 849cdf0e10cSrcweir catch (java.io.IOException ie) 850cdf0e10cSrcweir { 851cdf0e10cSrcweir System.out.println("Exception while reading file " + _sFilename); 852cdf0e10cSrcweir System.out.println("Message: " + ie.getMessage()); 853cdf0e10cSrcweir } 854cdf0e10cSrcweir try 855cdf0e10cSrcweir { 856cdf0e10cSrcweir aReader.close(); 857cdf0e10cSrcweir } 858cdf0e10cSrcweir catch (java.io.IOException ie) 859cdf0e10cSrcweir { 860cdf0e10cSrcweir System.out.println("Couldn't close file " + _sFilename); 861cdf0e10cSrcweir System.out.println("Message: " + ie.getMessage()); 862cdf0e10cSrcweir } 863cdf0e10cSrcweir return sOfficeType; 864cdf0e10cSrcweir } 865cdf0e10cSrcweir 866cdf0e10cSrcweir } 867