طباعة PDF في .NET Framework
تعمل مقتطفات الكود التالية أيضًا مع مكتبة Aspose.PDF.Drawing .
طباعة ملف PDF في C# - طباعة ملف PDF إلى الطابعة الافتراضية باستخدام إعدادات الطابعة والصفحة
تصف هذه المقالة كيفية طباعة ملف PDF إلى الطابعة الافتراضية باستخدام إعدادات الطابعة والصفحة في C#.
تسمح لك فئة PdfViewer بطباعة ملف PDF إلى الطابعة الافتراضية. تحتاج إلى إنشاء كائن PdfViewer وفتح PDF باستخدام طريقة BindPdf . لتحديد إعدادات طباعة مختلفة، استخدم فئات PageSettings و PrinterSettings . أخيرًا، استدعِ طريقة PrintDocumentWithSettings لطباعة PDF إلى الطابعة الافتراضية. يوضح مقتطف الكود التالي كيفية طباعة PDF إلى الطابعة الافتراضية مع إعدادات الطابعة والصفحة.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SimplePrint ()
{
// 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 SimplePrint ()
{
// 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
منذ إصدار 24.4، أصبح من الممكن اختيار مصدر الورق حسب حجم صفحة PDF في مربع حوار الطباعة. يمكّن مقتطف الكود التالي من اختيار درج الطابعة بناءً على حجم صفحة PDF.
يمكن تشغيل هذه الميزة وإيقافها باستخدام خاصية Document.PickTrayByPdfSize .