1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
public class WordProcessing {
// define your standard:
private static final boolean noteNotMatchingBookmarks = false; // shall the user be informed that certain bookmarks weren't found in the template?
// end of standard definition
private static File wordInput;
private static FileWriter wordInputWriter;
/**
* Triggers to the template selection dialog and creates a new document
* based on the chosen template.
*/
public static void createNewDocumentFromTemplateToSelectByUser() {
output("@createNewDocumentFromTemplate", "TEMPLATE_TO_SELECT_BY_USER");
}
/**
* Creates a new document based on the desired template.
* @param templateName the name of the template to be used
*/
public static void createNewDocumentFromTemplate(String templateName) {
output("@createNewDocumentFromTemplate", templateName);
}
/**
* Set the warning flag about not matching bookmarks
* Decides whether the user shall be informed that the template didn't
* include certain bookmarks.
* @param noteNotMatchingBookmarks whether the user should be warned
*/
public static void setNoteNotMatchingBookmarks(boolean noteNotMatchingBookmarks) {
if (noteNotMatchingBookmarks) output("@noteNotMatchingBookmarks", "TRUE");
else output("@noteNotMatchingBookmarks", "FALSE");
}
/**
* Goes to the specified bookmark and types the desired text.
* @param bookmark the bookmark where text type starts
* @param textToType the text to be included
*/
public static void typeTextAtBookmark(String bookmark, String textToType) {
output(bookmark, textToType);
}
/**
* Goes to the specified bookmark and types the desired text with line feed.
* @param linesToType the lines to be included
*/
public static void typeTextAtBookmark(String bookmark, String[] linesToType) {
StringBuffer textToType = new StringBuffer();
for (int i=0; i < linesToType.length; i++) {
textToType.append(linesToType[i] + "\n");
}
output(bookmark, new String(textToType));
}
/**
* Sets the document directory for future document saving.
* @param documentDirectory the name of the directory
*/
public static void changeDocumentDirectory(String documentDirectory) {
output("@changeDocumentDirectory", documentDirectory);
}
/**
* Saves the active document using the indicated name
* (usually without extension).
* @param documentName the name of the document
*/
public static void saveDocumentAs(String documentName) {
output("@saveDocumentAs", documentName);
}
/**
* Saves the active document using the indicated name and closes it.
* @param documentName the name of the document
*/
public static void saveDocumentAsAndClose(String documentName) {
output("@saveDocumentAsAndClose", documentName);
}
/**
* Closes the active document.
*/
public static void closeDocument() {
output("@closeDocument", "");
}
/**
* Prints the document on the standard printer and closes the document without saving.
*/
public static void printAndForget() {
output("@printAndForget", "");
}
/**
* Prints the document on the specified printer and closes the document without saving.
* @param printerName the name of the desired printer
*/
public static void printAndForget(String printerName) {
output("@printAndForget", printerName);
}
/**
* Triggers to the printer selection dialog, prints the document
* on the selected printer and closes the document without saving.
*/
public static void printToPrinterToSelectByUserAndForget() {
output("@printAndForget", "PRINTER_TO_SELECT_BY_USER");
}
/**
* Executes an arbitrary WordBasic macro.
* @param macroName the name of the macro to be executed
*/
public static void executeMacro(String macroName) {
output("@executeMacro", macroName);
}
/**
* Quits the word processing application.
*/
public static void quitApplication() {
output("@quitApplication", "");
}
/**
* Quits the word processing application after a pause.
* This gives the word processing time to finish e.g. a print job.
* This avoids dialogs by the word processing system wether the print job is to stop
*/
public static void quitApplicationAfterWaiting(int milliseconds) {
output("@quitApplicationAfterWaiting", String.valueOf(milliseconds));
}
/**
* Starts the execution of the above instructions.
* (This stacking is particularly helpful at large numbers of standard letters.)
* Always use use this as the last method of a sequence.
*/
public static boolean exec() {
closeWordInput();
String cmd = "c:/WordProcessing/WordAPI.exe";
try {
Runtime run = Runtime.getRuntime();
Process p = run.exec(cmd);
p.waitFor();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(cmd + " could not be executed.");
System.out.println("Please ensure that WordAPI.exe may be found by java.exe by putting it in an appropriate directory.");
return false;
}
return true;
}
/**
* Annule le traitement en cours.
*/
public static void cancel() {
if (wordInputWriter != null) closeWordInput();
}
/**
* Génération et initialisation d'une commande Word.
* @param key : commande.
* @param value : paramètre de la commande.
*/
private static void output(String key, String value) {
String record;
record = key;
while (record.length() < 40) record += " ";
record += value;
if (wordInputWriter == null) {
if (!openWordInput()){
return;
}
}
try {
wordInputWriter.write(record + "\r\n");
wordInputWriter.flush();
}
catch ( IOException e2 ) {
System.out.println( "caught: " + e2 );
}
}
private static boolean openWordInput() {
try {
/*Attention a ne pas changer le répertoire de location de WordInp.txt.*/
wordInput = new File("WordInp.txt");
wordInputWriter = new FileWriter(wordInput);
setNoteNotMatchingBookmarks(noteNotMatchingBookmarks);
}
catch ( IOException e2 ) {
System.out.println( "caught: " + e2 );
System.out.println("could not open interface file WordInp.txt");
return false;
}
return true;
}
private static void closeWordInput() {
try {
wordInputWriter.close();
wordInput = null;
wordInputWriter = null;
}
catch ( IOException e2 ) {
System.out.println( "caught: " + e2 );
}
}
} |
Partager