Click here to Skip to main content
Click here to Skip to main content

EMF Printer Spool File Viewer

By , 21 Aug 2006
 

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 this article 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:

    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 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

  • 21 Aug 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)

About the Author

Duncan Edwards Jones
Software Developer (Senior) JP Morgan
Ireland Ireland
Member
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAdobe PDF Files Display Incorrectly [modified]membertokfrans22 Mar '10 - 2:44 
I really appreciate the hard work that has been put into this article and the source code especially.
I have found that viewing PDF files doesn't work correctly for some documents.
When loading the spooler file, the text looks scrambled and the underlying text is different to what's showing in the display.
To illustrate this I'm showing what I'm seeing.
 
In the original document when printed on paper you can find this text:
Hardware Requirements
 
The EMFViewer Application displays:
Ibsexbsf!Sfrvjsfnfout
 
When debugging the code, I find that this is the text in the EMF record:
,EVH[EVI6IUYMVIQIRXW
 
To me this seems like the EMF display has been shifted one byte up. H=I, a=b, r=s and so forth. And the same goes for the EMF record text, but the shift is 28 bytes, H=, a=E, r=V, d=H. You get the picture.
 
Some PDF files works without any problems and looks great, and others display problems like or similar to this.
Can anyone please try to explain why this is, and if there's anything I can do to handle this.
 
Thanks.
Timmy

GeneralRe: Adobe PDF Files Display IncorrectlymemberDuncan Edwards Jones22 Mar '10 - 9:38 
I can kind-of explain it but I'm afraid I do not know what to do to resolve it.
 
Basically when you print to a PDF it doesn't know if the target machine is going to have the font you are printing in or not so in certain circumstances the font is embedded in the spool file. However it only embeds the letters that are used which means that the glyph at a given position in the font table may be different from the character you would expect to be there - hence the offset effect.
 
I have not found any way of either (a) knowing that this has occured or (b) un-offsetting the font array but if you do please post it here and I will bake it into the above code.
'--8<------------------------
Ex Datis:
Duncan Jones
 
Free eBook: Printing - a .NET Developer's Guide (Part 1)

GeneralDidn't get The Text From Spool Job using EmfTextRecordmemberm.junaid8614 Jan '10 - 1:25 
I found EMFSpoolfileReader.EMFTextRecord.
 
how can i read Text from Spool Job
Can you please help me its Urgent. I am very Thank full to you
 
Regards
Muhammad Junaid Khan

GeneralRe: Didn't get The Text From Spool Job using EmfTextRecordmemberDuncan Edwards Jones14 Jan '10 - 20:52 
From the start of the EMFTextRecord read the structure thus:
 
        With SpoolBinaryReader
            _Top = .ReadInt32
            _Left = .ReadInt32
            _Bottom = .ReadInt32
            _Right = .ReadInt32
            _GraphicsMode = .ReadInt32
            _scaleX = .ReadSingle
            _scaleY = .ReadSingle
            _PTx = .ReadInt32
            _PTy = .ReadInt32
            _nChars = .ReadInt32
            _offString = .ReadInt32
            _Options = .ReadInt32
            _TxtTop = .ReadInt32
            _TxtLeft = .ReadInt32
            _TxtBottom = .ReadInt32
            _TxtRight = .ReadInt32
            _offDX = .ReadInt32
            If _offString >= 76 Then
                .BaseStream.Seek(_offString - 76, SeekOrigin.Current)
                Dim chars() As Byte
                chars = .ReadBytes(_nChars * 2)
                _Text = System.Text.Encoding.Unicode.GetString(chars) '  New String(chars)
            End If
        End With

 
'--8<------------------------
Ex Datis:
Duncan Jones
Merrion Computing Ltd

GeneralRe: Didn't get The Text From Spool Job using EmfTextRecordmembermujahidcodeproject21 Jun '12 - 0:08 
Hi Junaid,
 
I have the same requirement of reading the text, can u plz share the code if you were able to view the text.
 
Thanks & Regards
Mujahid
GeneralRead Text Printer outputmemberbardill19 Dec '08 - 4:17 
Hi
i am searching for a code that convert every printoutput in a textfile for
a content filtering.
i think with your code i can do this but i not understand how i can read the text from the spool file?
i see you have a EMFTextRecord , but i don't now how i ca rraed it???
have you a example in vb.net
i am not a very good programmer and my english is not the best sorry.
GeneralPrint all pagesmemberCount.Zilch11 Dec '08 - 23:15 
Is there anyway to send all pages of an EMF file to the printer with the listed exe? Unless I'm missing something, I'm having to print each page one-by-one, (to PDFCreator print driver) and then I'll have to use Acrobat or something to join all the pages back together Frown | :-(
GeneralRe: Print all pagesmemberDuncan Edwards Jones12 Dec '08 - 22:18 
Indeed - although I think if you drag the EMF spool file onto the PDF printer icon (control panel -> printer) it might print it?
 
'--8<------------------------
Ex Datis:
Duncan Jones
Merrion Computing Ltd

GeneralRe: Print all pagesmemberCount.Zilch13 Dec '08 - 0:05 
Thanks for the tip, but no, unfortunately I already tried this (PDFCreator under XP using .SPL files). It first prompts you to change this to be the default printer, then fails with a message "This file cannot be printed. Please try opening the file with the correct application and printing from there". Frown | :-(
 
Also tried sharing the virtual printer and issuing "copy file.spl \\computer\pdfcreator" and that gives a PDF with a blank landscape page. Copying the same file to a physical laser printer works fine.
 
Any other ideas appreciated! The view works perfectly, except I have to do everything one page at a time...
GeneralIt's great, thanks. Where can I find the code of SpoolfileReaderBase.dll??? [modified]memberamanofsky10 Oct '08 - 6:40 
Can you please upload it?
Besides, I tried to compile the codes you uploaded successfully after removing the reference SpoolfileReaderBase.dll, but the output appears quite different from the binary you uploaded.
For example, the [FILE] menu, there is no [properties] sub menu items.
 
Can you please upload the latest codes that can compile the binary you uploaded? thanks
 
modified on Friday, October 10, 2008 12:55 PM

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 21 Aug 2006
Article Copyright 2005 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid