XpsDocumentWriter WriteAsync(DocumentPaginator)另存为模式对话框 [英] XpsDocumentWriter WriteAsync(DocumentPaginator) Save as modal dialog
问题描述
我正在使用C#中XpsDocumentWriter类中存在的WriteAsync(DocumentPaginator)方法来打印xps文件.此方法在内部打开另存为"对话框,用户可以在其中提供XPS文件的名称.但是,此另存为"对话框是无模式的
对话.有什么方法可以将另存为"对话框设置为模式对话框?我的意图是在另存为"对话框仍处于打开状态时,不允许用户切换到任何其他窗口.
I am using WriteAsync(DocumentPaginator) method present in XpsDocumentWriter class in C# to print an xps file. This method internally opens a 'Save as' Dialog where user can provide the name of the XPS file. However this 'Save as' dialog is a modeless
dialog. Is there any way to make the 'Save as' Dialog as modal dialog? My intention is not to allow the user to switch to any other window while the 'Save as' dialog is open still.
推荐答案
Hi Gurucharan_Singh,
Hi Gurucharan_Singh,
请尝试以下使用FixedDocument的方法,请更改为DocumentPaginator.
Please try the following method, which use FixedDocument, please change to DocumentPaginator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Xps;
using System.Windows.Documents;
using System.IO;
using System.Xaml;
using System.Windows.Markup;
using System.Windows.Xps.Packaging;
using System.Windows;
using System.IO.Packaging;
using System.Windows.Controls;
namespace Workpiece
{
public class FileHelper
{
public static string GetXPSFromDialog(bool isSaved)
{
if (isSaved)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XPS Document files (*.xps)|*.xps|Txt Document files(*.txt)|*.txt";
saveFileDialog.FilterIndex = 1;
if (saveFileDialog.ShowDialog() == true)
{
return saveFileDialog.FileName;
}
else
{
return null;
}
}
else return string.Format("{0}\\temp.xps", Environment.CurrentDirectory);//create a temp storage
}
public static string OpenXPSFileFromDialog()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XPS Document Files(*.xps)|*.xps";
if (openFileDialog.ShowDialog() == true)
return openFileDialog.FileName;
else
return null;
}
public static bool SaveXPS(FixedPage page,bool isSaved)
{
FixedDocument fixedDoc = new FixedDocument();
fixedDoc.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);
PageContent pageContent = new PageContent();
((IAddChild)pageContent).AddChild(page);
fixedDoc.Pages.Add(pageContent);
string containerName = 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.WriteAsync(fixedDoc);//write XPS file
_xpsDocument.Close();
return true;
}
else return false;
}
static XpsDocument xpsPackage = null;
public static void LoadDocumentViewer(string xpsFileName, DocumentViewer viewer)
{
XpsDocument oldXpsPackage = xpsPackage;
xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);
FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();
viewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
if (oldXpsPackage != null)
oldXpsPackage.Close();
xpsPackage.Close();
}
}
}
#用法:
Uri printViewUri = new Uri("/Workpiece;Component/PrintView.xaml",UriKind.Relative);
FixedPage printPage = (FixedPage)Application.LoadComponent(printViewUri);
ComboBox combo = printPage.FindName("dataCombo") as ComboBox;
combo.SelectedIndex = nameCombo.SelectedIndex;
if (FileHelper.SaveXPS(printPage, true))
MessageBox.Show(string.Format("save acess"));
else
MessageBox.Show("save failed");
最诚挚的问候,
吴可乐
这篇关于XpsDocumentWriter WriteAsync(DocumentPaginator)另存为模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!