طباعة PDF في تطبيق WPF
تعمل عينة الكود التالية أيضًا مع مكتبة Aspose.PDF.Drawing .
الطباعة المباشرة
تمتلك مكتبة Aspose.PDF القدرة على تحويل ملفات 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 DirectPrintWpf ()
{
// Select a PDF document to print
var openFileDialog = new OpenFileDialog
{
Filter = "PDF Documents|*.pdf"
};
if ( openFileDialog . ShowDialog () == true )
{
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( openFileDialog . FileName ))
{
using ( var memoryStream = new MemoryStream ())
{
// Convert the document to the XPS format
document . Save ( memoryStream , SaveFormat . Xps );
// Create XPS package
using ( var package = Package . Open ( memoryStream ))
{
//Create URI for the XPS package
//Any Uri will actually be fine here. It acts as a placeholder for the
//Uri of the package inside the PackageStore
var inMemoryPackageName = $"memorystream://{Guid.NewGuid()}.xps" ;
var packageUri = new Uri ( inMemoryPackageName );
//Add the package to PackageStore
PackageStore . AddPackage ( packageUri , package );
// Open the XPS document from the package
using ( var xpsDoc = new XpsDocument ( package , CompressionOption . Maximum , inMemoryPackageName ))
{
// Get the root document sequence
var fixedDocumentSequence = xpsDoc . GetFixedDocumentSequence ();
// Open a print dialog to set printing options
var printDialog = new PrintDialog ();
if ( printDialog . ShowDialog () == true )
{
if ( fixedDocumentSequence != null )
{
// Print converted document
printDialog . PrintDocument ( fixedDocumentSequence . DocumentPaginator ,
"A fixed document" );
}
}
// Remove the package from the store and close the document after the print
PackageStore . RemovePackage ( packageUri );
}
}
}
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DirectPrintWpf ()
{
// Select a PDF document to print
var openFileDialog = new OpenFileDialog
{
Filter = "PDF Documents|*.pdf"
};
if ( openFileDialog . ShowDialog ())
{
// Open PDF document
using var document = new Aspose . Pdf . Document ( openFileDialog . FileName );
// Convert the document to the XPS format
using var memoryStream = new MemoryStream ();
document . Save ( memoryStream , SaveFormat . Xps );
// Create XPS package
using var package = Package . Open ( memoryStream );
//Create URI for the XPS package
//Any Uri will actually be fine here. It acts as a placeholder for the
//Uri of the package inside the PackageStore
var inMemoryPackageName = $"memorystream://{Guid.NewGuid()}.xps" ;
var packageUri = new Uri ( inMemoryPackageName );
//Add the package to PackageStore
PackageStore . AddPackage ( packageUri , package );
// Open the XPS document from the package
using var xpsDoc = new XpsDocument ( package , CompressionOption . Maximum , inMemoryPackageName );
// Get the root document sequence
var fixedDocumentSequence = xpsDoc . GetFixedDocumentSequence ();
// Open a print dialog to set printing options
var printDialog = new PrintDialog ();
if ( printDialog . ShowDialog () == true )
{
if ( fixedDocumentSequence != null )
{
// Print converted document
printDialog . PrintDocument ( fixedDocumentSequence . DocumentPaginator ,
"A fixed document" );
}
}
// Remove the package from the store and close the document after the print
PackageStore . RemovePackage ( packageUri );
}
}
في هذه الحالة، سنتبع الخطوات التالية:
فتح ملف PDF باستخدام OpenFileDialog.
تحويل PDF إلى XPS وتخزينه في كائن MemoryStream.
ربط كائن MemoryStream مع حزمة Xps.
إضافة الحزمة إلى متجر الحزم.
إنشاء XpsDocument بناءً على الحزمة.
الحصول على نسخة من FixedDocumentSequence.
إرسال هذه السلسلة إلى الطابعة باستخدام PrintDialog.
عرض وطباعة المستند
في العديد من الحالات، يرغب المستخدمون في رؤية المستند قبل الطباعة. لتنفيذ العرض، يمكننا استخدام عنصر التحكم DocumentViewer
.
معظم الخطوات لتنفيذ هذا النهج مشابهة للمثال السابق.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PreviewDocumentWithDocumentViewer ( DocumentViewer docViewer )
{
// Select a PDF document to print
var openFileDialog = new OpenFileDialog
{
Filter = "PDF Documents|*.pdf"
};
if ( openFileDialog . ShowDialog () == true )
{
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( openFileDialog . FileName ))
{
using ( var memoryStream = new MemoryStream ())
{
// Convert the document to the XPS format
document . Save ( memoryStream , SaveFormat . Xps );
// Create XPS package
using ( var package = Package . Open ( memoryStream ))
{
//Create URI for the XPS package
var inMemoryPackageName = $"memorystream://{Guid.NewGuid()}.xps" ;
var packageUri = new Uri ( inMemoryPackageName );
//Add package to PackageStore
PackageStore . AddPackage ( packageUri , package );
// Open the XPS document from the package
using ( var xpsDoc = new XpsDocument ( package , CompressionOption . Maximum , inMemoryPackageName ))
{
// Display the document in the DocumentViewer
docViewer . Document = xpsDoc . GetFixedDocumentSequence ();
}
}
}
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PreviewDocumentWithDocumentViewer ( DocumentViewer docViewer )
{
// Select a PDF document to print
var openFileDialog = new OpenFileDialog
{
Filter = "PDF Documents|*.pdf"
};
if ( openFileDialog . ShowDialog () == true )
{
// Open PDF document
using var document = new Aspose . Pdf . Document ( openFileDialog . FileName );
// Convert the document to the XPS format
using var memoryStream = new MemoryStream ();
document . Save ( memoryStream , SaveFormat . Xps );
// Create XPS package
using var package = Package . Open ( memoryStream );
//Create URI for the XPS package
var inMemoryPackageName = $"memorystream://{Guid.NewGuid()}.xps" ;
var packageUri = new Uri ( inMemoryPackageName );
//Add package to PackageStore
PackageStore . AddPackage ( packageUri , package );
// Open the XPS document from the package
using var xpsDoc = new XpsDocument ( package , CompressionOption . Maximum , inMemoryPackageName );
// Display the document in the DocumentViewer
docViewer . Document = xpsDoc . GetFixedDocumentSequence ();
}
}