<< Click to Display Table of Contents >> Navigation: Create a PDF Editor > Delphi Example > Add code to initialize a PDF viewer |
On the form there is a TWPViewPDF component - but it will never be used to load a PDF file.
Instead we create a new viewer automatically when we need to load a new file. This is done in the method NewPDFDocument which receives a filename.
It will create a new tab inside the PageControl. The tab will use the class TPDFTabSheet which holds additional variables and of course a reference to a TWPViewPDF component.
Create a custom TTabSheet class:
type TPDFTabSheet = class(TTabSheet)
wpviewpdf : TWPViewPDF;
image : TImage; // uses Vcl.ExtCtrls, we use that later
end;
First we need an handler for the event which is used to update the state of all actions:
procedure TForm2.WPViewPDF_DoChangeViewPage(Sender: TObject; const PageNr: Integer);
var i : Integer;
begin
for I := 0 to ActionList1.ActionCount-1 do
ActionList1.UpdateAction(ActionList1.Actions[i])
end;
This method creates a new tab and the components on it.
procedure TForm2.NewPDFDocument( filename : string );
var
newTab :TPDFTabSheet;
begin
newTab := TPDFTabSheet.Create(PageControl1);
newTab.wpviewpdf := TWPViewPDF.Create(newTab);
try
newTab.PageControl := PageControl1;
// And move it to the page control
newTab.wpviewpdf.Align := alClient;
newTab.wpviewpdf.Parent := newTab;
if not newTab.wpviewpdf.LoadFromFile(filename) then
raise Exception.Create( filename + ' cannot be loaded.');
// Make sure the annotations work interactively!
newTab.wpviewpdf.Command(COMPDF_ACRO_MAKEDRAWOBJ,'', 8192 ); // 0=all Annots!
// Enables saving of annotations which have been added to the page
newTab.wpviewpdf.Command(COMPDF_Ann_SetAnnotSaveMode, 1);
// Standard Action Mode 'Click + Pan'
newTab.wpviewpdf.Command(COMPDF_SetActionMode,'',1);
// Set WPViewPDF Properties
newTab.wpviewpdf.ViewOptions := [wpViewThumbnails,wpInteractiveThumbnails];
newTab.wpviewpdf.ViewControls := [
wpVertScrollBar, // Scrollbar vertical
wpHorzScrollBar, // Scrollbar horizontal
wpPropertyPanel, // '?' Button
wpNavigationPanel, // Up / down
wpViewPanel, // Zooming
wpViewLeftPanel // Thumbnails and Bookmarks -
// also see COMPDF_ShowNavigation
];
// assign the requires events
newTab.wpviewpdf.OnChangeViewPage := WPViewPDF_DoChangeViewPage;
// We need an image for optional background metafiles
newTab.image := TImage.Create(newTab);
newTab.image.Visible := false;
newTab.image.Parent := newTab;
// Add the tab to the page control
newTab.Caption := ExtractFileName(filename);
PageControl1.ActivePage := newTab;
except
newTab.PageControl := nil;
// Loading failed, destroy the control!
newTab.wpviewpdf.Parent := nil;
newTab.wpviewpdf.Free;
newTab.Free;
raise;
end;
end;
Consequently we add a function "pdf" which retrieves the WPViewPDF control which is currently active. If no tab has been added, the result will be nil.
function TForm2.pdf : TWPViewPDF;
begin
Result := nil;
if PageControl1.ActivePage<>nil then
begin
Result := TPDFTabSheet( PageControl1.ActivePage ).wpviewpdf;
end;
end;