Imprimir PDF em aplicativo WPF
O seguinte trecho de código também funciona com a biblioteca Aspose.PDF.Drawing .
Impressão direta
A biblioteca Aspose.PDF tem a capacidade de converter arquivos PDF para XPS. Podemos usar essa função para organizar a impressão de documentos.
Vamos considerar o exemplo para impressão direta:
.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 );
}
}
Neste caso, seguiremos estas etapas:
Abra o arquivo PDF usando OpenFileDialog.
Converta PDF para XPS e armazene-o no objeto MemoryStream.
Associe o objeto MemoryStream ao pacote Xps.
Adicione o pacote ao Armazenamento de Pacotes.
Crie um XpsDocument com base no pacote.
Obtenha uma instância do FixedDocumentSequence.
Envie essa sequência para a impressora usando PrintDialog.
Visualizar e imprimir documento
Em muitos casos, os usuários querem ver o documento antes de imprimir. Para implementar uma visualização, podemos usar um controle DocumentViewer
.
A maioria das etapas para implementar essa abordagem é semelhante ao exemplo 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 ();
}
}