Welcome
Guest
•
Login
PDFsharp and MigraDoc Wiki
Navigation
¶
Main Page
Random Page
Create a new Page
All Pages
Categories
Navigation Paths
File Management
Create Account
Quick Search
»
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Back
MigraDoc Sample: Document Viewer
Modified on 2015/09/14 10:17
by
Thomas Hövel
Categorized as
MigraDoc Samples
,
Samples
{s:navigationPrevUpNext|HelloMigraDoc-sample|MigraDocSamples|Images-sample} Demonstrates all techniques you need to preview and print a MigraDoc document, and convert it to a PDF, RTF, or image file. {TOC} ==Screen Shots== Here is a sample screen shot: {s:ImageThumbLink|The Document Viewer application|DocumentViewer-sample%2fDocumentViewerth.png|DocumentViewer-sample%2fDocumentViewer.png} ==Source Code== This sample uses the {s:className|MigraDoc.Rendering.Forms.DocumentPreview} class to create a preview window. The {s:className|DocumentPreview} object receives the document as an [MigraDocDDL|MigraDoc DDL] string. You can obtain this DDL string from your {s:className|Document} object using the {s:className|DdlWriter} class. ===Initializing DocumentPreview=== Here's the constructor: {s:beginCsharp} 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(); } {s:endCsharp} ===Read MigraDoc DDL from a File=== You can use the {s:className|DdlWriter} class to store a document in a [MigraDocDDL|MigraDoc DDL] file. This sample also shows how to read these files: {s:beginCsharp} /// <summary> /// Opens and shows a MigraDoc DDL file. /// /// A MigraDoc DDL file is a text based serialization of a MigraDoc document. /// </summary> 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(); } {s:endCsharp} ===Print a Document=== This sample shows how to print a document: {s:beginCsharp} /// <summary> /// Prints the current document on a printer. /// </summary> 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(); } } {s:endCsharp} ===Save the Document as an RTF File=== Saving the document as an RTF file is pretty simple: {s:beginCsharp} /// <summary> /// Creates a RTF file from the current document. /// </summary> 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"); } {s:endCsharp} ===Create an Image from the Current Page=== The same drawing routines can a draw a bitmap, too: {s:beginCsharp} 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... } {s:endCsharp} ===Create a Meta File=== Meta files are also supported: {s:beginCsharp} 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"); } {s:endCsharp} {s:sampleSourceCode}
Meta Keywords:
Meta Description:
Change Comment:
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles
ScrewTurn Wiki version 3.0.5.600. Some of the icons created by
FamFamFam
.
Impressum - Privacy Policy, Data Protection Declaration, Legal Notice