【问题标题】:Excel, Word, PDFs AND PowerPoint print dialoguesExcel、Word、PDF 和 PowerPoint 打印对话框
【发布时间】:2009-05-11 18:42:09
【问题描述】:

有人知道如何在 C# 应用程序中获取所有这些信息,从而将用户引导至打印对话屏幕吗? 我的意思是:
在 gui 中,用户选择一个文档,右键单击该文档,选择打印。不是立即打印出文档,而是出现打印对话框。

有人知道所有这些的语法吗?我尝试使用 MS Office 应用程序的所有互操作并尝试 printdialogue 方法......但没有这样的运气。

谁能给我一些代码?我已经认真地研究了 HOURS 小时,但什么也想不出来。

【问题讨论】:

  • 您能更具体地介绍一下您的方案吗?用户在哪里选择 Office 文档?在资源管理器中?在您自己的应用程序中?你已经看过Application.Dialogs 集合了吗?
  • 我有一个单独的 UI Win 表单,它显示了我所有文档的树形视图。我是 C# 新手,所以我需要我能得到的所有帮助!

标签: c# printing


【解决方案1】:

也许可以看到之前的问答: send-document-to-printer-with-c#

或者,对于打印 FlowDocument 的 WPF 应用程序:

    private void DoThePrint(System.Windows.Documents.FlowDocument document)
    {
        // Clone the source document's content into a new FlowDocument.
        // This is because the pagination for the printer needs to be 
        // done differently than the pagination for the displayed page. 
        // We print the copy, rather that the original FlowDocument. 
        System.IO.MemoryStream s = new System.IO.MemoryStream();
        TextRange source = new TextRange(document.ContentStart, document.ContentEnd);
        source.Save(s, DataFormats.Xaml);
        FlowDocument copy = new FlowDocument();
        TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd);
        dest.Load(s, DataFormats.Xaml);

        // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, 
        // and allowing the user to select a printer.

        // get information about the dimensions of the seleted printer+media. 
        System.Printing.PrintDocumentImageableArea ia = null;
        System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia);

        if (docWriter != null && ia != null)
        {
            DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;

            // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
            paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);
            Thickness t = new Thickness(72);  // copy.PagePadding;
            copy.PagePadding = new Thickness(
                             Math.Max(ia.OriginWidth, t.Left),
                               Math.Max(ia.OriginHeight, t.Top),
                               Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),
                               Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));

            copy.ColumnWidth = double.PositiveInfinity;
            //copy.PageWidth = 528; // allow the page to be the natural with of the output device

            // Send content to the printer.
            docWriter.Write(paginator);
        }

    }

【讨论】:

  • 我不太确定我需要 FlowDocument 的代码。用户将尝试打印文档,只需点击打印对话框,他们就可以选择他们的打印机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多