Conversion de PDF en PostScript
Le code suivant fonctionne également avec la bibliothèque Aspose.PDF.Drawing .
PDF en Postscript en C#
La classe PdfViewer fournit la capacité d’imprimer des documents PDF et avec l’aide de cette classe, nous pouvons également convertir des fichiers PDF en format PostScript. Pour convertir un fichier PDF en PostScript, installez d’abord une imprimante PS et imprimez simplement dans un fichier avec l’aide de PdfViewer. Pour installer une imprimante PS, référez-vous aux instructions fournies par votre fournisseur d’imprimante. Le code suivant vous montre comment imprimer et convertir un PDF en format PostScript.
Vérification du statut de la tâche d’impression
Un fichier PDF peut être imprimé sur une imprimante physique ainsi que sur le Microsoft XPS Document Writer, sans afficher de boîte de dialogue d’impression, en utilisant la classe PdfViewer . Lors de l’impression de grands fichiers PDF, le processus peut prendre beaucoup de temps, donc l’utilisateur peut ne pas être certain si le processus d’impression est terminé ou a rencontré un problème. Pour déterminer le statut d’une tâche d’impression, utilisez la propriété PrintStatus . Le code suivant vous montre comment imprimer le fichier PDF dans un fichier XPS et obtenir le statut d’impression.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CheckingPrintJobStatus ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Instantiate PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.pdf" );
// Print the file with adjusted size
viewer . AutoResize = true ;
// Hide printing dialog
viewer . PrintPageDialog = false ;
// Create Printer Settings object
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Specify the printer name
ps . PrinterName = "Microsoft XPS Document Writer" ;
// Resultant Printout name
ps . PrintFileName = dataDir + "CheckingPrintJobStatus_out.xps" ;
// Print the output to file
ps . PrintToFile = true ;
// Set a range of pages to print
ps . FromPage = 1 ;
ps . ToPage = 2 ;
ps . PrintRange = Aspose . Pdf . Printing . PrintRange . SomePages ;
// Specify the page size of printout
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
ps . DefaultPageSettings . PaperSize = pgs . PaperSize ;
// Specify page margins
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print the document with settings specified above
viewer . PrintDocumentWithSettings ( pgs , ps );
// Check the print status
if ( viewer . PrintStatus != null )
{
// An exception was thrown
if ( viewer . PrintStatus is Exception ex )
{
// Get exception message
Console . WriteLine ( ex . Message );
}
}
else
{
// No errors were found. Printing job has completed successfully
Console . WriteLine ( "Printing completed without any issue." );
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CheckingPrintJobStatus ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Instantiate PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.pdf" );
// Print the file with adjusted size
viewer . AutoResize = true ;
// Hide printing dialog
viewer . PrintPageDialog = false ;
// Create Printer Settings object
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Specify the printer name
ps . PrinterName = "Microsoft XPS Document Writer" ;
// Resultant Printout name
ps . PrintFileName = dataDir + "CheckingPrintJobStatus_out.xps" ;
// Print the output to file
ps . PrintToFile = true ;
// Set a range of pages to print
ps . FromPage = 1 ;
ps . ToPage = 2 ;
ps . PrintRange = Aspose . Pdf . Printing . PrintRange . SomePages ;
// Specify the page size of printout
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
ps . DefaultPageSettings . PaperSize = pgs . PaperSize ;
// Specify page margins
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print the document with settings specified above
viewer . PrintDocumentWithSettings ( pgs , ps );
// Check the print status
if ( viewer . PrintStatus != null )
{
// An exception was thrown
if ( viewer . PrintStatus is Exception ex )
{
// Get exception message
Console . WriteLine ( ex . Message );
}
}
else
{
// No errors were found. Printing job has completed successfully
Console . WriteLine ( "Printing completed without any issue." );
}
}
Obtenir/Définir le nom du propriétaire de la tâche d’impression
Parfois, il est nécessaire d’obtenir ou de définir le nom du propriétaire de la tâche d’impression (c’est-à-dire, l’utilisateur réel qui a appuyé sur un bouton d’impression sur une page web). Cette information est requise lors de l’impression du fichier PDF. Pour accomplir cette exigence, la propriété PrinterJobName est utilisée.
Utilisation de l’imitation
Une autre approche pour obtenir le nom du propriétaire de la tâche d’impression est d’utiliser l’imitation (exécuter des routines d’impression dans un autre contexte utilisateur) ou l’utilisateur peut changer le nom du propriétaire directement en utilisant la routine SetJob.
Veuillez noter qu’il n’est pas possible de définir la valeur du propriétaire en utilisant l’API d’impression Aspose.PDF pour des raisons de sécurité. La propriété PrinterJobName peut être utilisée pour définir la valeur de la colonne du nom du document dans l’application d’impression spooler. Le code partagé ci-dessus montre simplement comment l’utilisateur peut joindre le nom d’utilisateur dans la colonne du nom du document (par exemple en utilisant la syntaxe UserName\documentName). Mais le paramétrage des colonnes du propriétaire peut être mis en œuvre de la manière suivante directement par l’utilisateur :
Imitation. Comme la valeur de la colonne du propriétaire contient la valeur de l’utilisateur qui exécute le code d’impression, il existe un moyen d’invoquer l’API d’impression Aspose.PDF à l’intérieur d’un autre contexte utilisateur. Par exemple, jetez un œil à la solution décrite ici. En utilisant cette classe Impersonator , l’utilisateur peut atteindre cet objectif :
Utilisation de l’API Spooler et de la routine SetJob
Le code suivant montre comment imprimer certaines pages d’un fichier PDF en mode Simplex et certaines pages en mode Duplex.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
struct PrintingJobSettings
{
public int ToPage { get ; set ; }
public int FromPage { get ; set ; }
public string OutputFile { get ; set ; }
public Aspose . Pdf . Printing . Duplex Mode { get ; set ; }
}
private static void PrintUsingSpoolerApi ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
int printingJobIndex = 0 ;
string outputDir = dataDir ;
var printingJobs = new List < PrintingJobSettings >();
// Create multiple printing jobs to print different page ranges with different duplex settings
var printingJob1 = new PrintingJobSettings ();
printingJob1 . FromPage = 1 ;
printingJob1 . ToPage = 3 ;
printingJob1 . OutputFile = outputDir + "PrintUsingSpoolerApi_p1-3_out.xps" ;
printingJob1 . Mode = Aspose . Pdf . Printing . Duplex . Default ;
printingJobs . Add ( printingJob1 );
PrintingJobSettings printingJob2 = new PrintingJobSettings ();
printingJob2 . FromPage = 4 ;
printingJob2 . ToPage = 6 ;
printingJob2 . OutputFile = outputDir + "PrintUsingSpoolerApi_p4-6_out.xps" ;
printingJob2 . Mode = Aspose . Pdf . Printing . Duplex . Simplex ;
printingJobs . Add ( printingJob2 );
PrintingJobSettings printingJob3 = new PrintingJobSettings ();
printingJob3 . FromPage = 7 ;
printingJob3 . ToPage = 7 ;
printingJob3 . OutputFile = outputDir + "PrintUsingSpoolerApi_p7_out.xps" ;
printingJob3 . Mode = Aspose . Pdf . Printing . Duplex . Default ;
printingJobs . Add ( printingJob3 );
// Create PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "Print-PageRange.pdf" );
// Set attributes for printing
// Print the file with adjusted size
viewer . AutoResize = true ;
// Print the file with adjusted rotation
viewer . AutoRotate = true ;
// Do not produce the page number dialog when printing
viewer . PrintPageDialog = false ;
// Create objects for printer and page settings
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set printer name
ps . PrinterName = "Microsoft XPS Document Writer" ;
// Set output file name and PrintToFile attribute
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . PrintToFile = true ;
// Set parameters for the first print job
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
ps . PrintRange = Aspose . Pdf . Printing . PrintRange . SomePages ;
// Set paper size and margins
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
ps . DefaultPageSettings . PaperSize = pgs . PaperSize ;
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Chain other print jobs at the end of the finished job
viewer . EndPrint += ( sender , args ) =>
{
if (++ printingJobIndex < printingJobs . Count )
{
// Set the next print job parameters
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
// Run the next print job
viewer . PrintDocumentWithSettings ( pgs , ps );
}
};
// Run the first print job
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
struct PrintingJobSettings
{
public int ToPage { get ; set ; }
public int FromPage { get ; set ; }
public string OutputFile { get ; set ; }
public Aspose . Pdf . Printing . Duplex Mode { get ; set ; }
}
private static void PrintUsingSpoolerApi ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
int printingJobIndex = 0 ;
var printingJobs = new List < PrintingJobSettings >();
// Create multiple printing jobs to print different page ranges with different duplex settings
var printingJob1 = new PrintingJobSettings ();
printingJob1 . FromPage = 1 ;
printingJob1 . ToPage = 3 ;
printingJob1 . OutputFile = dataDir + "PrintUsingSpoolerApi_p1-3_out.xps" ;
printingJob1 . Mode = Aspose . Pdf . Printing . Duplex . Default ;
printingJobs . Add ( printingJob1 );
PrintingJobSettings printingJob2 = new PrintingJobSettings ();
printingJob2 . FromPage = 4 ;
printingJob2 . ToPage = 6 ;
printingJob2 . OutputFile = dataDir + "PrintUsingSpoolerApi_p4-6_out.xps" ;
printingJob2 . Mode = Aspose . Pdf . Printing . Duplex . Simplex ;
printingJobs . Add ( printingJob2 );
PrintingJobSettings printingJob3 = new PrintingJobSettings ();
printingJob3 . FromPage = 7 ;
printingJob3 . ToPage = 7 ;
printingJob3 . OutputFile = dataDir + "PrintUsingSpoolerApi_p7_out.xps" ;
printingJob3 . Mode = Aspose . Pdf . Printing . Duplex . Default ;
printingJobs . Add ( printingJob3 );
// Create PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "Print-PageRange.pdf" );
// Set attributes for printing
// Print the file with adjusted size
viewer . AutoResize = true ;
// Print the file with adjusted rotation
viewer . AutoRotate = true ;
// Do not produce the page number dialog when printing
viewer . PrintPageDialog = false ;
// Create objects for printer and page settings
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set printer name
ps . PrinterName = "Microsoft XPS Document Writer" ;
// Set output file name and PrintToFile attribute
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . PrintToFile = true ;
// Set parameters for the first print job
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
ps . PrintRange = Aspose . Pdf . Printing . PrintRange . SomePages ;
// Set paper size and margins
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
ps . DefaultPageSettings . PaperSize = pgs . PaperSize ;
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Chain other print jobs at the end of the finished job
viewer . EndPrint += ( sender , args ) =>
{
if (++ printingJobIndex < printingJobs . Count )
{
// Set the next print job parameters
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
// Run the next print job
viewer . PrintDocumentWithSettings ( pgs , ps );
}
};
// Run the first print job
viewer . PrintDocumentWithSettings ( pgs , ps );
}