- Feb 1, 2021
- 38
- 11
- 8
- Customer Identifier
- E074775
Applications often need to generate PDF files, for example, for emailing a report to a client. An easy way to do this is to use a so-called PDF printer, that is, a virtual printer which converts the output to PDF and saves it to a file.
However, many PDF printers require user interaction for specifying the directory and name of the destination file, which is not desirable sometimes. This is true in particular for the free PDF printing solutions. which often offer user presets or other means for defining PDF file names as part of a premium service plan..
On Windows 10 and newer computers, the "Microsoft Print to PDF" printer provides a viable alternative for generating simple PDF files without having to revert to a third-party tool. This virtual printer ships with the Windows operating system and can be used in Xbase++ applications.
The following example code generates a PDF file in the application folder. The example first creates a printer object for the "Microsoft Print to PDF" printer by passing the printer's name to the :create() method, Afterwards, the destination PDF file is specified via the :setPrintFile() method.
However, many PDF printers require user interaction for specifying the directory and name of the destination file, which is not desirable sometimes. This is true in particular for the free PDF printing solutions. which often offer user presets or other means for defining PDF file names as part of a premium service plan..
On Windows 10 and newer computers, the "Microsoft Print to PDF" printer provides a viable alternative for generating simple PDF files without having to revert to a third-party tool. This virtual printer ships with the Windows operating system and can be used in Xbase++ applications.
Note: If "Microsoft Print to PDF" is not available on your computer, the feature may have to be enabled manually using the "Turn Windows features on or off" dialog. To do this, enter "turn windows features on or off" into Windows search and make a sure a checkmark is set next to the "Microsoft Print to PDF" feature.
The following example code generates a PDF file in the application folder. The example first creates a printer object for the "Microsoft Print to PDF" printer by passing the printer's name to the :create() method, Afterwards, the destination PDF file is specified via the :setPrintFile() method.
Xbase++:
#include "gra.ch"
PROCEDURE Main()
LOCAL cPDF := StrTran(AppName(.T.), AppName(.F.), "") + "test.pdf"
LOCAL oPrt
// Create a printer object for the PDF printer and set the
// destination file name
oPrt := XbpPrinter():new():create( "Microsoft Print to PDF" )
oPrt:setPrintFile( cPDF )
// Print some data
PrintTest( oPrt )
WAIT "Test PDF printed to " + cPDF + ". Press a key."
RETURN
// Output some test data to the specified printer
PROCEDURE PrintTest( oPrt )
LOCAL oPS
LOCAL aStart
LOCAL aEnd
LOCAL i
LOCAL oFnt
LOCAL aSize
// Create a presentation space for drawing and associate
// it with the printer, then start the print job
oPS := XbpPresSpace():new():create( oPrt )
oPrt:startDoc()
// Print a string at the top of the page
oFnt := XbpFont():new(oPS):create( "46.Arial" )
GraSetFont( oPS, oFnt )
aSize := oPrt:paperSize()
aStart := {100,aSize[8]-400}
GraStringAt( oPS, aStart, "This is a test" )
// Output a number of colored boxes
GraSetColor( oPS, GRA_CLR_RED )
aStart[2] -= 1100
aEnd := {aStart[1]+300,aStart[2]+300}
FOR i:=1 TO 5
GraBox( oPS, aStart, aEnd, GRA_OUTLINE )
aStart[1] += 200
aStart[2] += 200
aEnd[1] += 200
aEnd[2] += 200
NEXT
// End the print job and release resources
oPrt:endDoc()
oPS:destroy()
RETURN
Last edited by a moderator: