/// <summary>
/// 将多个RichTextBox内容保存为XPS打印文件
/// </summary>
/// <param name="PageSize">分页页数</param>
/// <param name="PageData">数据源</param>
private void PrintDataPage(int PageSize, string PageData)
{
// ICollectionView paraCollection;
if (PageData == "")
{
MessageBox.Show(string.Format("未查询到相应数据,无法打印!"));
return;
}
FixedPage[] tt = new FixedPage[PageSize];
int t = sp.Children.Count;
for (int i = 0; i < t;i++ )//为FixedPage[] tt每页内容赋值
{
MyRichTextBox mb = sp.Children[0] as MyRichTextBox;
sp.Children.Remove(mb);
tt[i] = new FixedPage();
tt[i].Children.Add(mb);
}
if (FileH.SaveXPS(tt, false, PageSize))
{
// MessageBox.Show(string.Format("文件保存成功"));
}
else
{
MessageBox.Show("取消文件保存");
}
}
public class FileH
{
public string GetXPSFromDialog(bool isSaved)
{
if (isSaved)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XPS Document files (*.xps)|*.xps";
saveFileDialog.FilterIndex = 1;
if (saveFileDialog.ShowDialog() == true)
{
return saveFileDialog.FileName;
}
else
{
return null;
}
}
else return string.Format("{0}\\printMB.xps", Environment.CurrentDirectory);//制造一个临时存储
}
/// <summary>
/// 这个静态方法主要是显示选择对话框以提供文件的保存位置
/// 将传入的FixedPage对象数组(多页)写入到.xps文件
/// </summary>
/// <param name="page"></param>
/// <param name="page"></param>
/// <param name="isSaved"></param>
/// <returns></returns>
public static bool SaveXPS(FixedPage[] page, bool isSaved, int iPageCount)
{
FixedDocument fixedDoc = new FixedDocument();//创建一个文档
fixedDoc.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);
PageContent[] pageContent = new PageContent[iPageCount];
for (int i = 0; i < iPageCount; i++)
{
pageContent[i] = new PageContent();
((IAddChild)pageContent[i]).AddChild(page[i]);
fixedDoc.Pages.Add(pageContent[i]);//将对象加入到当前文档中
}
FileH fh = new FileH();
string containerName = fh.GetXPSFromDialog(isSaved);
if (containerName != null)
{
try
{
File.Delete(containerName);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
XpsDocument _xpsDocument = new XpsDocument(containerName, FileAccess.Write);
XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);
xpsdw.Write(fixedDoc);//写入XPS文件
_xpsDocument.Close();
return true;
}
else return false;
}
static XpsDocument xpsPackage = null;
public static void LoadDocumentViewer(string xpsFileName, DocumentViewer viewer)
{
XpsDocument oldXpsPackage = xpsPackage;//保存原来的XPS包
xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);//从文件中读取XPS文档
FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();//从XPS文档对象得到FixedDocumentSequence
viewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
if (oldXpsPackage != null)
oldXpsPackage.Close();
xpsPackage.Close();
}
}
xps承载页面(打印预览)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="RichEditorLibrary.PrintWindow"
x:Name="Window"
Title="PrintWindow"
UseLayoutRounding="True"
Width="640" Height="480" Loaded="Window_Loaded">
<Grid x:Name="LayoutRoot">
<DocumentViewer x:Name="docViewer" />
</Grid>
</Window>
后台代码:
/// <summary>
/// Interaction logic for PrintWindow.xaml
/// </summary>
public partial class PrintWindow : Window
{
public PrintWindow()
{
this.InitializeComponent();
setaa();
// Insert code required on object creation below this point.
}
//传递一个公共的数据类
public string fixedDocFile;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FileH.LoadDocumentViewer(fixedDocFile, docViewer);
}
public void setaa()
{
#region 设置全屏
this.WindowState = System.Windows.WindowState.Maximized;
//this.WindowStyle = System.Windows.WindowStyle.None;
//this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
this.Topmost = false;
this.Left = 0.0;
this.Top = 0.0;
this.MaxWidth = SystemParameters.WorkArea.Width;
this.MaxHeight = SystemParameters.WorkArea.Height;
this.MinWidth = SystemParameters.WorkArea.Width;
this.MinHeight = SystemParameters.WorkArea.Height;
#endregion
}
}