العمل مع طباعة 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 بالألوان الرمادية
يمكن طباعة مستند PDF ملون إلى طابعة XPS بالألوان الرمادية، باستخدام PdfViewer . لتحقيق ذلك، قم بتعيين خاصية 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، دون إظهار حوار الطباعة، باستخدام فئة 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." );
}
}
طباعة الصفحات في وضع Simplex و Duplex
في مهمة طباعة معينة، يمكن طباعة صفحات مستند PDF إما في وضع Duplex أو في وضع Simplex ولكن لا يمكنك طباعة بعض الصفحات كـ simplex وبعض الصفحات كـ duplex ضمن مهمة طباعة واحدة. ومع ذلك، من أجل تحقيق المتطلبات، يمكن استخدام نطاقات صفحات مختلفة وكائن PrintingJobSettings. يوضح مقتطف الكود التالي كيفية طباعة بعض صفحات ملف PDF في وضع Simplex وبعض الصفحات في وضع Duplex.
.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 طباعة أي عدد من المستندات في مهمة طباعة واحدة مع إعدادات طابعة مشتركة عبر طرق 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 );
}