I updated the code. I also made the image look more like a page.
For me it works fine. If you have any suggestions, commands or additions please let me know
unit pdfview;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,uiSEDQuickPDF, ExtCtrls,Jpeg,printers;
type Tborder = record height: integer; width: integer; end;
Tpage =record height:integer; width:integer; first :integer; last :integer; Number: integer; end;
TQuickPDFViewer = class(Tscrollbox) private Image :Timage; QP : TiSEDQuickPDF; FFileName: string; Border: Tborder; Page:Tpage; FLastPage: integer; FFirstPage: integer;
procedure set_LoadFile(const FileName: string); procedure makePages; procedure setFirstPage(const Value: integer); procedure setLastPage(const Value: integer); protected defaultprinter: string; procedure HandleMessage(var Msg: TMsg; var Handled: Boolean); public constructor create(Aowner:Tcomponent); override; destructor destroy; override; procedure show; procedure Print; published property LoadFile: string read FFileName write set_LoadFile; property FirstPage: integer read FFirstPage write setFirstPage; property LastPage: integer read FLastPage write setLastPage;
end; implementation
{ TQuickPDFViewer }
constructor TQuickPDFViewer.create(Aowner: Tcomponent); begin inherited create(Aowner); parent:=Twincontrol(Aowner); visible:=false; Left:=0; Top:=0; height:=Twincontrol(Aowner).Height -40; width:=Twincontrol(Aowner).width -100; border.height:=40; border.width:=30; image:= TImage.create(self); image.Parent:=self; QP := TiSEDQuickPDF.Create; QP.UnlockKey('...'); //fill in you key I'm not gonna make a mistake twice ;-) invalidate; application.OnMessage :=handleMessage; defaultprinter:='odprn005'; //This is the printername wich I used plaese fill in a valid one fot your case end;
destructor TQuickPDFViewer.destroy; begin image.Free; QP.free; inherited; end;
//I need this because I have some trouble with scrolling
procedure TQuickPDFViewer.HandleMessage(var Msg: TMsg; var Handled: Boolean); begin if Msg.Message = WM_MOUSEWHEEL then begin Msg.lParam := 0; if Msg.wParam > 0 then self.VertScrollBar.Position:=self.VertScrollBar.Position-75 else self.VertScrollBar.Position:=self.VertScrollBar.Position+75; Handled := true; end;
end;
procedure TQuickPDFViewer.makePages; var i: integer; begin
image.Top :=0; image.Left:=0; image.Height :=page.Height*page.Number +(page.Number+1)*border.height; image.Width :=page.Width +2*border.width ; image.Canvas.Brush.Color :=clgray; image.Canvas.Brush.style :=bsSolid; image.Canvas.Pen.Style :=Psclear; image.Canvas.Rectangle(0,0,border.width ,image.height); image.Canvas.Rectangle(image.Width-border.width,0,image.Width ,image.height);
for i:=0 to page.Number do begin image.Canvas.Rectangle(0,(i* (border.height+page.height)),image.Width ,(i* (border.height+page.height))+border.height); end;
image.Canvas.Brush.Color :=clgray; image.Canvas.Brush.style :=bsClear; image.Canvas.Pen.Style :=psSolid; image.Canvas.Pen.color :=clblack;
for i:=0 to page.Number-1 do begin image.Canvas.Rectangle(border.width-1,((i+1)*border.height)-1+(i*page.height) ,image.Width - border.width+1,(page.Height+border.height)*(i+1)+1); end;
end;
procedure TQuickPDFViewer.Print; begin
QP.PrintDocument(defaultprinter,firstpage,lastpage,0); end;
procedure TQuickPDFViewer.setFirstPage(const Value: integer); begin FFirstPage := Value; page.first:=value; page.number:=page.last-page.first+1; end;
procedure TQuickPDFViewer.setLastPage(const Value: integer); begin FLastPage := Value; page.last:=value; page.number:=page.last-page.first+1;
end;
procedure TQuickPDFViewer.set_LoadFile(const FileName: string); var JpegStream:Tmemorystream; JPEG : TJpegImage; begin FFileName := FileName; JPEG:=TJpegImage.create; JpegStream:=Tmemorystream.Create; try // QP.LoadFromFile(filename); FirstPage:=1; LastPage:=QP.PageCount; QP.RenderPageToStream(75,1,1,jpegstream); jpegstream.Seek(0,0); JPEG.LoadFromStream(jpegstream); page.height :=jpeg.Height ; page.width :=jpeg.width ; finally jpegstream.Free ; JPEG.Free ; end; end;
procedure TQuickPDFViewer.show; var i,j: integer; JpegStream:Tmemorystream; JPEG : TJpegImage; JpegImage: Timage; begin MakePages; JPEG:=TJpegImage.create; JpegStream:=Tmemorystream.Create; JpegImage:=Timage.Create(self); try j:=0; for i:=FirstPage to LastPage do begin if i > qp.PageCount then begin visible:=true; exit; end; jpegstream.Clear; QP.RenderPageToStream(75,i,1,jpegstream); jpegstream.Seek(0,0); JPEG.LoadFromStream(jpegstream); jpegimage.Picture.Assign(JPEG); image.Canvas.Draw(border.width,(j+1)*border.height+ j*page.height,jpegimage.Picture.Graphic); inc(j); end; visible:=true; finally jpegstream.Free ; JPEG.Free ; jpegimage.free; end;
end;
end.
|