Demonstrates all techniques you need to preview and print a MigraDoc document, and convert it to a PDF, RTF, or image file.
Screen Shots
Here is a sample screen shot:
Source Code
This sample uses the
MigraDoc.Rendering.Forms.DocumentPreview class to create a preview window.
The
DocumentPreview object receives the document as an
MigraDoc DDL string. You can obtain this DDL string from your
Document object using the
DdlWriter class.
Initializing DocumentPreview
Here's the constructor:
public Viewer()
{
InitializeComponent();
// Create a new MigraDoc document
Document document = SampleDocuments.CreateSample1();
string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
this.pagePreview.Ddl = ddl;
UpdateStatusBar();
}
Read MigraDoc DDL from a File
You can use the
DdlWriter class to store a document in a
MigraDoc DDL file.
This sample also shows how to read these files:
///
/// Opens and shows a MigraDoc DDL file.
///
/// A MigraDoc DDL file is a text based serialization of a MigraDoc document.
///
private void miOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog dialog = null;
try
{
dialog = new OpenFileDialog();
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.Filter = "MigraDoc DDL (*.mdddl)|*.mdddl|All Files (*.*)|*.*";
dialog.FilterIndex = 1;
dialog.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory(), "..\\..");
//dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
Document document = MigraDoc.DocumentObjectModel.IO.DdlReader.DocumentFromFile(dialog.FileName);
string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
this.pagePreview.Ddl = ddl;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text);
this.pagePreview.Ddl = null; // TODO has no effect
}
finally
{
if (dialog != null)
dialog.Dispose();
}
UpdateStatusBar();
}
Print a Document
This sample shows how to print a document:
///
/// Prints the current document on a printer.
///
private void miPrint_Click(object sender, System.EventArgs e)
{
// Reuse the renderer from the preview
DocumentRenderer renderer = this.pagePreview.Renderer;
if (renderer != null)
{
int pageCount = renderer.FormattedDocument.PageCount;
// Creates a PrintDocument that simplyfies printing of MigraDoc documents
MigraDocPrintDocument printDocument = new MigraDocPrintDocument();
// Attach the current printer settings
printDocument.PrinterSettings = this.printerSettings;
if (this.printerSettings.PrintRange == PrintRange.Selection)
printDocument.SelectedPage = this.pagePreview.Page;
// Attach the current document renderer
printDocument.Renderer = renderer;
// Print the document
printDocument.Print();
}
}
Save the Document as an RTF File
Saving the document as an RTF file is pretty simple:
///
/// Creates a RTF file from the current document.
///
private void miRtf_Click(object sender, System.EventArgs e)
{
RtfDocumentRenderer rtf = new RtfDocumentRenderer();
rtf.Render(this.pagePreview.Document, "test.rtf", null);
Process.Start("test.rtf");
}
Create an Image from the Current Page
The same drawing routines can a draw a bitmap, too:
private void miBmp_Click(object sender, System.EventArgs e)
{
int page = this.pagePreview.Page;
// Reuse the renderer from the preview
DocumentRenderer renderer = this.pagePreview.Renderer;
PageInfo info = renderer.FormattedDocument.GetPageInfo(page);
// Create an image
int dpi = 150;
int dx, dy;
if (info.Orientation == PdfSharp.PageOrientation.Portrait)
{
dx = (int)(info.Width.Inch * dpi);
dy = (int)(info.Height.Inch * dpi);
}
else
{
dx = (int)(info.Height.Inch * dpi);
dy = (int)(info.Width.Inch * dpi);
}
Image image = new Bitmap(dx, dy, PixelFormat.Format32bppRgb);
// Create a Graphics object for the image and scale it for drawing with 72 dpi
Graphics graphics = Graphics.FromImage(image);
graphics.Clear(System.Drawing.Color.White);
float scale = dpi / 72f;
graphics.ScaleTransform(scale, scale);
// Create an XGraphics object and render the page
XGraphics gfx = XGraphics.FromGraphics(graphics, new XSize(info.Width.Point, info.Height.Point));
renderer.RenderPage(gfx, page);
gfx.Dispose();
image.Save("test.png", ImageFormat.Png);
Process.Start("mspaint", "test.png"); // Use MSPaint, not Photoshop...
}
Create a Meta File
Meta files are also supported:
private void miMetaFile_Click(object sender, EventArgs e)
{
int page = this.pagePreview.Page;
// Reuse the renderer from the preview
DocumentRenderer renderer = this.pagePreview.Renderer;
PageInfo info = renderer.FormattedDocument.GetPageInfo(page);
// Create an image
float dx, dy;
if (info.Orientation == PdfSharp.PageOrientation.Portrait)
{
dx = (float)(info.Width.Inch * 72);
dy = (float)(info.Height.Inch * 72);
}
else
{
dx = (float)(info.Height.Inch * 72);
dy = (float)(info.Width.Inch * 72);
}
// Create a graphics object as reference
Graphics graphicsDisplay = CreateGraphics();
IntPtr hdc = graphicsDisplay.GetHdc();
// There is a little difference between the display resolution (e.g. 96 DPI) and the real physical solution of the display.
// This must be taken into account...
DeviceInfos devInfo = DeviceInfos.GetInfos(hdc);
// Create the metafile
Metafile metafile = new Metafile("test.emf", hdc,
new RectangleF(0, 0, devInfo.ScaleX * dx, devInfo.ScaleY * dy), MetafileFrameUnit.Point);
graphicsDisplay.ReleaseHdc(hdc);
graphicsDisplay.Dispose();
// Create a Graphics object for the metafile and scale it for drawing with 72 dpi
Graphics graphics = Graphics.FromImage(metafile);
graphics.Clear(System.Drawing.Color.White);
graphics.ScaleTransform(graphics.DpiX / 72, graphics.DpiY / 72);
// Check if size is correct
graphics.DrawLine(Pens.Red, 0, 0, dx, dy);
// Create an XGraphics object and render the page
XGraphics gfx = XGraphics.FromGraphics(graphics, new XSize(info.Width.Point, info.Height.Point));
renderer.RenderPage(gfx, page);
gfx.Dispose();
metafile.Dispose();
Process.Start("test.emf");
}
Note: The samples on this site usually show and discuss code snippets only. The complete source code of the samples with solutions for Visual Studio is available from the
download area on CodePlex.