Imprimir PDF en aplicación WPF
El siguiente fragmento de código también funciona con la biblioteca Aspose.PDF.Drawing .
Impresión directa
La biblioteca Aspose.PDF tiene la capacidad de convertir archivos PDF a XPS. Podemos usar esta función para organizar la impresión de documentos.
Consideremos el ejemplo para la impresión directa:
.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 );
}
}
En este caso, seguiremos estos pasos:
Abrir el archivo PDF usando OpenFileDialog.
Convertir PDF a XPS y almacenarlo en un objeto MemoryStream.
Asociar el objeto MemoryStream con el paquete Xps.
Agregar el paquete al Almacén de Paquetes.
Crear un XpsDocument basado en el paquete.
Obtener una instancia de FixedDocumentSequence.
Enviar esta secuencia a la impresora usando PrintDialog.
Ver y imprimir documento
En muchos casos, los usuarios quieren ver el documento antes de imprimir. Para implementar una vista, podemos usar un control DocumentViewer
.
La mayoría de los pasos para implementar este enfoque son similares al ejemplo anterior.
.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 ();
}
}