0
votes

I use Microsoft Visual Studio 2017 and Microsoft Print to PDF in Windows 10.

I try to make a PDF file of FlowDocument without PrintDialog and I know it's impossible in pure WPF. So I referred system.drawing.printing.PrintDocument of Windows Forms into my WPF project.

I'm stuck on the point of converting FlowDocument to 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? Should I surely use 3rd party component?

2
In other words, you're asking how to print a FlowDocument to PDF. The same way you'd print to any printer, as long as a PDF printer driver is available. What have you tried and what doesn't work? Can you print to an actual printer? Is a PDF printer driver installed?Panagiotis Kanavos
As I mentioned above what I want to is saving a direct file not a physical document by a printer. Thanks.SHIN JaeGuk
And that's what Print to PDF, Print to File or Print to XPS do. PrintDocument is a class used for printing, not generating filesPanagiotis Kanavos
@PanagiotisKanavos PrintDocument does make files through PrinterSettings class. Thanks.SHIN JaeGuk
Because that's what Print to file means. It does so through printer drivers that send the output to files instead of an actual printer. If you want an answer to your question it involves findint the PDF printer driverPanagiotis Kanavos

2 Answers

1
votes

If you can set the default printer to be the PDF or XPS then you can use this code snippet. 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:

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);
            }
        }
    }
}
0
votes

It seems that there is no direct PDF file saving way without any dialog box under current Windows and WPF versions. But I got to the answer through 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.