Excel,Word,PDF和PowerPoint打印对话

时间:2009-05-11 18:42:09

标签: c# printing

有人知道如何在C#应用程序中获取所有这些,引导用户进入打印对话屏幕吗? 我的意思是这样的:
在gui中,用户选择文档,右键单击文档,选择“打印”。打印对话出现了,而不是立即打印出文档。

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

有人可以提供一些代码吗?我已经认真对待这个小时,并且无法想出一件事。

1 个答案:

答案 0 :(得分:0)

也许看到此前的问答: 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);
        }

    }