In this blog post, we will demonstrate how to print Word documents from your .NET applications using Essential DocIO. Syncfusion Essential DocIO is a .NET library that can read, write, modify and rasterize word documents without any dependence on Microsoft Office.
The first step to print word documents is to rasterize the pages as images as shown below:
C#:
// Loads the Word document c WordDocument doc = new WordDocument(@"....Template.docx"); // Renders the Word document pages as image Image[] images = doc.RenderAsImages(ImageType.Metafile); // Closes the Word document doc.Close();
The System.Drawing.Printing namespace represents print-related entities of Windows Forms applications. Through the instance of PrintDocument class, the user can specify the printer settings and page settings and print the document. Invoking the PrintDocument.Print method raises the PrintDocument.PrintPage event, which should be handled to perform the document layout for printing. The output to print can be specified using the Graphics property of the PrintPageEventArgs instance obtained from the PrintDocument.PrintPage event. Use the DrawImage method of the Graphics instance to print the Word document pages that are rendered as images.
The following code snippet demonstrates how to print the Word document pages that have been rendered as an image:
C#:
//Creates new PrintDialog instance. System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog(); //Sets new PrintDocument instance to print dialog. printDialog.Document = new PrintDocument(); //Enables the print current page option. printDialog.AllowCurrentPage = true; //Enables the print selected pages option. printDialog.AllowSomePages = true; //Sets the start and end page index printDialog.PrinterSettings.FromPage = 1; printDialog.PrinterSettings.ToPage = images.Length; //Opens the print dialog box. if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //Checks if the selected page range is valid. if (printDialog.PrinterSettings.FromPage > 0 && printDialog.PrinterSettings.ToPage <= images.Length) { //Updates the start page of the document to print. startPageIndex = printDialog.PrinterSettings.FromPage - 1; //Updates the end page of the document to print. endPageIndex = printDialog.PrinterSettings.ToPage; //Hooks the PrintPage event to handle be drawing pages for printing. printDialog.Document.PrintPage += new PrintPageEventHandler(OnPrintPage); //Prints the document. printDialog.Document.Print(); } } private void OnPrintPage(object sender, PrintPageEventArgs e) { //Gets the print start page width. int currentPageWidth = images[startPageIndex].Width; //Gets the print start page height. int currentPageHeight = images[startPageIndex].Height; //Gets the visible bounds width for print. int visibleClipBoundsWidth = (int)e.Graphics.VisibleClipBounds.Width; //Gets the visible bounds height for print. int visibleClipBoundsHeight = (int)e.Graphics.VisibleClipBounds.Height; //Checks if the page layout is landscape or portrait. if (currentPageWidth > currentPageHeight) { //Translates to draw the page as landscape. e.Graphics.TranslateTransform(0, visibleClipBoundsHeight); //Rotates the object at 270 degrees. e.Graphics.RotateTransform(270.0f); //Draws the current page image. e.Graphics.DrawImage(images[startPageIndex], new System.Drawing.Rectangle(0, 0, currentPageWidth, currentPageHeight)); } else { //Draws the current page image. e.Graphics.DrawImage(images[startPageIndex], new System.Drawing.Rectangle(0, 0, visibleClipBoundsWidth, visibleClipBoundsHeight)); } //Disposes the current page image after drawing. images[startPageIndex].Dispose(); //Increments the start page index. startPageIndex++; //Updates if the document contains some more pages to print. if (startPageIndex < endPageIndex) e.HasMorePages = true; }
A complete working sample of the code demonstrated above is available for download
For more information on DocIO, visit Syncfusion’s website.