.NET Core에서 PDF 파일 인쇄하는 방법
다음 코드 조각은 Aspose.PDF.Drawing 라이브러리와도 함께 작동합니다.
.NET Core에서 Pdf 문서 인쇄하기
Aspose.PDF 라이브러리를 사용하면 PDF 파일을 XPS로 변환할 수 있습니다. 이 기능은 문서 인쇄를 구성하는 데 유용할 수 있습니다. 기본 프린터를 사용하는 예제를 살펴보겠습니다.
이 예제에서는 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 PrintWithNetCore ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create the secondary thread and pass the printing method for
// the constructor's ThreadStart delegate parameter.
var printingThread = new Thread (() => PrintPDF ( dataDir + "PrintDocument.pdf" ));
// Set the thread that will use PrintQueue.AddJob to single threading.
printingThread . SetApartmentState ( ApartmentState . STA );
// Start the printing thread. The method passed to the Thread
// constructor will execute.
printingThread . Start ();
// Wait for the printing thread to finish its work
printingThread . Join ();
}
private static void PrintPDF ( string pdfFileName )
{
// Create print server and print queue.
var defaultPrintQueue = LocalPrintServer . GetDefaultPrintQueue ();
// Convert PDF to XPS
using ( var document = new Aspose . Pdf . Document ( pdfFileName ))
{
var xpsFileName = pdfFileName . Replace ( ".pdf" , ".xps" );
document . Save ( xpsFileName , SaveFormat . Xps );
// Print the Xps file while providing XPS validation and progress notifications.
using ( PrintSystemJobInfo xpsPrintJob = defaultPrintQueue . AddJob ( xpsFileName , xpsFileName , false ))
{
Console . WriteLine ( xpsPrintJob . JobIdentifier );
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PrintWithNetCore ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdfFacades_Printing ();
// Create the secondary thread and pass the printing method for
// the constructor's ThreadStart delegate parameter.
var printingThread = new Thread (() => PrintPDF ( dataDir + "PrintDocument.pdf" ));
// Set the thread that will use PrintQueue.AddJob to single threading.
printingThread . SetApartmentState ( ApartmentState . STA );
// Start the printing thread. The method passed to the Thread
// constructor will execute.
printingThread . Start ();
// Wait for the printing thread to finish its work
printingThread . Join ();
}
private static void PrintPDF ( string pdfFileName )
{
// Create print server and print queue.
var defaultPrintQueue = LocalPrintServer . GetDefaultPrintQueue ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( pdfFileName );
// Convert PDF to XPS
var xpsFileName = pdfFileName . Replace ( ".pdf" , ".xps" );
document . Save ( xpsFileName , SaveFormat . Xps );
// Print the Xps file while providing XPS validation and progress notifications.
using PrintSystemJobInfo xpsPrintJob = defaultPrintQueue . AddJob ( xpsFileName , xpsFileName , false );
Console . WriteLine ( xpsPrintJob . JobIdentifier );
}
PDF 페이지 크기에 따른 용지 소스 선택
24.4 릴리스부터는 인쇄 대화 상자에서 PDF 페이지 크기에 따라 용지 소스를 선택할 수 있습니다. 다음 코드 조각은 PDF의 페이지 크기에 따라 프린터 트레이를 선택할 수 있도록 합니다.
이 설정은 Document.PickTrayByPdfSize 속성을 사용하여 켜고 끌 수 있습니다.
인쇄 대화 상자 사전 설정 페이지 스케일링
다음 코드 조각은 PrintScaling 속성이 올바르게 적용되고 PDF에 저장되도록 하는 데 사용됩니다.
PrintScaling 속성은 Aspose.Pdf.PrintScaling.AppDefault
또는 Aspose.Pdf.PrintScaling.None
값을 가진 Document 클래스에 추가되었습니다.
이 문서에 대한 인쇄 대화 상자가 표시될 때 선택할 페이지 스케일링 옵션입니다. 유효한 값은 페이지 스케일링을 적용하지 않음을 나타내는 None
과 준수하는 리더의 기본 인쇄 스케일링을 나타내는 AppDefault
입니다. 이 항목에 인식할 수 없는 값이 있는 경우 AppDefault
가 사용되어야 합니다. 기본값: AppDefault
.
단일 인쇄 작업에서 여러 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 );
}