PDF 到 PostScript 转换
以下代码片段也适用于 Aspose.PDF.Drawing 库。
C# 中的 PDF 到 Postscript
PdfViewer 类提供打印 PDF 文档的能力,并且借助此类,我们还可以将 PDF 文件转换为 PostScript 格式。要将 PDF 文件转换为 PostScript,首先安装任何 PS 打印机,然后借助 PdfViewer 打印到文件。有关安装 PS 打印机的说明,请参考您的打印机供应商提供的说明。以下代码片段向您展示如何打印并将 PDF 转换为 PostScript 格式。
检查打印作业状态
PDF 文件可以打印到物理打印机,也可以打印到 Microsoft XPS Document Writer,而无需显示打印对话框,使用 PdfViewer 类。当打印大型 PDF 文件时,过程可能需要很长时间,因此用户可能不确定打印过程是否完成或遇到问题。要确定打印作业的状态,请使用 PrintStatus 属性。以下代码片段向您展示如何将 PDF 文件打印到 XPS 文件并获取打印状态。
获取/设置打印作业所有者名称
有时需要获取或设置打印作业所有者名称(即,实际在网页上按下打印按钮的用户)。在打印 PDF 文件时需要此信息。为了实现此要求,使用属性 PrinterJobName 。
使用 impersonation
获取打印作业所有者名称的另一种方法是使用 impersonation(在另一个用户上下文中运行打印例程),或者用户可以通过使用 SetJob 例程直接更改所有者名称。
请注意,由于安全考虑,无法使用 Aspose.PDF 打印 API 设置所有者值。属性 PrinterJobName 可用于在后台打印应用程序中设置文档名称列的值。上面共享的代码片段仅显示用户如何将用户名加入文档名称列(例如使用语法 UserName\documentName)。但是,所有者列的设置可以通过用户直接以以下方式实现:
Impersonation。由于所有者列的值包含运行打印代码的用户的值,因此可以在另一个用户上下文中调用 Aspose.PDF 打印 API。例如,请查看此处描述的解决方案。使用 这个 Impersonator 类 ,用户可以实现目标:
使用 Spooler API 和 SetJob 例程
以下代码片段展示了如何在单面模式下打印 PDF 文件的某些页面,以及在双面模式下打印某些页面。
.NET Core 3.1
Copy
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 ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
int printingJobIndex = 0 ;
string outputDir = dataDir ;
var printingJobs = new List < PrintingJobSettings >();
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 );
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
viewer . BindPdf ( dataDir + "Print-PageRange.pdf" );
viewer . AutoResize = true ;
viewer . AutoRotate = true ;
viewer . PrintPageDialog = false ;
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
ps . PrinterName = "Microsoft XPS Document Writer" ;
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . PrintToFile = true ;
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
ps . PrintRange = Aspose . Pdf . Printing . PrintRange . SomePages ;
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
ps . DefaultPageSettings . PaperSize = pgs . PaperSize ;
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
viewer . EndPrint += ( sender , args ) =>
{
if (++ printingJobIndex < printingJobs . Count )
{
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
viewer . PrintDocumentWithSettings ( pgs , ps );
}
};
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
Copy
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 ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
int printingJobIndex = 0 ;
var printingJobs = new List < PrintingJobSettings >();
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 );
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
viewer . BindPdf ( dataDir + "Print-PageRange.pdf" );
viewer . AutoResize = true ;
viewer . AutoRotate = true ;
viewer . PrintPageDialog = false ;
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
ps . PrinterName = "Microsoft XPS Document Writer" ;
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . PrintToFile = true ;
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
ps . PrintRange = Aspose . Pdf . Printing . PrintRange . SomePages ;
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
ps . DefaultPageSettings . PaperSize = pgs . PaperSize ;
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
viewer . EndPrint += ( sender , args ) =>
{
if (++ printingJobIndex < printingJobs . Count )
{
ps . PrintFileName = Path . GetFullPath ( printingJobs [ printingJobIndex ]. OutputFile );
ps . FromPage = printingJobs [ printingJobIndex ]. FromPage ;
ps . ToPage = printingJobs [ printingJobIndex ]. ToPage ;
ps . Duplex = printingJobs [ printingJobIndex ]. Mode ;
viewer . PrintDocumentWithSettings ( pgs , ps );
}
};
viewer . PrintDocumentWithSettings ( pgs , ps );
}