0

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?

SHIN JaeGuk
  • 494
  • 1
  • 5
  • 14
  • 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 Feb 11 '19 at 10:27
  • As I mentioned above what I want to is saving a direct file not a physical document by a printer. Thanks. – SHIN JaeGuk Feb 11 '19 at 14:02
  • And that's what `Print to PDF`, `Print to File` or `Print to XPS` do. `PrintDocument` is a class used for printing, not generating files – Panagiotis Kanavos Feb 11 '19 at 14:05
  • @PanagiotisKanavos PrintDocument does make files through PrinterSettings class. Thanks. – SHIN JaeGuk Feb 11 '19 at 14:10
  • 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 driver – Panagiotis Kanavos Feb 11 '19 at 14:16
  • Besides, this question has been asked a lot of times in the past. There are a lot of workarounds, like printint to XPS and then using something like PDFSharp to convert the XPS document to PDF. – Panagiotis Kanavos Feb 11 '19 at 14:18
  • @PanagiotisKanavos I understand your comment that I need to definitely use 3rd party component. That means it's possible in Windows Forms without 3rd party component but not possible in WPF. It's hardly acceptable. Thanks. – SHIN JaeGuk Feb 11 '19 at 14:23
  • No, that's not what my comment says. – Panagiotis Kanavos Feb 11 '19 at 14:24

2 Answers2

1

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);
            }
        }
    }
}
XAMlMAX
  • 2,268
  • 1
  • 14
  • 23
  • This snippet prints to the default prinet, it doesn't specify XPS. It could be used to print to PDF or XPS if the appropriate driver was specified – Panagiotis Kanavos Feb 11 '19 at 11:16
  • Hence _If you can set the default printer to be the XPS_. – XAMlMAX Feb 11 '19 at 11:20
  • Your code invokes a PDF file save dialog which is not the solution what I have asked. Thanks. – SHIN JaeGuk Feb 11 '19 at 14:04
  • @SHINJaeGuk did you try Xamimax's code? `PrintVisual` [doesn't display a dialog box.](https://stackoverflow.com/questions/2995559/how-to-print-directly-without-print-dialog-in-wpf) – Panagiotis Kanavos Feb 11 '19 at 14:29
  • @PanagiotisKanavos Yeah I absolutely did. Right, it doesn't invoke PrintDialog but does PDF file save dialog box which asks the file name and the folder. PrintDialog asks which printing device do I choose. There are 2 dialog boxes for PDF. Thanks. – SHIN JaeGuk Feb 11 '19 at 14:38
1

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.

SHIN JaeGuk
  • 494
  • 1
  • 5
  • 14