Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:59:57+00:00 2026-06-18T19:59:57+00:00

I am building a program to capture printed documents and then convert these documents

  • 0

I am building a program to capture printed documents and then convert these documents to xps\image.

Currently i am using FileSystemWatcher to monitor the directory “C:\Windows\System32\spool\PRINTERS\” and copy the spl files, make sure its not duplicate then try converting it to xps file by printing the spl file using the Win32 Spooler API and the Microsoft XPS Document Writer predefined printer, but when i specify an output file as shown below, the error code 1804 is returned, if i leave it empty it succeeds but then i don’t get an output file.

    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount, string outputFile, string dataType, out int errorCode)
    {
        IntPtr hPrinter;
        var di = new DOCINFOA();
        var bSuccess = false; // Assume failure unless you specifically succeed.

        di.pDocName = "Spool Doc";
        di.pDataType = dataType;
        di.pOutputFile = outputFile;

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    var dwWritten = 0;
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        errorCode = bSuccess == false ? Marshal.GetLastWin32Error() : 0;
        return bSuccess;
    }

So, what i am doing wrong, and how to print\convert the spool file to an XPS document, and if possible to an image file and a text file.

Edit: Adding Additional info

here is the implementation of DOCINFOA

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;
    }

and here is the complete call chain:

The app runs the following code

    var dataTypes = new[] { null, "RAW", "RAW [FF appended]", "RAW [FF auto]", "NT EMF 1.003", "NT EMF 1.006", "NT EMF 1.007", "NT EMF 1.008", "TEXT", "XPS_PASS", "XPS2GDI" };
    foreach (var dataType in dataTypes)
    {
        int errorCode;
        RawPrinterHelper.SendFileToPrinter(@"Microsoft XPS Document Writer", sourceFile, outputFile, dataType, out errorCode);
        //print errorCode
    }

And in RawPrinterHelper

    public static bool SendFileToPrinter(string szPrinterName, string szFileName, string outputPath, string dataType, out int errorCode)
    {
        // Open the file.
        var fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        var br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        // Your unmanaged pointer.
        var documentPath = Path.GetFileName(szFileName);
        var nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        var bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        var pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        var bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength, outputPath, dataType, out errorCode);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        fs.Close();
        return bSuccess;
    }

And the API functions

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:59:58+00:00Added an answer on June 18, 2026 at 7:59 pm

    The problem is your declaration of the DOC_INFO_1 struct. It doesn’t match the layout Windows expects. The Windows declaration looks like this:

    typedef struct _DOC_INFO_1 {
      LPTSTR pDocName;
      LPTSTR pOutputFile;
      LPTSTR pDatatype;
    } DOC_INFO_1;
    

    But your declaration has it like this:

    public class DOCINFOA
    {
        public string pDataType;
        public string pDocName;
        public string pOutputFile;
    }
    

    Reorder the elements in your declaration to match the Windows struct and it should fix your problem.

    EDIT: Bad news. Your approach will not work. Take a look at the documentation for WritePrinter:

    WritePrinter only supports GDI printing and must not be used for XPS
    printing. If your print job uses the XPS or the OpenXPS print path,
    then use the XPS Print API. Sending XPS or OpenXPS print jobs to the
    spooler using WritePrinter is not supported and can result in
    undetermined results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building a program which needs to interface with the d-bus. I'm using
I'm building a c/c++ program and I'm using MS Excel for results representation. The
I'm building a program in C++, using SDL, and am occasionally receiving this error:
I'm building a program, and I'm quite confident using Objective-C, but I don't know
I'm building a program(in 16-Bits) using Assembly(nasm as the assembler), but as I love
I am building a program using C++/CLI in Visual Studio 2008, I've been trying
I'm currently building a program which adds to the current user's shell depending on
I'm currently building a program in C# which will call functions in provided python
I'm building a program that uses weather simulation using OpenGL. I want to include
I'm building a program that uses the following Boost headers (I'm using Microsoft Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.