martedì 24 agosto 2010

WPF - Anteprima di stampa con XPSDocument

C# 4.0

Questo metodo esegue l’anteprima di stampa di una stringa o di un oggetto visual, utilizzando l’oggetto XPSDocument.

///
/// Prints the preview.
///

///
The content.
public static void PrintPreview(string content)
{
byte[] bytes = Encoding.UTF8.GetBytes(content);
MemoryStream memoryStream = new MemoryStream(bytes.Length);
Package package = Package.Open(memoryStream, FileMode.Create, FileAccess.ReadWrite);

Uri uri = new Uri("pack://PackTemporaryUri.xps");
if (PackageStore.GetPackage(uri) == null)
PackageStore.AddPackage(uri, package);

XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri);
FixedDocument fixedDocument = new FixedDocument();
PageContent pageContent = new PageContent();

FixedPage fixedPage = new FixedPage { Background = Brushes.White, Width = 96 * 8.5, Height = 96 * 11 };

TextBlock tbTitle = new TextBlock { Text = content };
FixedPage.SetLeft(tbTitle, 96 * 0.75); // left margin
FixedPage.SetTop(tbTitle, 96 * 0.75); // top margin
fixedPage.Children.Add(tbTitle);

((IAddChild)pageContent).AddChild(fixedPage);

fixedDocument.Pages.Add(pageContent);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(fixedDocument);

DocumentViewer documentViewer = new DocumentViewer
{
Document = xpsDocument.GetFixedDocumentSequence()
};
xpsDocument.Close();

// Viene create dinamicamente una window per la visualizzazione dell'anteprima di stampa.
Window window = new Window
{
Title = "Print Preview",
WindowStartupLocation = WindowStartupLocation.CenterScreen
};

Uri uriIcon = new Uri("pack://application:,,,/Admin.Console;component/Content/Print.png");
window.Icon = BitmapFrame.Create(uriIcon);

Grid grid = new Grid();
grid.Children.Add(documentViewer);
window.Content = grid;

window.ShowDialog();
}

///
/// Prints the visual preview.
///

///
The content.
public static void PrintPreview(Visual content)
{
MemoryStream memoryStream = new MemoryStream();
Package package = Package.Open(memoryStream, FileMode.Create, FileAccess.ReadWrite);

Uri uri = new Uri("pack://PackTemporaryUri.xps");
if (PackageStore.GetPackage(uri) == null)
PackageStore.AddPackage(uri, package);

XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(content);

DocumentViewer documentViewer = new DocumentViewer
{
Document = xpsDocument.GetFixedDocumentSequence()
};
xpsDocument.Close();

// Viene create dinamicamente una window per la visualizzazione dell'anteprima di stampa.
Window window = new Window
{
Title = "Print Preview",
WindowStartupLocation = WindowStartupLocation.CenterScreen
};

Uri uriIcon = new Uri("pack://application:,,,/Admin.Console;component/Content/Print.png");
window.Icon = BitmapFrame.Create(uriIcon);

Grid grid = new Grid();
grid.Children.Add(documentViewer);
window.Content = grid;

window.ShowDialog();
}