Cetak PDF dalam aplikasi WPF
Potongan kode berikut juga bekerja dengan pustaka Aspose.PDF.Drawing .
Cetak langsung
Pustaka Aspose.PDF memiliki kemampuan untuk mengonversi file PDF ke XPS. Kita dapat menggunakan fungsi ini untuk mengatur pencetakan dokumen.
Mari kita pertimbangkan contoh untuk pencetakan langsung:
.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 );
}
}
Dalam kasus ini, kita akan mengikuti langkah-langkah berikut:
Buka file PDF menggunakan OpenFileDialog.
Konversi PDF ke XPS dan simpan dalam objek MemoryStream.
Kaitkan objek MemoryStream dengan Paket Xps.
Tambahkan paket ke Package Store.
Buat XpsDocument berdasarkan paket.
Dapatkan instance dari FixedDocumentSequence.
Kirim urutan ini ke printer menggunakan PrintDialog.
Lihat dan cetak dokumen
Dalam banyak kasus, pengguna ingin melihat dokumen sebelum mencetak. Untuk menerapkan tampilan, kita dapat menggunakan kontrol DocumentViewer
.
Sebagian besar langkah untuk menerapkan pendekatan ini mirip dengan contoh sebelumnya.
.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 ();
}
}