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;
24 var Input,InputNew : string;
29 function ExecWait(const CommandLine: string;
30 const Visible: boolean = false;
31 const MaxSeconds: integer = 60): boolean;
32 //Executes programs and waits until they are terminated
35 PI: TProcessInformation;
42 SI.dwFlags := STARTF_USESHOWWINDOW;
43 SI.wShowWindow := SW_HIDE;
45 if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, SI, PI)) then
47 case WaitForSingleObject(PI.hProcess, MaxSeconds * 1000) of
48 WAIT_OBJECT_0: GetExitCodeProcess(PI.hProcess, ExitCode);
49 WAIT_ABANDONED: TerminateProcess(PI.hProcess, ExitCode);
50 WAIT_TIMEOUT: TerminateProcess(PI.hProcess, ExitCode);
52 result := ExitCode = 0;
53 CloseHandle(PI.hProcess);
54 CloseHandle(PI.hThread);
59 function RenameFile(const OldName,NewName: string; hConsole: THandle): boolean;
60 //renames files, taken from
61 //http://www.dsdt.info/tipps/?id=128&search=RenameFile
67 //terminate with null byte to set list ending
68 sh.pFrom:= PChar(OldName + #0);
69 sh.pTo:= PChar(NewName + #0);
70 sh.fFlags:= fof_Silent or fof_MultiDestFiles;
71 Result:= ShFileOperation(sh)=0;
77 //Read path to this application
80 //get handle of this console window
81 hConsole := FindWindow(nil,Pchar(Input));
82 // hide the window of this console application
83 ShowWindow(hConsole,SW_HIDE);
87 //InputNew = original filename with ending "-preview" (e.g. test-preview.pdf)
88 InputNew:= copy(Input,1,Length(Input)-4); //remove ".pdf"
89 InputNew:= InputNew+'-preview.pdf';
90 //check if renamed file exists
91 FileTest:= FileExists(InputNew);
92 if FileTest = true then
95 ExecWait('pdfclose --file "'+InputNew+'"');
100 RenameFile(Input,InputNew,hConsole);
101 ExecWait('pdfopen --file "'+InputNew+'" --back');