Document Studio .NET
Next-generation .NET compliant multi-format document viewing & processing suite for .NET developers
Visual Studio 2013/2015/2017/2019

Print without preview using Document Studio .NET's ASP.NET document-viewer control - Version 2016 R2 or below

Learn to display server-side documents and print without preview.
By Pradeep Malage

In our January 2015 introductory article on our ASP.NET viewer control, we had shown how to show client-side documents in the viewer. (The end-user uploads DOCX, DOC or PDF documents and then viewer displays them in the browser without any client-side Office software or PDF plugin.)

Feedback from our customers indicated that they would also want the ASP.NET viewer to display server-side documents without revealing the documents' actual URL. Another important requirement from customers was the ability to print such documents directly to a client-side printer without having to first show them to the end-user. In this article, we will address both requirements.

If you are using latest version of Document Studio (2016 R3 or above), please refer to this article link.

Display a server-side document in the ASP.NET document viewer control

For this, you need to create a web form and embed the viewer control as described in the introductory article. Then, in the code-behind file of the form, use the following code in the Page_Load() method.

public partial class WebForm1 : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
    String sDocID, sDocPath;
    ViewerController vc = new ViewerController();
    sDocPath = Server.MapPath(".") + "\\App_Data\\sample.docx";
    sDocID = vc.LoadDocument(sDocPath);
    Page.ClientScript.RegisterHiddenField("hidden_document_field_id", sDocID);

This code snippet assumes that the server-side documents have been kept in the "App_Data" folder. (You can choose a different location so long as it is accessible to the Page instance.) Next, use a Gnostice.Documents.Controls.HTML.ViewerController instance to transform the document in a manner that is usable to the ASP.NET viewer. This is done using the ViewerController.LoadDocument() method. This method returns a server-side document reference for the viewer. How do we pass this server-side document reference to the viewer? By creating a hidden field with the ClientScript.RegisterHiddenField() method. Here, "hidden_document_field_id" has been used as the hidden field's name. This field needs to be used in the form's JavaScript so that the viewer can display the document.

function displayServerDocumentInViewer() {
  if (document.getElementById('hidden_document_field_id') != null) {

Place this displayServerDocumentInViewer() JavaScript method in the HEAD section of the page. This JavaScript checks if the hidden field exists, and then makes the viewer to display the document identified by the server-side reference found in the hidden field.

To make the viewer display the document as soon as the page loads, we call the displayServerDocumentInViewer() method in the BODY element's onload event.

Video Demo

Print without Preview using ASP.NET document viewer control

In our Limitation of Web Applications, we have mentioned why it is not possible to print to a local printer without showing a prompt. We will not readdress all that here.

Our customers wanted a "Print" button and then document should be printed. They did not want to first show the document and then print the document. The solution for this is to hide the control. This can be done by setting the "display" style property to "none". Then, add a button with an onclick event that calls the viewer's print() command.

Disable the "display" style property of the viewer element and then add a "Print" button for printing the document.
Animation showing how a document can be printed without a preview. As we can't show the printed paper, we have printed to a XPS software printer and then displayed the resultant XPS document in an XPS viewer application.


Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.


A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2023 Gnostice Information Technologies Private Limited. All rights reserved.