]> 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 var Input,InputNew : string;
23     FileTest : boolean;
24     hConsole : THandle;
25
26     
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
31 var
32 SI: TStartupInfo;
33 PI: TProcessInformation;
34 ExitCode: DWORD;
35 begin
36  result := false;
37  GetStartupInfo(SI);
38  if not Visible then
39  begin
40   SI.dwFlags := STARTF_USESHOWWINDOW;
41   SI.wShowWindow := SW_HIDE;
42  end;
43  if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, SI, PI)) then
44  begin
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);
49   end;
50   result := ExitCode = 0;
51   CloseHandle(PI.hProcess);
52   CloseHandle(PI.hThread);
53  end;
54 end; // end function
55
56
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
60 var
61   sh: TSHFileOpStruct;
62 begin
63   sh.Wnd:= hConsole;
64   sh.wFunc:= fo_Rename;
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;
70 end; //end function
71
72
73 begin //begin program 
74
75  //Read path to this application
76  Input:= ParamStr(0);
77
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);
82
83  //Read given filename
84  Input:= ParamStr(1);
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
91  begin
92   //close old file
93   ExecWait('pdfclose --file "'+InputNew+'"');
94   //delete old file
95   DeleteFile(InputNew);
96  end;
97  //rename file
98  RenameFile(Input,InputNew,hConsole);
99  ExecWait('pdfopen --file "'+InputNew+'" --back');
100
101 end. //end program