Print PDF in WPF application
The following code snippet also work with Aspose.PDF.Drawing library.
Direct print
The Aspose.PDF library has the ability to convert PDF files to XPS. We can use this function to organize the printing of documents.
Let’s consider the example for direct printing:
.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 the 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 an 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 the 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 an 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 );
}
}
In this case, we will follow these steps:
Open PDF file using OpenFileDialog.
Convert PDF to XPS and store it in MemoryStream object.
Associate MemoryStream object with Xps Package.
Add the package to the Package Store.
Create an XpsDocument based on package.
Get an instance of the FixedDocumentSequence.
Send this sequence to the printer using PrintDialog.
View and print document
In many cases, users want to see the document before printing. To implement a view, we can use a DocumentViewer
control.
Most of the steps for implementing this approach are similar to the previous example.
.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 the 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 an 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 the 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 an 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 ();
}
}