كيفية طباعة ملف PDF في .NET Core
تعمل مقتطفات الشيفرة التالية أيضًا مع مكتبة Aspose.PDF.Drawing .
طباعة مستند PDF في .NET Core
تتيح لنا مكتبة 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 إلى فئة Document بقيم Aspose.Pdf.PrintScaling.AppDefault
أو Aspose.Pdf.PrintScaling.None
.
خيار قياس الصفحة الذي يجب اختياره عند عرض مربع حوار الطباعة لهذا المستند. القيم الصالحة هي None
، والتي تشير إلى عدم وجود قياس للصفحة، وAppDefault
، والتي تشير إلى قياس الطباعة الافتراضي للقارئ المتوافق. إذا كانت هذه الإدخال تحتوي على قيمة غير معروفة، يجب استخدام AppDefault
. القيمة الافتراضية: AppDefault
.
طباعة مستندات PDF متعددة في وظيفة طباعة واحدة
أحيانًا، يكون من الضروري طباعة مستندات متعددة ذات صلة معًا كوظيفة طباعة واحدة. يضمن ذلك عدم تداخل هذه المستندات مع مخرجات المستخدمين الآخرين، خاصة مع الطابعات الشبكية البعيدة. تدعم Aspose.PDF طباعة أي عدد من المستندات في وظيفة طباعة واحدة مع إعدادات طابعة مشتركة عبر الطرق الثابتة PrintDocuments
من فئة PdfViewer . يمكن تقديم المستندات المراد طباعتها كمسارات ملفات، أو تدفقات مستندات، أو كائنات 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 );
}