2016-09-06 124 views
0

我有一個xps文件。當我嘗試直接打印這個文件,我可以與下面的代碼做沒有任何錯誤:VB.Net打印xps文件「文件包含損壞的數據」錯誤

Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue 

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

但是,如果我從一個Web服務作爲一個字節數組,該文件並將其保存爲XPS文件我無法打印它。

我保存的字節數組代碼如下所示:

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

或驗證碼:

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

當我嘗試打印test.xps,我得到的錯誤:

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

我可以處理這個問題嗎?有沒有人有任何想法?


順便說一下,不需要Web服務。 請看我下面的代碼。 你可以試試這個任何xps文件。 首先,我將文件分配爲字節數組 然後,我將字節數組保存爲xps文件。

首先XPS文件是工作,但第二個不工作

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) 

回答

0

我已經解決了問題。

我已經改變了下面的代碼:

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

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

,問題已經解決。

+0

原始問題可能是'Dim bByte(FS.Length)As Byte',當它應該是'Dim bByte(FS.Length - 1)As Byte'。 VB中的數組用最後一個索引而不是大小聲明。 –