Windows10 Print to PDF - Print to XPS

Published on by Yousfi Benameur



The XPS Document Writer allows you to create xps files using any program that you can print from in Windows. Print to the XPS Document Writer when you want to create, send, and share or publish documents that you do not want other people to modify, or when you want to print a document or
display it online exactly as it appears on your screen. It's also a good idea to create an XPS document for files that contain graphics or illustrations that might otherwise display differently in print than online or on computers with different monitors.
XML Paper Specification (XPS)    is a format that preserves document formatting and enables file sharing.
The XPS format is Microsoft’s alternative to PDF. It was introduced in Windows Vista, but never  gained much traction. However, modern versions of Windows continue to include better support  for XPS files than PDF files.

Portable Document Format (PDF) : preserves document formatting and enables file sharing. When the PDF format file is viewed online or printed, it retains the format that you intended. The PDF format is  also useful for documents that will be reproduced by using commercial printing methods.


on Windows10 there is two printers can be used usefully:
Microsoft print to PDf
Microsoft XPS Document Writer

Of course, the win10 native Microsoft Print to PDF feature is available in every application in Windows 10 that  has printing capability. You just access the Print dialog box from a standard Windows application or a Windows Store app  and select the Microsoft Print to PDF option.


this code try to make a semi automatic printing programmatly with each extension file accepted to be print directly.can choose Microsoft print to PDF or Microsoft XPS Document Writer printer.
the vfp documents text based (prg,mpr,h,...) are not accepted.then i converted them to text in a temp file.Fortunatly TXT extension is well accepted by the 2 printers.
Can apply this method for any another file extension text based.

limitations: i dont see any available method to prevent the printers dialog box and the saving created file to appears.that why i said a semi automatic method.the native printers cannot accept a destination filename programmatly (or silently).
-urls must be typed with http://... otherwise a blank output is returned.

*added codes 2-3-4 for doc2pdf, xls2pdf and frx2pdf


Click on code to select [then copy] -click outside to deselect


*1*
*assuming have a virtual printer to pdf as
*"Microsoft print to PDf"  or "Microsoft XPS Document Writer" in windows10 (print to pdf or to XPS files)
*print to pdf any printable file or valid web url in selection  with virtual printer as default
*code fires yet 2 dialog boxes(confirm default printer+dialog to choose a pdf filename)

Set Safe Off

Local m.lpt,m.upr
m.lpt=Int(Val(Inputbox("Microsoft print to PDf(1) - Microsoft XPS Document Writer(2) ","","1")))
If Empty(m.lpt)
    m.lpt=1
Endi
If m.lpt=1
	m.upr="Microsoft print to PDF"
Else
	m.upr="Microsoft XPS Document Writer"
Endi

Local m.xx
m.xx=Int(Val(Inputbox("Local file(1)-Web url (2)","","1")))
If Empty(m.xx)
	Return .F.
Endi

Local m.lcfile
Do Case
Case m.xx=1 &&local files
	m.lcfile=Getfile("htm|html|jpg|png|gif|txt|prg|mpr|h|dbf|csv")
	If !Inlist(Lower(Justext(m.lcfile)),"htm","html","jpg","png","gif","txt","prg","mpr","h","dbf","csv") &&can add another printable files
		Messagebox("file not in selection...cancelling.",16+4096,"Error")
		Return .F.
	Endi
	If Inlist(Lower(Justext(m.lcfile)),"dbf") &&VFP files not accepted directly must convert to txt if txt based.
		Use (m.lcfile)
		Copy To Addbs(Sys(2023))+"ytemp.txt" Sdf
		Use In (m.lcfile)
		m.lcfile=Addbs(Sys(2023))+"ytemp.txt"
	Endi

	If Inlist(Lower(Justext(m.lcfile)),"prg","mpr","h","csv")
		Copy File (m.lcfile) To Addbs(Sys(2023))+"ytemp.txt"
		m.lcfile=Addbs(Sys(2023))+"ytemp.txt"
	Endi

Case m.xx=2  &&web url
	Local m.url
	m.url=Inputbox("Type the web url","","")
	If Empty(m.url)
		Return .F.
	Else
		m.lcfile=m.url
	Endi

Endcase

*Printers can be
*"Microsoft print to PDF" set as default printer to pdf  in 1th line
*"Microsoft XPS Document Writer" to xps

Local m.myvar
TEXT to m.myvar textmerge noshow
RUNDLL32.EXE PRINTUI.DLL,PrintUIEntry /y /n  "<<m.upr>>"
rundll32.exe %windir%\system32\mshtml.dll,PrintHTML "<<m.lcfile>>"
ENDTEXT
Strtofile(m.myvar,"ybat.bat")

Local oshell
oshell=Newobject("wscript.shell")
oshell.Run("ybat.bat",0,.T.)
oshell=Null
Erase (Addbs(Sys(2023))+"ytemp.txt")
Messagebox("the pdf file is built",0+32+4096)



Windows10 Print to PDF -  Print to XPS

Click on code to select [then copy] -click outside to deselect


*save an open word document file to pdf silently and without any dialog box
*this is done in office2007 but with an addon installed on office2007 named saveasPDFandXPS.exe (935ko )
*free download from https://www.microsoft.com/fr-fr/download/confirmation.aspx?id=7

*2*
#define wdFormatPDF 17
oWord=createObject('word.application')
lodoc=oWord.documents.open(getfile('doc'))
loDoc.saveas("f:\mypdf",wdFormatPDF)  &&pdf
loDoc.close()
oword.quit


*3*
*excel to pdf with same addon installed
#define xlTypePDF 0
*&&save Excel to pdf ( office2007 + addon)
oExcel=createObject('Excel.application')
lodoc=oExcel.workBooks.open(getfile('xls'))
*ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=“sales.pdf” Quality:=xlQualityStandard DisplayFileAfterPublish:=True
loDoc.ExportAsFixedFormat(xlTypePDF,"f:\mypdf")  &&pdf
loDoc.close()
oExcel.quit


*4*
*a beautiful solution to export a vfp report(frx) to pdf with foxypreviewer.download stable version from http://foxypreviewer.codeplex.com/

DO locfile('foxypreviewer.app')  
report form (getfile("frx") ;
          object type 10 ; && regular pdf
          to file ("f:\asupprimer.pdf") ; && destination file
          preview    && open the default pdf viewer
          


* in code *2* or *3* or *4* can use shellexecute to view to pdf file produced

&&shellexecute
DECLARE INTEGER ShellExecute IN SHELL32.DLL INTEGER nWinHandle,;
                                            STRING cOperation,;
                                            STRING cFileName,;
                                            STRING cParameters,;
                                            STRING cDirectory,;
                                            INTEGER nShowWindow

result = ShellExecute(0, "open", "full path to your pdf file","","",1)




Important:All Codes above are tested on VFP9SP2 & windows 10 pro &

Please come back with any bug.correct code is usefull to all readers. With the pleasure to share.

To be informed of the latest articles, subscribe:
Comment on this post

FOXER 02/18/2017 15:47

THANKS,ITS USERFULL

pravin 04/23/2016 06:11

good work

Haman 03/25/2016 13:04

Very userfull
Thank for sharine