0

I have an xps file. When I try to print this file directly, I can do it without any error with my below code:

Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue

Dim xpsPrintJob As PrintSystemJobInfo = defaultPrintQueue.AddJob("test", "C:\Temp\test.xps", False)

However, If I get this file from a web service as a byte array and save it as an xps file I cannot print it.

My save byte array codes are below:

FS = New IO.FileStream("C:\Temp\test.xps", FileMode.Create)
                        FS.Write(arrayByte, 0, arrayByte.Length)
                        FS.Close()

or this code:

 File.WriteAllBytes("c:\Temp\test.xps", arrayByte)

When I try to print test.xps, I'm getting the error:

An unhandled exception of type 'System.Printing.PrintJobException' occurred in System.Printing.dll

Additional information: An exception occurred while creating print job information. Check inner exception for details.

Detailed view of the error is here

Have can I handle this problem? Is anyone has any idea?


By the way, there is no need a web service. Please see my below code. You can try this any xps file. Firstly, I'm assinging the file as byte array Then, I'm saving the byte array as an xps file.

First XPS file is working but second one isn't working

Dim FS As FileStream
FS = File.Open("C:\Temp\test2.xps", FileMode.Open, FileAccess.Read)

Dim bByte(FS.Length) As Byte
FS.Read(bByte, 0, FS.Length)
FS.Close()

File.WriteAllBytes("c:\Temp\test2byte.xps", bByte)

Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue

'This is working
Dim xpsPrintJob1 As PrintSystemJobInfo = defaultPrintQueue.AddJob("Test", "C:\Temp\test2.xps", False)

'This is not working
Dim xpsPrintJob2 As PrintSystemJobInfo = defaultPrintQueue.AddJob("Test", "C:\Temp\test2byte.xps", False)
Gurcan
  • 428
  • 1
  • 8
  • 19

1 Answers1

0

I've solve the problem.

I've changed the below code:

Dim FS As FileStream
FS = File.Open("C:\Temp\test2.xps", FileMode.Open, FileAccess.Read)

Dim bByte(FS.Length) As Byte
FS.Read(bByte, 0, FS.Length)
FS.Close()

as

Dim bByte() As Byte = File.ReadAllBytes("C:\Temp\test2.xps")

and the problem has been solved.

Gurcan
  • 428
  • 1
  • 8
  • 19
  • The original problem may have been `Dim bByte(FS.Length) As Byte` when it should be `Dim bByte(FS.Length - 1) As Byte`. Arrays in VB are declared with their last index rather than the size. – Andrew Morton Sep 06 '16 at 10:19