Print HTML In C# With Or Without The Web Browser Control And The Print Dialog

CK CK 3788 
Created at
Updated at  
972 0 0

Printing with the web browser control in .NET platform is not hard work. But the challenge for me was to navigate to a specific URL and then print the document with a single click event. There was another challenge I had to face,when I tried to print an HTML document from its source in the same manner. Well,this article describes some techniques to print an HTML document with that challenge (if anyone thinks so). This code is also able to print an HTML page without the print dialog. So,if any one needs printing without any user interaction,this code may be helpful for you. In addition,this code is also able to print an HTML document without any web browser control in the form.
 

<PRE lang=cs nd="7">
object em =null;
axW.ExecWB(
  SHDocVw.OLECMDID.OLECMDID_PRINT,
  SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,
  ref em,ref em);</PRE>
<P nd="11">For printing without print dialog:</P><PRE lang=cs nd="12">
axW.ExecWB(
  SHDocVw.OLECMDID.OLECMDID_PRINT,
  SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
  ref em,ref em);</PRE>
<P nd="15">Now,what if you want to navigate and print with a single click? You 
have to navigate the web browser to a specific URL with its <CODE 
nd="16">Navigate</CODE> method and wait until it loads the whole HTML page. That 
is:</P><PRE lang=cs nd="18">
for(;axW.ReadyState!=SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE;)
{
    System.Windows.Forms.Application.DoEvents();
}</PRE>
<P nd="19">Then print the page with any of the previous options you want.</P>
<P nd="20">Now,if you want to print an HTML page from its source,what should 
do is,just add the HtmlPrinter project to your solution. Then go to Add 
References option,by right clicking on your own project. From the Project tab,select the HtmlPrinter project.</P>
<P nd="21">Now,to print directly,you need to create a <CODE 
nd="22">HtmlPrinter</CODE> object and then call its <CODE 
nd="23">PrintUrlFromMemory</CODE> method with the URL as its parameter.</P><PRE lang=cs nd="25">
using HtmlPrinter;

hpObj=new HtmlPrinter.HtmlPrinter();
hpObj.PrintUrlFromMemory(txtUrl.Text);</PRE>
<P nd="27">Now,you add the code in your project to print the HTML page from its 
source text:</P><PRE lang=cs nd="28">
HtmlPrinter.HtmlPrinter hpObj=new HtmlPrinter.HtmlPrinter();
hpObj.PrintHtml(txtString.Text,true);</PRE>
<P nd="31">If you want to print without the print dialog,then use the following 
line:</P><PRE lang=cs nd="32">hpObj.PrintHtml(txtString.Text,false);</PRE>


 

Tags C# Print HtmlPrinter Print Print HTML PrintDocument PrintHtml PrintUrlFromMemory Facebook X
Comments 0
Similar posts
  1. Adding Custom Paper Sizes To Named Printers
    644
  2. A Customizable Printing Text Class
    427
  3. Simplified .NET Printing In C#
    691
  4. How To Print Text In C#
    453
  5. Printing A Page
    376
  6. Get A Print Dialog Box By Clicking A Button
    817
  7. How To Show The Print Dialog And Print Text Files
    1,294
  8. How to print external document ?
    413
  9. Changing the Title of a Print Dialog in Delphi
    1,092
  10. How To Check If Your Printer Is Connected Using C#
    1,025
  1. How to exit from a console application
    809
  2. How to restrict a program to a single instance
    727
  3. Compress and decompress strings in C#
    970
  4. Graphics in C# - Irregular Forms
    906
  5. Extracting the Country from IP Address
    466
  6. How to run shell command by MFC ?
    434
  7. Binary Search Sample Code
    403
  8. Double Linked List based in C++
    796
  9. String Replace Function For C#
    1,542
  10. How to print character in C# by ASCII code
    475
  11. MFC based World Wide Web HTTP Server Source Code
    458
  12. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    470
  13. UDP Send and Receive Using CAsyncSocket
    1,204
  14. Using the shell to receive notification of removable media being inserted or removed
    680
  15. Adding Custom Paper Sizes To Named Printers
    644
  16. A Customizable Printing Text Class
    427
  17. Simplified .NET Printing In C#
    691
  18. How To Print Text In C#
    453
  19. C# Lists
    1,146
  20. Simple Example of IF/THEN using C#
    1,262
  21. Fast and Good Keyboard/Mouse Test without the message handler
    975
  22. Simple CGI Programming in C
    541
  23. C++ CGI Example working on Apache
    481
  24. Simple C# CGI working on Apache
    476
  25. How to send binary data through C# CGI app?
    993
  26. Interacting With TinyPic From C#
    1,298
  27. Creating A Watched Folder With Assigned Events
    1,653
  28. Calling Your Main Thread From A Worker Thread In C#
    545
  29. HashTable Tutorial In C#
    1,785
  30. Stack Tutorial In C#
    665
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,734
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    86
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    234
  17. The Concept of Scaffolding in AI Agents
    222
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252