gpt4 book ai didi

c# - WPF DocumentViewer 覆盖打印按钮

转载 作者:太空狗 更新时间:2023-10-29 23:31:01 26 4
gpt4 key购买 nike

我已经使用自定义 DocumentViewer(如下所示)在我的应用程序中实现了打印预览功能。我在显示预览之前调用了 PrintDialog.ShowDialog(),以便根据纸张方向正确创建文档。

不过,DocumentViewer 打印按钮调用 PrintDialog.ShowDialog() 提示用户再次选择打印机和选项(他们在预览窗口打开之前已经这样做了)。

有没有办法让 DocumentViewer 打印按钮无需调用 PrintDialog.ShowDialog() 就可以打印?

这是我的方法调用:

ReportViewModel.cs

    public void PrintButtonClick(DataGrid dataGrid)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == false)
return;

// Get page size based on print dialog printable area (orientation)
Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);

// create new paginator for datagrid
DataGridDocumentPaginator paginator = new DataGridDocumentPaginator(dataGrid as DataGrid, "Employer Match Report", pageSize, new Thickness(30, 20, 30, 20));
...
}

我这样做是为了能够使用纵向或横向大小值正确生成分页器。否则,DocumentViewer 中的预览文档可能无法根据所选方向正确显示。

PrintDocumentViewer : 文档查看器

   protected override void OnPrintCommand()
{
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

printDialog.PrintTicket.PageOrientation = PageOrientation;
// Code assumes this.Document will either by a FixedDocument or a FixedDocumentSequence
FixedDocument fixedDocument = this.Document as FixedDocument;
FixedDocumentSequence fixedDocumentSequence = this.Document as FixedDocumentSequence;

if (fixedDocument != null)
fixedDocument.PrintTicket = printDialog.PrintTicket;

if (fixedDocumentSequence != null)
fixedDocumentSequence.PrintTicket = printDialog.PrintTicket;

XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);

if (fixedDocument != null)
writer.WriteAsync(fixedDocument, printDialog.PrintTicket);

if (fixedDocumentSequence != null)
writer.WriteAsync(fixedDocumentSequence, printDialog.PrintTicket);

// Create Preview Window and show preview
string s = _previewWindowXaml;
using (var reader = new System.Xml.XmlTextReader(new StringReader(s)))
{
Window preview = System.Windows.Markup.XamlReader.Load(reader) as Window;

DocumentViewer _docViewer = LogicalTreeHelper.FindLogicalNode(preview, "PrintDocumentViewer") as DocumentViewer;
_docViewer.Document = (fixedDocument != null) ? fixedDocument as IDocumentPaginatorSource : fixedDocumentSequence as IDocumentPaginatorSource;

// hide the search bar in the PrintPreview dialog
ContentControl cc = _docViewer.Template.FindName("PART_FindToolBarHost", _docViewer) as ContentControl;
cc.Visibility = Visibility.Collapsed;

preview.ShowDialog();
}
}

最佳答案

您可以填写打印机和纸张尺寸属性,然后不会显示对话框。

var pd = new PrintDialog();
pd.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.NorthAmericaLetter, 816.0, 1056.0);
pd.PrintQueue = new LocalPrintServer().GetPrintQueue("Microsoft Print to PDF");

覆盖打印命令:

<DocumentViewer>
<DocumentViewer.CommandBindings>
<CommandBinding Command="ApplicationCommands.Print" Executed="Print_Executed" />
</DocumentViewer.CommandBindings>
</DocumentViewer>

关于c# - WPF DocumentViewer 覆盖打印按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com