PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Printing with MigraDoc 1.50 beta 2 and later
https://forum.pdfsharp.net/viewtopic.php?f=8&t=3202
Page 1 of 1

Author:  TH-Soft [ Tue Sep 29, 2015 6:57 pm ]
Post subject:  Printing with MigraDoc 1.50 beta 2 and later

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 and later (some names have changed, so it needs a bit renaming).

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 a MigraDocPrintDocument.cs that was adapted for version 1.50 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 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 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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/