admin管理员组文章数量:1336085
Is there anyway for which i can access the reason way everytime i execute this, the job status is PrintJobStatus.Deleted? Does it always happen when printing something or is it an error?
using System;
using System.IO;
using System.Linq;
using System.Printing;
using System.Text;
namespace PrintTest
{
class Program
{
static void Main(string[] args)
{
try
{
// Definir la ruta del archivo a imprimir
string filePath = @"C:\Downloads\prueba.pdf";
// Verificar si el archivo existe
if (!File.Exists(filePath))
{
Console.WriteLine("El archivo no existe.");
return;
}
// Leer el contenido del archivo
string textToPrint = File.ReadAllText(filePath);
Console.WriteLine("Archivo leído correctamente.");
// Llamar al método estático GetDefaultPrintQueue directamente desde LocalPrintServer
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
Console.WriteLine("Impresora predeterminada obtenida.");
// Verificar si la impresora está en línea
if (defaultPrintQueue.IsOffline)
{
Console.WriteLine("La impresora está fuera de línea. Asegúrate de que esté encendida y conectada.");
return;
}
// Verificar si la impresora tiene errores
if (defaultPrintQueue.IsBusy)
{
Console.WriteLine("La impresora está ocupada. Espera a que termine el trabajo anterior.");
return;
}
// Verificar si hay trabajos en la cola que puedan indicar un error
if (defaultPrintQueue.GetPrintJobInfoCollection().Count() > 0)
{
Console.WriteLine("Hay trabajos en la cola de impresión. Esperando a que se libere.");
return;
}
// Crear un trabajo de impresión en la cola predeterminada
PrintSystemJobInfo myPrintJob = defaultPrintQueue.AddJob("Impresión de archivo");
Console.WriteLine("Trabajo de impresión agregado a la cola.");
// Escribir en el flujo del trabajo de impresión
Stream myStream = myPrintJob.JobStream;
byte[] myByteBuffer = Encoding.Unicode.GetBytes(textToPrint);
// Escribir los datos en el flujo del trabajo de impresión
myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
myStream.Close();
// Verificar el estado del trabajo de impresión
CheckPrintJobStatus(myPrintJob);
Console.WriteLine("Trabajo de impresión enviado correctamente.");
// Esperar una tecla antes de cerrar
Console.WriteLine("Presiona cualquier tecla para salir...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Error al crear el trabajo de impresión: " + ex.Message);
}
}
// Método para verificar el estado del trabajo de impresión
static void CheckPrintJobStatus(PrintSystemJobInfo printJob)
{
try
{
// Obtener el estado del trabajo de impresión
printJob.Refresh(); // Actualiza el estado del trabajo de impresión
// Imprimir varios atributos para obtener más detalles
Console.WriteLine($"Job ID: {printJob.JobIdentifier}");
Console.WriteLine($"Estado del trabajo: {printJob.JobStatus}");
// Verificar el estado del trabajo
switch (printJob.JobStatus)
{
case PrintJobStatus.Completed:
Console.WriteLine("El trabajo de impresión se completó correctamente.");
break;
case PrintJobStatus.Paused:
Console.WriteLine("El trabajo de impresión está en pausa.");
break;
case PrintJobStatus.Error:
Console.WriteLine("Hubo un error durante el proceso de impresión.");
break;
case PrintJobStatus.Deleted:
Console.WriteLine("El documento ha sido borrado.");
break;
default:
Console.WriteLine($"El trabajo de impresión está en estado: {printJob.JobStatus}");
break;
}
}
catch (IOException ioEx)
{
Console.WriteLine($"Error de entrada/salida: {ioEx.Message}");
}
catch (InvalidOperationException invalidOpEx)
{
Console.WriteLine($"Error de operación no válida: {invalidOpEx.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error inesperado: {ex.Message}");
}
}
}
}
I want to know how i can see if the printer has paper and ink. I have tried sending it to the printer an looking for the error, but i can't continue
本文标签:
版权声明:本文标题:error handling - Is there an attribute for C# System.Printing or similar that allows you to see the reason behind a PrintJobStat 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403622a2468375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论