Print Page | Close Window

PDF view

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=329
Printed Date: 22 Nov 24 at 6:39PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: PDF view
Posted By: cpri
Subject: PDF view
Date Posted: 23 Feb 06 at 2:15AM

I want to use quickpdf as a viewer. So I started with a very simple viewer. The idea is to build a VCL component that can do all the viewing things I want. So first here's is my very basic code for viewing pdf's In the future I will post my extentions to this code.

procedure TForm1.Button1Click(Sender: TObject);
var
 QP: TiSEDQuickPDF;
 Test_Stream:Tmemorystream;
 JPEG : TJpegImage;

begin
 QP := TiSEDQuickPDF.Create;
 JPEG:=TJpegImage.create;
 Test_Stream:=Tmemorystream.Create;
 try
   QP.UnlockKey('...');
   QP.LoadFromFile('c:\test.pdf');
   QP.RenderPageToStream(75,1,1,test_stream);
   test_stream.Seek(0,0);
   JPEG.LoadFromStream(test_stream);
   image1.Top :=0;
   image1.Left:=0;
   image1.Height :=jpeg.Height ;
   image1.Width :=jpeg.Width;
   image1.Picture.Assign(Jpeg);
 finally
   QP.Free;
   test_stream.Free ;
   JPEG.Free ;

 end;

end;

 




Replies:
Posted By: cpri
Date Posted: 24 Feb 06 at 2:10AM

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.



Posted By: ECPVFR
Date Posted: 22 May 06 at 11:22AM
Hello Christian,

today I tested Your "TQuickPDFViewer" component and it works well.
The only problem is: if I'm loading PDF docs with two or more pages, it is VERY SLOW (30 - 60 seconds for FOUR pages...).
Do You have an idea why it takes so much time??

-------------
Best Regards,
Volker


Posted By: cpri
Date Posted: 23 May 06 at 1:23AM
Sorry I can't help you I tested it with documents with a lot of pages and I found no problem what so ever


Posted By: cpri
Date Posted: 30 May 06 at 2:12AM

Hello

I took a look at your problem, and I think it's due to the memory.

In the code a image is created with the size of the complete pdf. So if you have a lot of pages you need a lot of memory.

My viewer isn't optimized for that. If you need to view a lot of pages thge best thing you can do is make sections. Something like the prev_Section main_Section and next_Section. The main_section contains an image (only one page) with the page you are looking at the prev_section contains the previous page and the next_section conatins the next page. Every time you scroll up or down the sections are updated with the right pages. By using more secrions you can do the rendering to image in the background.

Because the programs I made doesn't have to deal with a lot of pages I didn't optimize the viewer for that. But every one who has ideas to make this viewer better please be my guest and update the viewer to your own needs.



Posted By: ECPVFR
Date Posted: 30 May 06 at 3:54AM
Hi cpri,

thanks, I realized the 'problem' when testing with large PDF files (2-15 pages) containing images.

At the project I need the viewer for:
Every PDF has only one page and contains NO images. For this use Your viewer is excellent!

-------------
Best Regards,
Volker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk