]> git.lyx.org Git - features.git/blob - development/Win32/packaging/AltInstaller/specials/PDFViewWin/PDFViewWin.dpr
installer:
[features.git] / development / Win32 / packaging / AltInstaller / specials / PDFViewWin / PDFViewWin.dpr
1 program PDFViewWin;
2 // this program opens and closes PDF-files with Acrobat or Adobe Reader
3 // author: Uwe Stöhr
4
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.}
16
17 {$APPTYPE CONSOLE}
18
19 uses
20   Windows, SysUtils, ShellApi;
21
22 {$R *.res}
23
24 var Input,InputNew : string;
25     FileTest : boolean;
26     hConsole : THandle;
27
28     
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
33 var
34 SI: TStartupInfo;
35 PI: TProcessInformation;
36 ExitCode: DWORD;
37 begin
38  result := false;
39  GetStartupInfo(SI);
40  if not Visible then
41  begin
42   SI.dwFlags := STARTF_USESHOWWINDOW;
43   SI.wShowWindow := SW_HIDE;
44  end;
45  if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, SI, PI)) then
46  begin
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);
51   end;
52   result := ExitCode = 0;
53   CloseHandle(PI.hProcess);
54   CloseHandle(PI.hThread);
55  end;
56 end; // end function
57
58
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
62 var
63   sh: TSHFileOpStruct;
64 begin
65   sh.Wnd:= hConsole;
66   sh.wFunc:= fo_Rename;
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;
72 end; //end function
73
74
75 begin //begin program 
76
77  //Read path to this application
78  Input:= ParamStr(0);
79
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);
84
85  //Read given filename
86  Input:= ParamStr(1);
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
93  begin
94   //close old file
95   ExecWait('pdfclose --file "'+InputNew+'"');
96   //delete old file
97   DeleteFile(InputNew);
98  end;
99  //rename file
100  RenameFile(Input,InputNew,hConsole);
101  ExecWait('pdfopen --file "'+InputNew+'" --back');
102
103 end. //end program