• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

Setting physical page offset

I am using the following code to control certain aspects of printing:

{Set to custom size}

 pDMode^.dmFields := pDMode^.dmFields or
 DM_PAPERSIZE or
 DM_PAPERWIDTH or
 DM_PAPERLENGTH;
 pDMode^.dmPaperSize := DMPAPER_USER;
 pDMode^.dmPaperWidth := 100 {SomeValueInTenthsOfAMillimeter};
 pDMode^.dmPaperLength := 100 {SomeValueInTenthsOfAMillimeter};

This works well, but I have limited information on how to expand on this to control margins for BOTH physical and printable area.  I am using a WPTools Richedit for printing and have set the left margin to zero but I still have a physical margin of 1.5 cm on the printed output.  Prior to using the above code I could take the physical margin down to 0.5 cm but I was limited in controlling custom paper sizes.

My question is, how can I control physical page offset?

Regards

Peter Knapp
Orion Practice Management
0
OrionPMS
Asked:
OrionPMS
  • 3
  • 2
1 Solution
 
mikelittlewoodCommented:
This PAQ discusses the printer areas you are looking for

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21386921.html
0
 
OrionPMSAuthor Commented:
Since our code is already set up to use pDEVMODE I had really hoped to be able to find a way to set margins using that construct.  I have no experience of using the construct in the link you mention.
0
 
pcsentinelCommented:
I think you would find using pDev in this manner a bit odd as you need to set the paper to a defult size first anyway to allow the structure to return the information you want before setting it to the custom paper size.

The code I posted in

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21386921.html

will do what you are looking for

I have repeated it here with a bit more info

  FPhysWidthPix:=GetDeviceCaps(Printer.Handle,Physicalwidth);
This is the actual width of the page in printer pixels

  FPhysHeightPix:=GetDeviceCaps(Printer.Handle,Physicalheight);
This is the actual height of the page in printer pixels

  FActWidthPix:=GetDeviceCaps(Printer.Handle,HORZRES);
This is the printable width of the page in printer pixels

  FPhysHeightPix:=GetDeviceCaps(Printer.Handle,VERTRES);
This is the printable height of the page in printer pixels

  FPhysleftOffsetpix:=GetDeviceCaps(Printer.Handle,Physicaloffsetx);
This is the physical left margin i.e. the non printable left side of the page

  FPhysTopOffsetPix:=GetDeviceCaps(Printer.Handle,Physicaloffsety);
This is the physical top margin i.e. the non printable top side of the page

  FHorzPi:=GetDeviceCaps(Printer.Handle,LogPixelsX);
Number of printer pixels per inch across the page

  FVertPi:=GetDeviceCaps(Printer.Handle,LogPixelsY);
Number of printer pixels per inch down the page

Using all of this info you can implement what you want

i.e. if you want a left margin of 1/2" then convert that to horizontal pixelsusing the FHorzPi value

now your printer may not be able to print in the first 1/4" so this value would be returned in the
FPhysleftOffsetpix

so

 then subtract the FPhysleftOffsetpix to get the actual number of pixels to set your left margin to.

Its very simple to use this construct just set the printer first.


regards







0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
OrionPMSAuthor Commented:
Sentinel...

Thanks for this info, but I must admit Im confused.  This entire area of controlling printers is new to us and I cant see in your example the syntax for actually setting a left hand or for that matter a right hand margin.

I followed the help thread and see that you are UK like us.. we are in Glasgow.  Are you a contract Delphi programmer?   If so we would be interested in getting in touch, not only for this problem, but for other potential projects in future.

Dont want to post an email address here, so if you have a temporary contact email then let me know.

Peter Knapp
Technical Director
Orion Practice Management Systems Ltd
Glasgow
0
 
pcsentinelCommented:
You can contact me at pcstemp@hotmail.co.uk

regards
0
 
pcsentinelCommented:
For a RichEdit where you want say a 30mm top margin and a 50mm left margin
with a printable area of say 100mmx100mm then you would use the following

procedure TForm1.Button1Click(Sender: TObject);
var
      lPhysWidthPix: integer;
  lPhysHeightPix: integer;
  lActWidthPix: integer;
  lActHeightPix: integer;
  lPhysleftOffsetpix: integer;
  lPhysTopOffsetPix: integer;
  lHorzPi: integer;
  lVertPi: integer;
  lPageRect: TRect;
  lt: integer;
  ll: integer;
  lw: integer;
  lh: integer;
begin
  lPhysWidthPix:=GetDeviceCaps(Printer.Handle,Physicalwidth);
  lPhysHeightPix:=GetDeviceCaps(Printer.Handle,Physicalheight);
  lActWidthPix:=GetDeviceCaps(Printer.Handle,HORZRES);
  lPhysHeightPix:=GetDeviceCaps(Printer.Handle,VERTRES);
  lPhysleftOffsetpix:=GetDeviceCaps(Printer.Handle,Physicaloffsetx);
  lPhysTopOffsetPix:=GetDeviceCaps(Printer.Handle,Physicaloffsety);
  lHorzPi:=GetDeviceCaps(Printer.Handle,LogPixelsX);
  lVertPi:=GetDeviceCaps(Printer.Handle,LogPixelsY);
  //measurements in a rich edit are set in twips
  //so say you want a top margin of 30mm
  //a left margin of 50mm
  //so for the top margin convert 30mm into printer pixels
  lt:=trunc((30/25.4)*lVertPi); //no of pixels down from physical top of page
  //then subtract the unprintable area
  lt:=lt-lPhysTopOffsetPix;
  //now the left margin
  ll:=trunc((50/25.4)*lHorzPi); //no of pixels across from physical left of page
  //then subtract the unprintable area
  ll:=ll-lPhysleftOffsetpix;
  //now lets say the area you want to print in is 100mm*100mm
  lh:=trunc((100/25.4)*lVertPi);
  lw:=trunc((100/25.4)*lHorzPi);
  //so now we have the number of pixels required
  lPageRect.Top:=lt;
  lPageRect.Left:=ll;
  lPageRect.Bottom:=lt+lh;
  lPageRect.Right:=ll+lw;
  //now set the rect and print
  Richedit1.PageRect:=lPageRect;
  RichEdit1.Print('Test');
end;

Remember to add Printers to your uses clause.

Note some of the code in this is redundant and some is optimised, I have just left it in so that you can see it

Also note that Delphi help states that the Pagerect is in twips, well its not its actually in pixels and the delphi source code converts it to twips, or at least it does in mine

hope this helps

regards
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now