We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to do silent printing to print the Word document by rendering document pages as Image using Essential DocIO?

Essential DocIO supports to do silent printing to print the Word documents using System.Drawing.Printing namespace of .NET Framework Class Library.

First step is to render the Word document content as images as shown below using Essential DocIO.

C#:

// Load the word Document

WordDocument doc = new WordDocument(@"..\..\Template.docx");

// Convert Word Document into image

images = doc.RenderAsImages(ImageType.Metafile);

doc.Close();

 

VB:

' Load the word Document

Dim doc As New WordDocument("..\..\Template.docx")

' Convert Word Document into image

images = doc.RenderAsImages(ImageType.Metafile)

doc.Close()

Second step is to do silent printing to print the Word document pages that are rendered as images by Using PrintDocument property. It helps to do the complete printing using PrintDialog with default printer settings, page settings and print the document by invoking the PrintDocument.Print method. Kindly refer the blog for more details to print the Word document with custom printer settings.

 

Below code snippets demonstrates how to do silent printing to print Word documents by rendering document pages as image:

 

C#:

//Create a PrintDialog

System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();

//dialog.PrinterSetting

printDialog.Document = new PrintDocument();

//Retrieve the Page need to be rendered

printDialog.Document.PrintPage += new PrintPageEventHandler(OnPrintPage);

//Print the document

printDialog.Document.Print();

 

/// <summary>

/// The PrintPage event is called for each page to be printed.

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected virtual void OnPrintPage(object sender, PrintPageEventArgs e)

{

//Current page width

int currentPageWidth = images[startPageIndex].Width;

//Current page height

int currentPageHeight = images[startPageIndex].Height;

//Visible clip bounds width

int visibleClipBoundsWidth = (int)e.Graphics.VisibleClipBounds.Width;

//Visible clip bounds height

int visibleClipBoundsHeight = (int)e.Graphics.VisibleClipBounds.Height;

//Draw the current page

e.Graphics.DrawImage(images[startPageIndex], new System.Drawing.Rectangle(0, 0, visibleClipBoundsWidth, visibleClipBoundsHeight));

 

//Dispose the current page

images[startPageIndex].Dispose();

//Increment the start page index

startPageIndex++;

//check if the start page index is lesser than end page index

if (startPageIndex < endPageIndex)

e.HasMorePages = true;//if the document contain more than one pages

else

startPageIndex = 0;

}

 

VB:

'Create a PrintDialog

Dim printDialog As New System.Windows.Forms.PrintDialog()

' dialog.PrinterSetting

printDialog.Document = New PrintDocument()

'Retrieve the Page need to be rendered

printDialog.Document.PrintPage += New PrintPageEventHandler(OnPrintPage)

'Print the document

printDialog.Document.Print()

 

''' The PrintPage event is called for each page to be printed.

Protected Overridable Sub OnPrintPage(sender As Object, e As PrintPageEventArgs)

'Current page width

Dim currentPageWidth As Integer = images(startPageIndex).Width

'Current page height

Dim currentPageHeight As Integer = images(startPageIndex).Height

'Visible clip bounds width

Dim visibleClipBoundsWidth As Integer = CInt(e.Graphics.VisibleClipBounds.Width)

'Visible clip bounds height

Dim visibleClipBoundsHeight As Integer = CInt(e.Graphics.VisibleClipBounds.Height)

'Draw the current page

e.Graphics.DrawImage(images(startPageIndex), New System.Drawing.Rectangle(0, 0, visibleClipBoundsWidth, visibleClipBoundsHeight))

'Dispose the current page

images(startPageIndex).Dispose()

'Increment the start page index

startPageIndex += 1

'check if the start page index is lesser than end page index

If startPageIndex < endPageIndex Then

e.HasMorePages = True

Else

'if the document contain more than one pages

startPageIndex = 0

End If

End Sub

The below sample demonstrates how to do silent printing to print the Word document using Essential DocIO

Sample

Article ID: Published Date: Last Revised Date: Platform: Control: .NET Framework:
4887 05/15/2015 05/15/2015 Windows Forms DocIO 2.0
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.