简体   繁体   English

使用Windows窗体的PrintDocument将WPF FlowDocument直接保存为PDF文件

[英]Saving WPF FlowDocument to PDF file directly by using PrintDocument of Windows Forms

I use Microsoft Visual Studio 2017 and Microsoft Print to PDF in Windows 10. 我在Windows 10中使用Microsoft Visual Studio 2017和Microsoft Print to PDF。

I try to make a PDF file of FlowDocument without PrintDialog and I know it's impossible in pure WPF. 我尝试不使用PrintDialog制作FlowDocument的PDF文件,我知道在纯WPF中是不可能的。 So I referred system.drawing.printing.PrintDocument of Windows Forms into my WPF project. 因此,我将Windows窗体的system.drawing.printing.PrintDocument引用到我的WPF项目中。

I'm stuck on the point of converting FlowDocument to PrintDocument. 我很想将FlowDocument转换为PrintDocument。

PrintDocument _PrintDocument = (PrintDocument)FlowDocument1;

IDocumentPaginatorSource _DocumentPaginatorSource = FlowDocument1;
PrintDocument _PrintDocument = FlowDocument1;

Neither of them works. 它们都不起作用。

Is it possible or is there any sideway to make PDF by assigning the folder and file name with code? 通过为文件夹和文件名分配代码来制作PDF是否可能,或者有其他方法吗? Should I surely use 3rd party component? 我应该确定使用第三方组件吗?

If you can set the default printer to be the PDF or XPS then you can use this code snippet. 如果您可以将默认打印机设置为PDF或XPS,则可以使用此代码段。 It will use the default printer and then print the visual that you want. 它将使用默认打印机,然后打印所需的图像。 Bear in mind if you need different orientation than Landscape you should change it. 请记住,如果您需要的方向与横向不同,则应进行更改。
EDIT: 编辑:
For completeness you can search for a printer queue that is PDF printing like this: 为了完整起见,您可以搜索像这样的PDF打印的打印机队列:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;

namespace Services.Printing
{
    public static class PrintingService
    {
        public static void Print(Visual elementToPrint, string description)
        {
            using (var printServer = new LocalPrintServer())
            {
                var dialog = new PrintDialog();
                //Find the PDF printer
                var qs = printServer.GetPrintQueues();
                var queue = qs.FirstOrDefault(q => q.Name.Contains("PDF"));
                if(queue == null) {/*handle what you want to do here (possibly use XPS?)*/}
                dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
                dialog.PrintQueue = queue;
                dialog.PrintVisual(elementToPrint, description);
            }
        }
    }
}

It seems that there is no direct PDF file saving way without any dialog box under current Windows and WPF versions. 在当前的Windows和WPF版本下,没有任何对话框,似乎没有直接保存PDF文件的方法。 But I got to the answer through PdfSharp.Xps. 但是我通过PdfSharp.Xps找到了答案。 It is quite simple and the quility of documents is well preserved. 这非常简单,并且文件的完整性得到很好的保留。

You can easily use it by Nuget Package Manager of Visual Studio. 您可以通过Visual Studio的Nuget软件包管理器轻松使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM