Table of Contents [Hide/Show]
Screen ShotsSource Code Initializing DocumentPreview Read MigraDoc DDL from a File Print a Document Save the Document as an RTF File Create an Image from the Current Page Create a Meta File
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(); }
/// /// 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(); }
/// /// 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(); } }
/// /// 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"); }
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... }
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"); }