Articles in this section
Category / Section

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

6 mins read

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


You can download complete working sample from GitHub.



Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied