2007-01-17

Print Individual Images Revisited

Referring to a previous post, from may 2006 entitled "Print individual images", Naomi Nosonovsky asks for the possibility to obtain a better control over the image.

That's really easy, and the original code needed a very simple modification, in a first moment clearing and then sending the desired information to the expr field of the report table.

The EXPR, TAG, and TAG2 columns in the header record are used for printer setup attributes.

In some limited cases, we may edit the values in the expr column and, if these values are reasonable for the associated attributes, they will be recognized by the designer, and used by the report engine at run time.

For example, if a report definition includes in the expr field : orientation=1, copies=3 and color=1, we will obtain a landscape report, three printed copies and an image printed in greyscale.

For the case related, we need to pass these information as parameters for the udf printimage(), eg.

=PrintImage(getpict(), "orientation=1" + chr(13) + "copies=3"

Notice that I added a CHR(13) to separate each configuration. That's very important, and VFP needs this to recognize the settings.

 

Here are some of the available settings:

driver=winspool
device=ms publisher imagesetter
output=file:
orientation=0 (0 = portrait ; 1 = landscape)
papersize=1
scale=100
ascii=100
copies=1
defaultsource=15
printquality=600
color=1 (0 = color ; 1 = greyscale)
yresolution=600
ttoption=2
collate=1


We can find precious information about these settings in vfp's help for prtinfo(): http://msdn2.microsoft.com/en-us/library/7h4yx3sf(vs.80).aspx

 

Here's the code - save it as PRINTIMAGE.PRG and call it like this:

 

= PrintImage (getpict(),"copies = 2" + chr(13) + "orientation = 1" + chr(13) + "color = 1")

* program: printimage.prg
function P
rintImage(tcimage, tcprintsettings)
*--------------------------------------------------------
* vfp code that shows how to print image files.
* code adapted from microsoft knowledge base article
* 895602.
http://support.microsoft.com/kb/895602/en-us/
*
* most of the codes and comments below come from
* trevor hancock, from ms
*--------------------------------------------------------
*--------------------------------------------------------
* parameters accepted:
* tcimage = image file name
* tcprintsettings = special printing settings, that allow to choose orientation, copies, etc
* eg. "copies = 2" + chr(13) + "orientation = 1"
* will print 2 copies with orientation set as landscape
* more printer settings can be found at vfp help for prtinfo()
* below is an example with some of the parameters allowed:
* driver=winspool
* device=ms publisher imagesetter
* output=file:
* orientation=0
* papersize=1
* scale=100
* ascii=100
* copies=1
* defaultsource=15
* printquality=600
* color=1
* yresolution=600
* ttoption=2
* collate=1
* if no parameter is passed, will print using the default printer settings
*--------------------------------------------------------
* related links
* msdn - prtinfo()
*
http://msdn2.microsoft.com/en-us/library/7h4yx3sf(vs.80).aspx
* pete sass : the visual foxpro report writer
* http://www.foxite.com/articles/read.aspx?id=24&document=the-visual-foxpro-report-writer

local
lnarea
lnarea =
select()
create cursor
reporttemp (imagefile c(150))
insert into
reporttemp values (tcimage)
*-- this calls a function that makes a report programmatically.
*-- this is included here to make sure that this sample can be run
*-- as-is, without asking the developer to manually create a report.

makereport(tcprintsettings)

*-- make sure that the cursor is selected,
*-- and then run the report to preview using
*-- the instance of our report listener.

select
reporttemp
report form
___imagereport preview
delete file
"___imagereport.fr*"
select
(lnarea)
return


 

*--------------------------------
*-- this function programmatically creates a report
*-- with an ole bound control and other fields. this is included
*-- only for demonstration purposes so this article code can stand-alone.
*-- typically, you would create your own report manually by using
*-- the report designer.

function
makereport(tcprintsettings)

if vartype(tcprintsettings) <> "c"
tcprintsettings = ""

endif

tcprintsettings =
strtran(tcprintsettings, " ", "")

create report ___imagereport from reporttemp

*-- open the report file (frx) as a table.
use
___imagereport.frx in 0 alias thereport exclusive
select
thereport

*-- remove from the frx the auto generated fields and labels
delete from
thereport where objtype = 5 and objcode = 0 && remove the labels
delete from
thereport where objtype = 8 and objcode = 0 && remove the fields

*-- find the header record (normally the first one, but who knows)
locate for
objtype = 1 and objcode = 53
if found
()
replace tag with "",; && clears the tag, tag2
tag2 with "",;
expr with tcprintsettings && stores the new expr
endif

*-- add a picture/ole bound control to the report by inserting a
*-- record with appropriate values. using an object that is based on the empty
*-- class here and the gather name class later to insert the record makes it easier to
*-- see which values line up to which fields (when compared to a large
*-- sql-insert command).

local
lonewrecobj as empty
lonewrecobj =
newobject( 'empty' )
addproperty
( lonewrecobj, 'platform', 'windows' )
addproperty
( lonewrecobj, 'uniqueid', sys(2015) )
addproperty
( lonewrecobj, 'objtype', 17 ) && "picture/ole bound control"
addproperty
( lonewrecobj, 'name', 'reporttemp.imagefile' ) && the object ref to the image object.
addproperty
( lonewrecobj, 'hpos', 100)
addproperty
( lonewrecobj, 'vpos', 600)
addproperty
( lonewrecobj, 'height', 100000)
addproperty
( lonewrecobj, 'width', 100000)
addproperty
( lonewrecobj, 'double', .t. ) && picture is centered in the "picture/ole bound control"
addproperty
( lonewrecobj, 'supalways', .t. )
*-- for the picture/ole bound control, the contents of the offset field specify whether
*-- filename (0), general field name (1), or expression (2) is the source.

addproperty
( lonewrecobj, 'offset', 2 )
*-- add the picture/ole bound control record to the report.

append blank in
thereport
gather name
lonewrecobj memo
*-- clean up and then close the report table.

pack
use in select
('thereport')
endfunc

1 comment:

  1. Versión en Español de este artículo en / Spanish version at http://www.portalfox.com/article.php?sid=2343

    ReplyDelete