2

I am working on an application where I have to put an extra line of text after original printed text.

For this, I am using FindFirstPrinterChangeNotification and FindNextPrinterChangeNotification methods of Print Spooler API which works fine.

I am able to get Print Queue which shows number of job count 1.

I am using following code to add a new job in Print Queue:

        // Create the printer server and print queue objects
        LocalPrintServer localPrintServer = new LocalPrintServer();
        PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

        // Call AddJob
        PrintSystemJobInfo myPrintJob = defaultPrintQueue.AddJob();

        // Write a Byte buffer to the JobStream and close the stream
        Stream myStream = myPrintJob.JobStream;
        Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.");
        myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
        myStream.Close();

My code execute successfully without any exception but the new job doesn't get printed along with original text.

  • You just copied the MSDN sample code. Which warns: "This code only works with printers that can detect and print plain text. Some of them cannot.". Turn "some" into "most", not a lot of dot-matrix printers around anymore. Don't do this, use PrintDocument instead. – Hans Passant Feb 15 '17 at 07:32
  • please check this http://stackoverflow.com/a/6106155/1849024 – imsome1 Feb 15 '17 at 07:33
  • @HansPassant thanks for reply. Will this code work for POS thermal printers or I have to use PrintDocument for them also? – 720p Download Watch Dabangg 3 Feb 15 '17 at 07:37
  • @imsome1 thanks for your reply. My scenario is to print some extra line of text at the end of original printed text every time when a new print initiated. – 720p Download Watch Dabangg 3 Feb 15 '17 at 07:43
  • 2
    From the PrintSystemJobInfo.JobStream documentation: "Starting with the .NET Framework 4.5 on Windows 8 and later versions of the operating system, data written to this stream must be in XPS format as a package stream." – Tony Edgecombe Oct 07 '17 at 12:07
  • @TonyEdgecombe you were right, this was t he problem. Thank you very much! The closest solution I found to it is this: https://vishalsbsinha.wordpress.com/2014/05/06/how-to-programmatically-c-net-print-a-pdf-file-directly-to-the-printer/ – AzzamAziz Oct 25 '17 at 02:46

1 Answers1

2

As Tony pointed out in the comments, the JobStream has changed in .NET Framework 4.5 for Windows 8 and up to use the XPS document format. If you want to print, you're going to have to follow the guideline.

I have not found a solution that works, but you can try using the XPS Printing API

The closest solution to that I found were here and here as mentioned by Microsoft here

AzzamAziz
  • 2,144
  • 1
  • 24
  • 34