Click here to Skip to main content
15,860,972 members
Articles / Web Development / HTML

EMF Printer Spool File Viewer

Rate me:
Please Sign up or sign in to vote.
4.72/5 (32 votes)
23 Apr 2014CPOL3 min read 806.9K   25.2K   92   181
A viewer application for EMF format spool files

What is an EMF Spool File?

When an application sends pages to a printer, they are held in an intermediary file which the application can write to and the printer driver can read from simultaneously. This speeds up the print process as control can return to the application before the printer has finished printing the document.

Usually, this spool file is held in the raw printer page definition language (which could be PCL, PostScript or one of many other options) but in Windows NT, 2000, XP and 2003, it is possible to make the spooler use a different, more device independent format known as an EMF spool file.

The file layout of an EMF spool file is not officially documented by Microsoft, but the newly released documentation reveals that it is a series of enhanced metafile records (one for each page) and a number of other record types which are specific to the business of printing these pages.

The EMF Spool File Record Types

The spool file is composed of the following record types:

VB
Private Enum SpoolerRecordTypes
    SRT_EOF = &H0            ' // int32 zero
    SRT_RESERVED_1 = &H1     '*  1                               */
    SRT_FONTDATA = &H2       '  2 Font Data                      */
    SRT_DEVMODE = &H3        '  3 DevMode                        */
    SRT_FONT2 = &H4          '4 Font Data                        */
    SRT_RESERVED_5 = &H5     ' 5                                 */
    SRT_FONT_MM = &H6        ' 6 Font Data (Multiple Master)     */
    SRT_FONT_SUB1 = &H7      '   7 Font Data (SubsetFont 1)      */
    SRT_FONT_SUB2 = &H8      '   8 Font Data (SubsetFont 2)
    SRT_RESERVED_9 = &H9
    SRT_UNKNOWN = &H10       ' // int unknown...
    SRT_RESERVED_A = &HA
    SRT_RESERVED_B = &HB
    SRT_PAGE = &HC           ' 12  Enhanced Meta File (EMF)       */
    SRT_EOPAGE1 = &HD        ' 13  EndOfPage                      */
    SRT_EOPAGE2 = &HE        ' 14  EndOfPage                      */
    SRT_EXT_FONT = &HF       ' 15  Ext Font Data                  */
    SRT_EXT_FONT2 = &H10     ' 16  Ext Font Data                  */
    SRT_EXT_FONT_MM = &H11   ' 17  Ext Font Data (Multiple Master)
    SRT_EXT_FONT_SUB1 = &H12 ' 18  Ext Font Data (SubsetFont 1)   */
    SRT_EXT_FONT_SUB2 = &H13 '* 19  Ext Font Data (SubsetFont 2)  */
    SRT_EXT_PAGE = &H14      ' 20  Enhanced Meta File?
End Enum

Of these, the most important record types are:

SRT_PAGE

This record contains a Windows standard enhanced metafile, consisting of an EMF header and one or more EMF graphics record structures. The EMF header record gives you the dimensions of the boundary rectangle (the area inside the print margins), the number of graphics records that make up the page, and the file size of this single EMF page.

SRT_DEVMODE

This record contains the device settings that apply to the rest of the pages in the document (or until another DEVMODE record is encountered that overrides it). The DEVMODE structure holds details such as the number of copies, the page orientation (landscape versus portrait), the paper size and paper source, and so on.

SRT_EOF

This signifies the end of the spool file records.

Using the EMF Spoolfile Viewer

To view an EMF spool file, select the menu File -> Open. A dialog box will appear to allow you to browse to the spool file (extension .spl). These are typically held in your $winnt$\System32\spool\ directory. (I have included some samples with this application code as well.)

The viewer allows you to go to a particular page, zoom in and out, and even print the spool file using the following menu options:

  • File
    • Open - Brings up a dialog box to find and open EMF spool files
    • Print - Allows you to reprint the currently open EMF spool file
    • Properties - Brings up a dialog box listing the print file properties
    • Exit - Quits the application
  • Pages
    • First Page - Goes to the beginning of the document
    • Previous Page - Goes back one page
    • Next Page - Goes to the next page
    • Last Page - Goes to the end of the document
    • Goto - Goes directly to the selected page
    • Properties = Brings up a dialog with the properties of this page
  • View
    • Zoom in enlarges the preview
    • Zoom out reduces the size of the preview

Uses for EMF Spoolfiles

Since pretty much every application has a print function, the EMF spool file can serve as a quick and dirty portable document format (though much less featured than a PDF). Thus if you have an application and you want to send a file to someone who doesn't have that application to view, you simply use the application's print function and send them the spool file.

In addition, knowing the structure of the spool file means that it is possible to parse the file to extract information from it. You might want to extract just the text from a printed document for archival purposes, and you can do this by parsing the file and extracting the EMR_EXTTEXTOUTA and EMR_EXTTEXTOUTW records.

Acknowledgements

The source code in this application draws heavily on the work of Feng Yuan and Christophe Lindemann as well as numerous others on the various forums and news groups.

Portions of this article are based on www.undocprint.org/formats/winspool/spl. These portions are subject to the GNU Free Documentation License and are copyright © 2003-2005 Free Software Foundation, Inc.

History

  • 21st August, 2006: Code updated to .NET 2.0 and page navigation improved

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
GeneralRe: Printing Function Pin
Duncan Edwards Jones23-Apr-14 11:38
professionalDuncan Edwards Jones23-Apr-14 11:38 
AnswerRe: Printing Function Pin
Duncan Edwards Jones23-Apr-14 0:19
professionalDuncan Edwards Jones23-Apr-14 0:19 
QuestionLength DEVMODE Pin
DanielRM28-Feb-13 9:39
DanielRM28-Feb-13 9:39 
QuestionSaving the EMF file Pin
Adi Shavit21-Jun-12 8:22
Adi Shavit21-Jun-12 8:22 
AnswerRe: Saving the EMF file Pin
Duncan Edwards Jones12-Jul-12 1:22
professionalDuncan Edwards Jones12-Jul-12 1:22 
Questiontried to open a .spl file and is getting this detailed error message.. any idea Pin
freefall_020321-Jun-12 7:17
freefall_020321-Jun-12 7:17 
AnswerRe: tried to open a .spl file and is getting this detailed error message.. any idea Pin
Duncan Edwards Jones25-Jun-12 3:53
professionalDuncan Edwards Jones25-Jun-12 3:53 
QuestionCrash on load Pin
kelemvorx7-Mar-12 6:17
kelemvorx7-Mar-12 6:17 
Trying to find a way to view a spooler file but it's not working.

I can select the file, it loads for a bit, then it crashes:

Unhandled exception has occurred in a component in your application.
Unable to read beyond the end of the stream.

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IO.EndOfStreamException: Unable to read beyond the end of the stream.
at System.IO.__Error.EndOfFile()
at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
at System.IO.BinaryReader.ReadInt32()
at EMFSpoolfileReader.EMFSpoolfileReader.GetTruePageCount(String SpoolFilename)
at EMFSpoolViewer.SpoolFileForm.MenuItem_File_Open_Click(Object sender, EventArgs e)
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.MenuItem.MenuItemData.Execute()
at System.Windows.Forms.Command.Invoke()
at System.Windows.Forms.Command.DispatchID(Int32 id)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3625 (GDR.050727-3600)
CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
EMFSpoolViewer
Assembly Version: 1.0.1980.19643
Win32 Version: 1.0.1980.19643
CodeBase: file:///C:/Documents%20and%20Settings/tkmaxa6/Desktop/EMFSpoolViewer/EMFSpoolViewer.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3623 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3620 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
EMFSpoolfileReader
Assembly Version: 1.1.1980.19643
Win32 Version: 1.1.1980.19643
CodeBase: file:///C:/Documents%20and%20Settings/tkmaxa6/Desktop/EMFSpoolViewer/EMFSpoolfileReader.DLL
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitdebugging="true">


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
AnswerRe: Crash on load Pin
Duncan Edwards Jones25-Jun-12 3:53
professionalDuncan Edwards Jones25-Jun-12 3:53 
QuestionCannot take the text of the Record Pin
sgkin8-Feb-12 7:34
sgkin8-Feb-12 7:34 
AnswerRe: Cannot take the text of the Record Pin
internet7827-Feb-12 0:26
internet7827-Feb-12 0:26 
GeneralMy vote of 5 Pin
LeviButler10-Jan-12 3:00
LeviButler10-Jan-12 3:00 
GeneralThere is definitely a difference between executable and source Pin
TrickyDicky211013-Jun-11 2:37
TrickyDicky211013-Jun-11 2:37 
GeneralRe: There is definitely a difference between executable and source Pin
Member 800497814-Jun-11 0:58
Member 800497814-Jun-11 0:58 
GeneralRe: There is definitely a difference between executable and source Pin
CalcMan5512-Jul-11 8:01
CalcMan5512-Jul-11 8:01 
GeneralRe: There is definitely a difference between executable and source Pin
Duncan Edwards Jones22-Jan-12 10:41
professionalDuncan Edwards Jones22-Jan-12 10:41 
GeneralRe: There is definitely a difference between executable and source Pin
Duncan Edwards Jones23-Apr-14 0:19
professionalDuncan Edwards Jones23-Apr-14 0:19 
GeneralCode difference? Pin
Skif7-Jun-11 13:14
Skif7-Jun-11 13:14 
GeneralLoading emf file .... nothing happens... Pin
tinvaltino15-Sep-10 12:28
tinvaltino15-Sep-10 12:28 
GeneralRe: Loading emf file .... nothing happens... Pin
Duncan Edwards Jones9-Dec-10 7:33
professionalDuncan Edwards Jones9-Dec-10 7:33 
GeneralRe: Loading emf file .... nothing happens... Pin
Loris Rinaldo10-Nov-11 4:31
Loris Rinaldo10-Nov-11 4:31 
GeneralRe: Loading emf file .... nothing happens... Pin
Loris Rinaldo18-Nov-11 10:21
Loris Rinaldo18-Nov-11 10:21 
GeneralRe: Loading emf file .... nothing happens... Pin
Loris Rinaldo18-Nov-11 10:23
Loris Rinaldo18-Nov-11 10:23 
QuestionExtract text from .spl file to .txt file Pin
BuzzardHawk24-Jul-10 9:06
BuzzardHawk24-Jul-10 9:06 
GeneralAdobe PDF Files Display Incorrectly [modified] Pin
tokfrans22-Mar-10 2:44
tokfrans22-Mar-10 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.