2 // this program opens and closes PDF-files with Acrobat or Adobe Reader
5 {The problematic is the following:
6 A PDF-file should be modified while it is opened with Acrobat.
7 This is not possible because Acrobat understands itself as editor, not as
8 reader and therefore opens PDFs always with write access, so that other
9 programs cannot modifiy them.
10 The idea to solve the problem is the following:
11 The file that should be shown in Acrobat is copied and then renamed -
12 the suffix "-preview" is attached. The renamed copy is opened by Acrobat
13 while the unrenamed version can be modified. When the modified version should
14 be displayed, the eventually opened renamed version is closed in Acrobat and
15 the modified version is copied, renamed and opened in Acrobat.}
20 Windows, SysUtils, ShellApi;
22 var Input,InputNew : string;
27 function ExecWait(const CommandLine: string;
28 const Visible: boolean = false;
29 const MaxSeconds: integer = 60): boolean;
30 //Executes programs and waits until they are terminated
33 PI: TProcessInformation;
40 SI.dwFlags := STARTF_USESHOWWINDOW;
41 SI.wShowWindow := SW_HIDE;
43 if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, SI, PI)) then
45 case WaitForSingleObject(PI.hProcess, MaxSeconds * 1000) of
46 WAIT_OBJECT_0: GetExitCodeProcess(PI.hProcess, ExitCode);
47 WAIT_ABANDONED: TerminateProcess(PI.hProcess, ExitCode);
48 WAIT_TIMEOUT: TerminateProcess(PI.hProcess, ExitCode);
50 result := ExitCode = 0;
51 CloseHandle(PI.hProcess);
52 CloseHandle(PI.hThread);
57 function RenameFile(const OldName,NewName: string; hConsole: THandle): boolean;
58 //renames files, taken from
59 //http://www.dsdt.info/tipps/?id=128&search=RenameFile
65 //terminate with null byte to set list ending
66 sh.pFrom:= PChar(OldName + #0);
67 sh.pTo:= PChar(NewName + #0);
68 sh.fFlags:= fof_Silent or fof_MultiDestFiles;
69 Result:= ShFileOperation(sh)=0;
75 //Read path to this application
78 //get handle of this console window
79 hConsole := FindWindow(nil,Pchar(Input));
80 // hide the window of this console application
81 ShowWindow(hConsole,SW_HIDE);
85 //InputNew = original filename with ending "-preview" (e.g. test-preview.pdf)
86 InputNew:= copy(Input,1,Length(Input)-4); //remove ".pdf"
87 InputNew:= InputNew+'-preview.pdf';
88 //check if renamed file exists
89 FileTest:= FileExists(InputNew);
90 if FileTest = true then
93 ExecWait('pdfclose --file "'+InputNew+'"');
98 RenameFile(Input,InputNew,hConsole);
99 ExecWait('pdfopen --file "'+InputNew+'" --back');