【问题标题】:Attach a PDF File to a Print Dialog将 PDF 文件附加到打印对话框
【发布时间】:2015-09-14 17:45:14
【问题描述】:

我正在尝试将 PDF 文件附加到打印对话框,但我还没有找到方法。

我正在使用 WPF 应用程序,并且我有一些与打印相关的代码,如下所示:

private void Imprimir() 
    {
        try
        {
            FixedDocument document = null;
            PageContent pageContent = null;
            FixedPage fixedPage = null;

            PrintDialog printDlg = new PrintDialog();
            if (printDlg.ShowDialog() != true)
                return;
            document.DocumentPaginator.PageSize = new System.Windows.Size(1400, 1450);
            fixedPage.Width = document.DocumentPaginator.PageSize.Width;
            fixedPage.Height = document.DocumentPaginator.PageSize.Height;
            fixedPage.Margin = new Thickness(96, 96, 0, 0);
            fixedPage.Children.Add(this);

            ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
            document.Pages.Add(pageContent);
            printDlg.PrintDocument(document.DocumentPaginator, "Impresion Cierre");
            fixedPage.Children.Clear();

        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }
    }

但是,通过这种方式,我只是打印添加到固定页面的 UI 元素。 我一直在寻找其他代码,但我一无所获。 所以,我不知道是否可以将本地存储的 PDF 文件添加到打印对话框?

感谢您的帮助...

【问题讨论】:

    标签: wpf pdf printdialog


    【解决方案1】:

    你不能使用 PrintDialog 做到这一点。有多种选择,具体取决于您的目标:

            var printQueue = LocalPrintServer.GetDefaultPrintQueue();
            using (var input = File.OpenRead("path_to_your.pdf")) {
                using (var job = printQueue.AddJob()) {
                    using (var output = job.JobStream) {
                        input.CopyTo(output);
                    }
                }
            }
    

    将您的文件的打印作业静默发送到本地打印队列。打印作业是可配置的。

    或者,您可以使用 adobe reader 为您处理(或安装在用户机器上的另一个 pdf 阅读器),但以您的 pdf 路径作为 FileName 和 Verb="print" 开始进程。

    另一种选择是使用第三方工具(如 ghostscript),它可以帮助您。

    【讨论】:

    • 好吧,我真正需要的是获取 PDF 文件,然后显示一个必须包含 PDF 的打印对话框,因此用户必须选择打印机并最终打印此文档...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多