Printing with MigraDoc 1.50 beta 2

Earlier versions of MigraDoc up to version 1.32 included the class MigraDocPrintDocument that allows MigraDoc to print directly.
This class can also be used with MigraDoc 1.50 beta 2.

The current implementation of class MigraDocPrintDocument works with the GDI build of MigraDoc only and can only be used on platforms where GDI+ is available.
If you are using Windows Desktop and the GDI build, just copy the class file MigraDocPrintDocument.cs into your project and use it. At the end of this post is a link to a ZIP file that includes MigraDocPrintDocument.cs and a sample that shows how to use it.

If you are using the WPF build of MigraDoc on a platform that supports GDI+, then you can also use MigraDocPrintDocument for printing. But you have to use a small trick to make it work.
You have to create one assembly that contains the class MigraDocPrintDocument and that references the GDI build of MigraDoc. Your application can still use the WPF build of MigraDoc.
And now for the small trick: you can invoke printing through a new class MigraDocPrintDocumentEx which internally calls class MigraDocPrintDocument. You have to pass your MigraDoc document to class MigraDocPrintDocumentEx as a string in the MDDDL format that MigraDoc uses to persist files. It takes only one line of code to convert from Document to MDDDL and the sample shows how to do it.

Further limitations of printing with MigraDocPrintDocument: Fonts from a FontResolver won’t be used for printing.
If you want to use private fonts also for printing, then you currently have to use the GDI build of MigraDoc and register your fonts in the XPrivateFontCollection.

Here is a sample solution that shows MigraDocPrintDocument at work and also shows printing from an application that uses the WPF build of MigraDoc.
Download MigraDocPrintingDemo.zip.

6 thoughts on “Printing with MigraDoc 1.50 beta 2”

  1. Hi, I’m trying to implement this printing with MigraDoc along with private fonts, however, I cannot seem to get the XPrivateFontCollection stuff working, is there an example/sample of how to do that with the MigraDoc and PdfSharp 1.5 beta3b GDI version?

    1. It should work with fonts in the XPrivateFontCollection (but I admit I haven’t tried it recently). It cannot work using IFontResolver.

    1. There is nothing missing, just open the SLN in Visual Studio and you can click on the CS file of the project. There is no XAML file though.
      The WPF project links to the CS file in the GDI project folder.

  2. It would be a wonderful thing if this worked. Unfortunately, it requires a PrinterSettings object, and the WPF PrintDialog class contains no such object, instead it has PrintTicket and PrintQueue. There is no cast or convert, and no mapping libraries. So after all the effort, it turns out to be useless.

    1. When using GDI/GDI+ for printing you have to open the normal printer settings dialogue to get the PrinterSettings object you need.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.