使用 PDF 打印 - 外观
将 PDF 文件打印到默认打印机,使用打印机和页面设置
首先,将文档转换为图像,然后在打印机上打印。
创建一个 PdfViewer 类的实例,该实例可以将 PDF 文件打印到默认打印机,使用 BindPdf 方法将文档打开,并更改必要的设置。此示例使用 A4 格式,纵向方向。在 PrinterSettings 中,首先应设置打印的打印机名称。否则,它将打印到默认打印机。接下来,填写所需的副本数量。
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFFile ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 and PrintDocument
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
var prtdoc = new System . Drawing . Printing . PrintDocument ();
// Set printer name
ps . PrinterName = prtdoc . PrinterSettings . PrinterName ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFFile ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 and PrintDocument
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
var prtdoc = new System . Drawing . Printing . PrintDocument ();
// Set printer name
ps . PrinterName = prtdoc . PrinterSettings . PrinterName ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
为了显示打印对话框,请使用以下代码片段
将 PDF 打印到虚拟打印机
有些打印机可以打印到文件。要使用它们,请设置虚拟打印机的名称,并类似于前面的示例,进行设置。
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFToSoftPrinter ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 ;
// Do not convert document pages to images
viewer . PrintAsImage = false ;
// Create objects for printer and page settings and PrintDocument
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set printer name
ps . PrinterName = "HP Universal Printing PS (v7.0.0)" ;
// Or set the PDF printer
// ps.PrinterName = "Adobe PDF";
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFToSoftPrinter ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 ;
// Do not convert document pages to images
viewer . PrintAsImage = false ;
// Create objects for printer and page settings and PrintDocument
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set printer name
ps . PrinterName = "HP Universal Printing PS (v7.0.0)" ;
// Or set the PDF printer
// ps.PrinterName = "Adobe PDF";
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
隐藏打印对话框
Aspose.PDF for .NET 支持隐藏打印对话框。为此,请使用 PrintPageDialog 属性。
以下代码片段演示了如何隐藏打印对话框。
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFHidePrintDialog ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 XPS/PDF printer name
ps . PrinterName = "OneNote for Windows 10" ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFHidePrintDialog ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 printin
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 XPS/PDF printer name
ps . PrinterName = "OneNote for Windows 10" ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
将彩色 PDF 打印为灰度的 XPS 文件
可以使用 PdfViewer 将彩色 PDF 文档打印为灰度到 XPS 打印机。为此,将属性 PdfViewer.PrintAsGrayscale 设置为 true 。以下代码片段演示了 PdfViewer.PrintAsGrayscale
属性的用法。
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFAsGrayscale ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 ;
// Print the file as grayscale
viewer . PrintAsGrayscale = false ;
// Create objects for printer and page settings
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set XPS/PDF printer name
ps . PrinterName = "OneNote for Windows 10" ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFAsGrayscale ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 ;
// Print the file as grayscale
viewer . PrintAsGrayscale = false ;
// Create objects for printer and page settings
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set XPS/PDF printer name
ps . PrinterName = "OneNote for Windows 10" ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
PDF 转 PostScript 转换
PdfViewer 类提供打印 PDF 文档的能力,并且借助此类,可以将 PDF 文件转换为 PostScript 格式。要将 PDF 文件转换为 PostScript,首先安装任何 PS 打印机,然后只需使用 PdfViewer
打印到文件。
以下代码片段演示了如何打印并将 PDF 转换为 PostScript 格式。
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFToPostScript ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using ( var viewer = new Aspose . Pdf . Facades . PdfViewer ())
{
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 ;
// Do not convert document pages to images
viewer . PrintAsImage = false ;
// Create objects for printer and page settings and PrintDocument
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set printer name
ps . PrinterName = "HP Universal Printing PS (v7.0.0)" ;
// Set output file name and PrintToFile attribute
ps . PrintFileName = dataDir + "PdfToPostScript_out.ps" ;
ps . PrintToFile = true ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingPDFToSoftPrinter ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create PdfViewer object
using var viewer = new Aspose . Pdf . Facades . PdfViewer ();
// Bind PDF document
viewer . BindPdf ( dataDir + "PrintDocument.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 ;
// Do not convert document pages to images
viewer . PrintAsImage = false ;
// Create objects for printer and page settings and PrintDocument
var ps = new Aspose . Pdf . Printing . PrinterSettings ();
var pgs = new Aspose . Pdf . Printing . PageSettings ();
// Set printer name
ps . PrinterName = "HP Universal Printing PS (v7.0.0)" ;
// Set output file name and PrintToFile attribute
ps . PrintFileName = dataDir + "PdfToPostScript_out.ps" ;
ps . PrintToFile = true ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
viewer . PrintDocumentWithSettings ( pgs , ps );
}
检查打印作业状态
PDF 文件可以打印到物理打印机,也可以打印到 Microsoft XPS Document Writer,而无需显示打印对话框,使用 PdfViewer 类。当打印大型 PDF 文件时,过程可能需要很长时间,因此用户可能不确定打印过程是否完成或遇到问题。要确定打印作业的状态,请使用 PrintStatus 属性。以下代码片段演示了如何将 PDF 文件打印到 XPS 文件并获取打印状态。
.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" );
// 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 ;
// Do not convert document pages to images
viewer . PrintAsImage = 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" ;
// Set output file name and PrintToFile attribute
ps . PrintFileName = dataDir + "CheckingPrintJobStatus_out.xps" ;
ps . PrintToFile = true ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
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" );
// 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 ;
// Do not convert document pages to images
viewer . PrintAsImage = 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" ;
// Set output file name and PrintToFile attribute
ps . PrintFileName = dataDir + "CheckingPrintJobStatus_out.xps" ;
ps . PrintToFile = true ;
// Set PageSize (if required)
pgs . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
// Set PageMargins (if required)
pgs . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print document using printer and page settings
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." );
}
}
在单面和双面模式下打印页面
在特定的打印作业中,PDF 文档的页面可以以双面或单面模式打印,但在单个打印作业中不能将某些页面打印为单面而某些页面打印为双面。然而,为了实现这一要求,可以使用不同的页面范围和 PrintingJobSettings 对象。以下代码片段演示了如何在单面模式下打印 PDF 文件的某些页面,而在双面模式下打印其他页面。
.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 PrintingPagesInSimplexAndDuplexMode ()
{
// 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 + "PrintPageRange_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 + "PrintPageRange_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 + "PrintPageRange_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 PrintingPagesInSimplexAndDuplexMode ()
{
// 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 + "PrintPageRange_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 + "PrintPageRange_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 + "PrintPageRange_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 );
}
在单个打印作业中打印多个 PDF 文档
有时,需要将多个相关文档作为单个打印作业一起打印。这确保这些文档不会与其他用户的输出交错,特别是在远程网络打印机上。Aspose.PDF 支持通过 PdfViewer 类的静态 PrintDocuments
方法在单个打印作业中打印任意数量的文档,使用共享的打印机设置。要打印的文档可以作为文件路径、文档流或 Document 对象提供。
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingMultipleDocumentsInSingleJob ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Paths to documents to be printed
var path1 = dataDir + "PrintDocument.pdf" ;
var path2 = dataDir + "Print-PageRange.pdf" ;
var path3 = dataDir + "35925_1_3.xps" ;
// Set up printer and page settings
var printDocument = new System . Drawing . Printing . PrintDocument ();
Aspose . Pdf . Printing . PrinterSettings printerSettings = new Aspose . Pdf . Printing . PrinterSettings ();
printerSettings . PrinterName = printDocument . PrinterSettings . PrinterName ;
Aspose . Pdf . Printing . PageSettings pageSettings = new Aspose . Pdf . Printing . PageSettings ();
pageSettings . PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ;
pageSettings . Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 );
// Print multiple documents in a single print job
Aspose . Pdf . Facades . PdfViewer . PrintDocuments ( printerSettings , pageSettings , path1 , path2 , path3 );
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintingMultipleDocumentsInSingleJob ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Paths to documents to be printed
var path1 = dataDir + "PrintDocument.pdf" ;
var path2 = dataDir + "Print-PageRange.pdf" ;
var path3 = dataDir + "35925_1_3.xps" ;
// Set up printer and page settings
var printDocument = new System . Drawing . Printing . PrintDocument ();
Aspose . Pdf . Printing . PrinterSettings printerSettings = new Aspose . Pdf . Printing . PrinterSettings
{
PrinterName = printDocument . PrinterSettings . PrinterName
};
Aspose . Pdf . Printing . PageSettings pageSettings = new Aspose . Pdf . Printing . PageSettings
{
PaperSize = Aspose . Pdf . Printing . PaperSizes . A4 ,
Margins = new Aspose . Pdf . Devices . Margins ( 0 , 0 , 0 , 0 )
};
// Print multiple documents in a single print job
Aspose . Pdf . Facades . PdfViewer . PrintDocuments ( printerSettings , pageSettings , path1 , path2 , path3 );
}